Documentation
¶
Overview ¶
cycle_detection.go - 循环引用检测实现
json_patch.go - JSON Patch 实现 (RFC 6902)
json_path.go - JSON Path 实现
json_pointer.go - JSON指针实现 (RFC6901)
json_schema.go - JSON Schema 验证实现(基于部分 JSON Schema Draft 7)
leptjson.go - Go语言版JSON库
Index ¶
- func BuildJSONPointer(segments ...interface{}) (string, error)
- func ClearArray(v *Value)
- func ClearObject(v *Value)
- func Copy(dst, src *Value)
- func CopySafe(dst, src *Value) error
- func CopySafeWithReplacement(dst, src *Value)
- func CustomCopySafeWithReplacement(dst, src *Value, replacer CircularReplacer)
- func Equal(lhs, rhs *Value) bool
- func EraseArrayElement(v *Value, index, count int)
- func FindObjectIndex(v *Value, key string) int
- func Free(v *Value)
- func GetArrayCapacity(v *Value) int
- func GetArraySize(v *Value) int
- func GetBoolean(v *Value) bool
- func GetErrorMessage(code ParseError) string
- func GetNumber(v *Value) float64
- func GetObjectCapacity(v *Value) int
- func GetObjectKey(v *Value, index int) string
- func GetObjectSize(v *Value) int
- func GetString(v *Value) string
- func HasCycle(v *Value) bool
- func Marshal(v interface{}) (string, error)
- func Move(dst, src *Value)
- func ParseJSONPointer(pointer string) (*JSONPointer, JSONPointerError)
- func PopBackArrayElement(v *Value)
- func RemoveObjectValue(v *Value, index int)
- func RemoveObjectValueByKey(v *Value, key string) bool
- func RemoveValueByPointer(v *Value, pointerStr string) error
- func ReserveArray(v *Value, capacity int)
- func ReserveObject(v *Value, capacity int)
- func SafeCopyWithReplacer(dst, src *Value, replacer CircularReplacer)
- func SetArray(v *Value, capacity int)
- func SetBoolean(v *Value, b bool)
- func SetNull(v *Value)
- func SetNumber(v *Value, n float64)
- func SetObject(v *Value)
- func SetString(v *Value, s string)
- func SetValueByPointer(v *Value, pointerStr string, value *Value) error
- func ShrinkArray(v *Value)
- func ShrinkObject(v *Value)
- func Swap(lhs, rhs *Value)
- type CircularReplacer
- type CycleError
- type EnhancedError
- type JSONMergePatch
- type JSONPatch
- type JSONPath
- type JSONPathError
- type JSONPointer
- func (p *JSONPointer) Get(root *Value) (*Value, JSONPointerError)
- func (p *JSONPointer) Insert(root *Value, value *Value) JSONPointerError
- func (p *JSONPointer) Remove(root *Value) JSONPointerError
- func (p *JSONPointer) Replace(root *Value, value *Value) JSONPointerError
- func (p *JSONPointer) String() string
- type JSONPointerError
- type JSONSchema
- type Member
- type ParseError
- type ParseOptions
- type PatchError
- type PatchOperation
- type SchemaValidationError
- type SchemaValidationResult
- type SliceInfo
- type StringifyError
- type Token
- type TokenType
- type Value
- func DefaultCircularReplacer(path []string) *Value
- func FindObjectKey(v *Value, key string) (*Value, bool)
- func GetArrayElement(v *Value, index int) *Value
- func GetObjectValue(v *Value, index int) *Value
- func GetObjectValueByKey(v *Value, key string) *Value
- func GetValueByPointer(v *Value, pointerStr string) (*Value, error)
- func InsertArrayElement(v *Value, index int) *Value
- func PushBackArrayElement(v *Value) *Value
- func QueryOneString(doc *Value, path string) (*Value, error)
- func QueryString(doc *Value, path string) ([]*Value, error)
- func SetObjectValue(v *Value, key string) *Value
- type ValueType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildJSONPointer ¶
BuildJSONPointer 创建一个JSON指针字符串
func CopySafeWithReplacement ¶
func CopySafeWithReplacement(dst, src *Value)
CopySafeWithReplacement 带替换的安全复制
func CustomCopySafeWithReplacement ¶
func CustomCopySafeWithReplacement(dst, src *Value, replacer CircularReplacer)
CustomCopySafeWithReplacement 使用自定义替换器的安全复制
func EraseArrayElement ¶
EraseArrayElement 删除数组中从index开始的count个元素
func FindObjectIndex ¶
FindObjectIndex 查找JSON对象中指定键的索引
func ParseJSONPointer ¶
func ParseJSONPointer(pointer string) (*JSONPointer, JSONPointerError)
ParseJSONPointer 解析JSON指针字符串 例如: "/foo/0/bar" => ["foo", "0", "bar"]
func RemoveObjectValueByKey ¶
RemoveObjectValueByKey 是一个辅助函数,需要添加到 leptjson.go 或在此处实现 它根据键来查找并删除对象成员
func RemoveValueByPointer ¶
RemoveValueByPointer 使用JSON指针删除值
func SafeCopyWithReplacer ¶
func SafeCopyWithReplacer(dst, src *Value, replacer CircularReplacer)
SafeCopyWithReplacer 带替换器的安全复制,处理循环引用
func SetValueByPointer ¶
SetValueByPointer 使用JSON指针设置值
Types ¶
type CircularReplacer ¶
CircularReplacer 定义了在发现循环引用时的替换函数类型
type CycleError ¶
type CycleError int
CycleError 表示循环引用错误
const ( CYCLE_OK CycleError = iota CYCLE_DETECTED )
循环引用错误常量
type EnhancedError ¶
type EnhancedError struct {
Code ParseError // 错误码
Message string // 错误消息
Line int // 行号
Column int // 列号
Context string // 错误发生的上下文
Pointer string // 错误位置指针(比如 "----^")
SourceInput string // 输入源
IsRecoverable bool // 是否可恢复
}
EnhancedError 定义了一个增强的错误类型,包含详细信息
type JSONMergePatch ¶
type JSONMergePatch struct {
Document *Value
}
JSONMergePatch 表示一个 JSON Merge Patch 文档
func CreateMergePatch ¶
func CreateMergePatch(source, target *Value) (*JSONMergePatch, error)
CreateMergePatch 创建从源文档到目标文档的 JSON Merge Patch (*Value) 返回一个表示变更的 JSONMergePatch 对象
func NewJSONMergePatch ¶
func NewJSONMergePatch(patchDoc *Value) (*JSONMergePatch, error)
NewJSONMergePatch 从 *Value 创建 JSON Merge Patch 对象 注意:Merge Patch 本身必须是有效的 JSON,由调用方保证
func (*JSONMergePatch) Apply ¶
func (p *JSONMergePatch) Apply(target *Value) (*Value, error)
Apply 将该 Merge Patch 应用到目标文档 (*Value) 返回修改后的新文档 (*Value),不修改原始文档或 patch 本身。
func (*JSONMergePatch) String ¶
func (p *JSONMergePatch) String() (string, error)
String 返回 JSON Merge Patch 的字符串表示
type JSONPatch ¶
type JSONPatch struct {
Operations []PatchOperation // 操作列表
}
JSONPatch 表示一个 JSON Patch 文档,包含多个操作
func CreatePatch ¶
CreatePatch 生成从 source 到 target 的 JSON Patch
func NewJSONPatch ¶
NewJSONPatch 从 JSON 值中创建 JSON Patch 对象
func NewJSONPatchFromString ¶
NewJSONPatchFromString 从 JSON 字符串创建 JSON Patch 对象
type JSONPath ¶
JSONPath 表示一个解析后的 JSON Path 表达式
func NewJSONPath ¶
NewJSONPath 解析 JSON Path 表达式并创建一个 JSONPath 对象
type JSONPathError ¶
type JSONPathError struct {
Path string // JSON Path 表达式
Message string // 错误消息
Index int // 错误发生的位置
}
JSONPathError 表示解析或执行 JSON Path 时的错误
type JSONPointer ¶
type JSONPointer struct {
// contains filtered or unexported fields
}
JSONPointer 表示一个JSON指针(RFC6901)
func GetJSONPointer ¶
func GetJSONPointer(segments ...interface{}) (*JSONPointer, error)
GetJSONPointer 创建一个指向指定路径的JSONPointer 例如: NewJSONPointer("foo", 0, "bar") => "/foo/0/bar"
func (*JSONPointer) Get ¶
func (p *JSONPointer) Get(root *Value) (*Value, JSONPointerError)
Get 根据JSON指针获取值
func (*JSONPointer) Insert ¶
func (p *JSONPointer) Insert(root *Value, value *Value) JSONPointerError
Insert 根据JSON指针在数组中插入值或在对象中添加/替换值 对于数组,支持索引插入和末尾追加 ("-")
func (*JSONPointer) Remove ¶
func (p *JSONPointer) Remove(root *Value) JSONPointerError
Remove 根据JSON指针删除值
func (*JSONPointer) Replace ¶
func (p *JSONPointer) Replace(root *Value, value *Value) JSONPointerError
Replace 根据JSON指针替换现有值或添加对象成员 注意:对于数组,此方法执行替换,不执行插入。
type JSONPointerError ¶
type JSONPointerError int
JSONPointerError 表示JSON指针相关错误
const ( POINTER_OK JSONPointerError = iota POINTER_INVALID_FORMAT POINTER_INDEX_OUT_OF_RANGE POINTER_KEY_NOT_FOUND POINTER_INVALID_TARGET )
JSON指针错误常量
type JSONSchema ¶
type JSONSchema struct {
Schema *Value // 存储 JSON Schema 的 Value 对象
}
JSONSchema 表示一个 JSON Schema 对象
func NewJSONSchema ¶
func NewJSONSchema(schemaJSON string) (*JSONSchema, error)
NewJSONSchema 创建一个新的 JSON Schema
func NewJSONSchemaFromValue ¶
func NewJSONSchemaFromValue(schema *Value) (*JSONSchema, error)
NewJSONSchemaFromValue 从 Value 对象创建 JSON Schema
func (*JSONSchema) Validate ¶
func (js *JSONSchema) Validate(data *Value) *SchemaValidationResult
Validate 根据 Schema 验证 JSON 数据
type ParseError ¶
type ParseError int
ParseError 表示解析错误
const ( PARSE_OK ParseError = iota // 解析成功 PARSE_EXPECT_VALUE // 期望一个值 PARSE_INVALID_VALUE // 无效的值 PARSE_ROOT_NOT_SINGULAR // 根节点不唯一 PARSE_NUMBER_TOO_BIG // 数字太大 PARSE_MISS_QUOTATION_MARK // 缺少引号 PARSE_INVALID_STRING_ESCAPE // 无效的转义序列 PARSE_INVALID_STRING_CHAR // 无效的字符 PARSE_INVALID_UNICODE_HEX // 无效的Unicode十六进制 PARSE_INVALID_UNICODE_SURROGATE // 无效的Unicode代理对 PARSE_MISS_COMMA_OR_SQUARE_BRACKET // 缺少逗号或方括号 PARSE_MISS_KEY // 缺少键 PARSE_MISS_COLON // 缺少冒号 PARSE_MISS_COMMA_OR_CURLY_BRACKET // 缺少逗号或花括号 PARSE_MAX_DEPTH_EXCEEDED // 超过最大嵌套深度 PARSE_COMMENT_NOT_CLOSED // 注释未闭合 )
解析错误常量
func ParseWithOptions ¶
func ParseWithOptions(v *Value, json string, options ParseOptions) ParseError
ParseWithOptions 使用自定义选项解析JSON文本
解析步骤: 1. 跳过前导空白字符 2. 解析JSON值 3. 跳过后续空白字符 4. 检查是否还有额外内容(这将导致PARSE_ROOT_NOT_SINGULAR错误)
type ParseOptions ¶
type ParseOptions struct {
MaxDepth int // 最大嵌套深度
AllowComments bool // 是否允许注释
AllowTrailing bool // 是否允许尾随逗号
StrictMode bool // 严格模式(更严格的检查)
RecoverFromErrors bool // 是否从非致命错误恢复
}
ParseOptions 定义解析选项
type PatchError ¶
type PatchError struct {
Operation string // 发生错误的操作类型
Path string // 发生错误的路径
Message string // 错误消息
}
PatchError 表示 JSON Patch 操作中的错误
type PatchOperation ¶
type PatchOperation struct {
Op string // 操作类型: add, remove, replace, move, copy, test
Path string // 操作的目标路径 (JSON Pointer)
From string // 源路径 (用于 move 和 copy 操作)
Value *Value // 值 (用于 add, replace 和 test 操作)
}
PatchOperation 表示 JSON Patch 中的单个操作
type SchemaValidationError ¶
SchemaValidationError 表示 JSON Schema 验证错误
type SchemaValidationResult ¶
type SchemaValidationResult struct {
Valid bool // 是否验证通过
Errors []SchemaValidationError // 验证错误列表
}
SchemaValidationResult 存储验证结果
func (*SchemaValidationResult) AddError ¶
func (r *SchemaValidationResult) AddError(path, message string)
AddError 添加验证错误
type StringifyError ¶
type StringifyError int
StringifyError 表示字符串化错误
const (
STRINGIFY_OK StringifyError = iota // 字符串化成功
)
字符串化错误常量
type Value ¶
type Value struct {
Type ValueType `json:"type"` // 值类型
N float64 `json:"n"` // 数字值(当Type为NUMBER时有效)
S string `json:"s"` // 字符串值(当Type为STRING时有效)
A []*Value `json:"a"` // 数组值(当Type为ARRAY时有效)
O []Member `json:"o"` // 对象值(当Type为OBJECT时有效)
}
Value 表示一个JSON值
func DefaultCircularReplacer ¶
DefaultCircularReplacer 默认循环引用替换器
func FindObjectKey ¶
FindObjectKey 根据键名在对象中查找对应值,如果找到返回值和true,否则返回nil和false
func GetObjectValueByKey ¶
GetObjectValueByKey 根据键获取JSON对象的值
func GetValueByPointer ¶
GetValueByPointer 使用JSON指针获取值
func InsertArrayElement ¶
InsertArrayElement 在指定位置插入元素,并返回该元素
func PushBackArrayElement ¶
PushBackArrayElement 在数组末尾添加一个新元素,并返回该元素
func QueryOneString ¶
QueryOneString 是一个便捷函数,返回匹配路径的第一个值
func QueryString ¶
QueryString 是一个便捷函数,直接使用路径表达式查询 JSON 值
func SetObjectValue ¶
SetObjectValue 设置对象的键值对,如果键已存在则返回其值指针,否则添加新的键值对并返回新值指针