sjson

package
v1.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT, MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sjson

源自 tidwall/sjson v1.2.9, 包路径改为 github.com/fufuok/pkg/xjson/sjson, 依赖本仓库的 xjson/gjson.

对上游的修正

以下修正在同步上游时必须保留, 每项都有回归测试.

位置 上游问题 本地修正 回归测试
maxpad (appendBuild/appendRawPaths) 只限制每层补齐. 路径 a 后重复 100 次 .100000 (701 字节) 仍生成约 50MB 合法 JSON, 累计分配约 240MB, 且不报错 单次调用中所有层的补齐合计不超过 100000 个 null, 超出返回错误并保留原 JSON. 先判本层再判累计: 任一层单独超限 (包括上层已有补齐时) 仍返回上游原有错误信息, 只有累计超限才报 total padding, 下标为 math.MaxInt 时错误文本不会溢出成负数 TestPadTotalBudget
setComplexPath 按 gjson 返回的偏移直接切片, 而上游 gjson 的偏移并不总指向原文 (管道之后相对子值, # 之后接静态值、修饰符或子选择器时为父元素偏移, 字符串元素以 0 为基准). 会 panic (如 Set("[1]", "#.!true", v))、拼出非法 JSON (如 Set("[12,13]", "#(>10)#.!1", "x") 得 ["x"2,"x"3]), 或在内容碰巧相同时改写别的字段而输出仍合法 (如对 {"xy":1,"a":[{"b":1}]} 写 a|#.b 改掉 xy, {"x":1,"a":[" [1,2]"]} 写 a.#.0 改掉 x). 路径来自外部输入时, 单个请求即可触发 本地 gjson 已改为按内存同源给出偏移 (见 gjson/README.md), 这里只做兜底: 每个写入点内容一致, 前后是值的分隔符 (排除数字前缀和键名位置), 不在字符串字面量内部 (gjson 会深入字符串内容取值, 如 {"a":["[1]"]} 的 a.#(=="[1]").0, 或顶层是字符串 "[1,2]" 的文档), 区间互不重叠; 单结果偏移 0 只对 JSON Lines 路径 (以 .. 开头) 且 Raw 与原文共享起始内存时放行. 先校验全部写入点再修改, 任一不满足即不修改也不报错, 与路径不存在时一致. 管道右侧、#.@this、对象上的 #( (按普通键解析)、查询值里含 `| 等此前拒绝或写错的路径, 现在写到 gjson 结果所在位置. 早先在 sjson 侧按路径语法白名单拒写 (约 250 行), 先后因splitPossiblePipe、parseArrayPath不处理转义、对象上的#(等 gjson 解析细节出现漏判和误拒, 已删除. 语义模型差分 (3 个种子, 各约 21.6 万条复杂路径): 错写 0、非法输出 0, 上游错写约 3900 次、panic 约 40 次, 保守误拒 0~1 条 (此前约 1.8%); 对象解释攻击形状约 100 万条错写 0 (上游约 59 万); 去掉 gjson 任一同源检查或 sjson 字符串内部检查的变异体都被回归测试抓到. 无需转义的值一次分配构建结果 (上游每个写入点复制整个文档并往返转换), 需转义的值另有编码分配: WSL 上对比 v1.4.2 小文档多结果快约 12%、分配 19→13 次, 单匹配查询持平、8→5 次. 字符串内部检查须从文档开头扫描到最后一个写入点, 长文本和长数字段交给IndexByte跳过 (最坏只比逐字节多一次IndexByte`, 跳过的区间推进游标, 多写入点总成本仍线性). 约 260~450KB 文档末元素命中时 (WSL 单核 8 轮, 对比 v1.4.2): 长文本 135→91µs、数字数组 236→158µs; 字符串内嵌转义 JSON 392→454µs (+16%)、短字段密集 342→568µs (+66%) 仍慢, 为纯 Go 逐字节扫描的下限 (约 1.5GB/s, 与一次 gjson 解析相当); 首元素命中均更快, 分配字节少 50%~62%. 逐字节扫描时 263KB 长文本末元素命中曾为 324µs
appendRawPaths, set 乐观路径 路径中间经过的值是字符串时, 把字符串的 Raw 交给 gjson, 而 gjson 顶层会跳到第一个 [ 或 {: Set({"a":"[1,2]"}, "a.0", "x") 得 {"a":"["x",2]"} (非法 JSON, 不报错), ["x[1,2]y"] 写 0.1 值 , 得 ["x[1,","]y"] (合法但拆开了字符串), Delete({"a":"[1,2]"}, "a.0") 得 {"a":"[2]"} (合法但改了字符串文本). 文档本身是字符串时乐观原地替换同样写进字符串. v1.4.2 相同 当前值恰好是一个字符串字面量时按 "未找到" 处理, 与数字等标量及不含括号的字符串一致: Set 替换为新建的对象或数组 ({"a":["x"]}), Delete 不变. 以字符串开头的非法片段 (如 "k":{...}) 不算字符串, 保持上游跳到其中第一个 { 的处理. 三方差分 30 万次操作中 27 条与上游不同, 逐条核实上游改在字符串内部、本地把整个字符串替换为合法容器或保持不变 TestSimplePathThroughString
atoui 超过 8 字节的路径段直接调用 strconv.ParseUint, q_utm_source 这类普通键名每次生成一个错误对象, SetBytes 新增键从 3 次分配变为 5 次 先确认全为数字再解析, 普通键名不分配 TestAtouiNoAlloc
deleteTailItem 删除以转义引号开头的键 (如 \") 时多退一位, 越过键的起始引号, 误删前一个键或留下 {"\"":,"y":2} 这类非法 JSON 去掉多余的回退 TestDeleteKeyLeadingEscapedQuote
set 乐观原地替换 同时开启 Optimistic 和 ReplaceInPlace 时, 写入需要转义的字符串 (非 ASCII、引号、控制字符) 会原样返回输入, 修改被静默丢弃 需要转义时改走分配新切片的路径 TestOptimisticInPlaceEscape
set 乐观原地替换的值别名 同时开启 Optimistic 和 ReplaceInPlace, 且字符串值 ([]byte) 与输入 JSON 共享内存 (如取自原值或相邻字段的子切片) 时, 先写开引号会覆盖值的首字节, 如 {"a":12345,"b":1} 写入其中的 123 得 {"a":""23","b":1}, 非法 JSON 且不报错. v1.4.2 相同 先复制值再写开引号: copy 按 memmove 处理重叠, 新值不长于原值, 写入范围不触及其后待移动的尾部. 无额外分配 TestOptimisticInPlaceAliasedValue

已知问题 (上游同样存在, 暂不修改)

  • maxpad 只约束单次调用. 对同一文档用多个外部路径反复 Set 时, 每次仍可补齐 100000 个 null (约 500KB), 增长会累积. 例如约 4KB 的查询串含 364 个 xx.99999 键时, 正文累计约 173MB, 分配约 70GB. 把外部输入当路径的调用方须自行限制总大小, 或先用转义把外部键当作普通键名.
  • 路径键含非法 UTF-8 时, 每次 Set 都会追加一个重复键. 写入时键被替换为 U+FFFD, 下次查找按原字节比较而找不到.
  • 开启 Optimistic 时, 以 .. 开头的路径按 gjson 的 JSON Lines 语义处理, 可能替换整个文档.
  • ..0 等不含特殊字符的 JSON Lines 路径按普通键路径处理, 不会定位到对应行.
  • 上游 v1.2.6 起的行为变化 (NaN/Inf 报错、补齐上限 100000、空路径表示空键等) 见 xjson/README.md.

同步上游

用 go mod download -json github.com/tidwall/sjson@<version> 取得源码, 逐版本对比后合入功能改动. 保留导入路径、中文注释和本文件列出的修正, 迁移上游新增测试, 并更新 xjson/README.md 的版本表.

Documentation

Overview

Package sjson provides setting json values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(json, path string) (string, error)

Delete deletes a value from json for the specified path.

func DeleteBytes

func DeleteBytes(json []byte, path string) ([]byte, error)

DeleteBytes deletes a value from json for the specified path.

func DeleteBytesOptions added in v1.3.7

func DeleteBytesOptions(json []byte, path string, options *Options) ([]byte, error)

DeleteBytesOptions deletes a value from json for the specified path, with the given options. options 可为 nil; 选项沿用 SetBytesOptions 的提示语义, 不保证原地复用输入.

func DeleteOptions added in v1.3.7

func DeleteOptions(json, path string, options *Options) (string, error)

DeleteOptions deletes a value from json for the specified path, with the given options. 路径和错误语义与 Delete 一致; options 可为 nil, 字符串输入不会原地修改.

func Set

func Set(json, path string, value any) (string, error)

Set sets a json value for the specified path. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. An error is returned if the path is not valid.

A path is a series of keys separated by a dot.

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "friends": [
    {"first": "James", "last": "Murphy"},
    {"first": "Roger", "last": "Craig"}
  ]
}
"name.last"          >> "Anderson"
"age"                >> 37
"children.1"         >> "Alex"

func SetBytes

func SetBytes(json []byte, path string, value any) ([]byte, error)

SetBytes sets a json value for the specified path. If working with bytes, this method preferred over Set(string(data), path, value)

func SetBytesOptions

func SetBytesOptions(json []byte, path string, value any,
	opts *Options,
) ([]byte, error)

SetBytesOptions sets a json value for the specified path with options. If working with bytes, this method preferred over SetOptions(string(data), path, value)

func SetOptions

func SetOptions(json, path string, value any,
	opts *Options,
) (string, error)

SetOptions sets a json value for the specified path with options. A path is in dot syntax, such as "name.last" or "age". This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. An error is returned if the path is not valid.

func SetRaw

func SetRaw(json, path, value string) (string, error)

SetRaw sets a raw json value for the specified path. This function works the same as Set except that the value is set as a raw block of json. This allows for setting premarshalled json objects.

func SetRawBytes

func SetRawBytes(json []byte, path string, value []byte) ([]byte, error)

SetRawBytes sets a raw json value for the specified path. If working with bytes, this method preferred over SetRaw(string(data), path, value)

func SetRawBytesOptions

func SetRawBytesOptions(json []byte, path string, value []byte,
	opts *Options,
) ([]byte, error)

SetRawBytesOptions sets a raw json value for the specified path with options. If working with bytes, this method preferred over SetRawOptions(string(data), path, value, opts)

func SetRawOptions

func SetRawOptions(json, path, value string, opts *Options) (string, error)

SetRawOptions sets a raw json value for the specified path with options. 与 SetOptions 类似, 但 value 直接作为原始 JSON 写入, 可用于已序列化的 JSON 对象.

Types

type Options

type Options struct {
	// Optimistic is a hint that the value likely exists which
	// allows for the sjson to perform a fast-track search and replace.
	Optimistic bool
	// ReplaceInPlace 提示复用输入 JSON 切片, 而非重新分配结果切片.
	// 此时原输入 JSON 可能已被覆盖, 调用方不应继续使用原切片.
	// In the case when the destination slice doesn't have enough free
	// bytes to replace the data in place, a new bytes slice will be
	// created under the hood.
	// The Optimistic flag must be set to true and the input must be a
	// byte slice in order to use this field.
	ReplaceInPlace bool
}

Options represents additional options for the Set and Delete functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL