Documentation
¶
Overview ¶
cycle_detection.go - 循环引用检测实现
json_pointer.go - JSON指针实现 (RFC6901)
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 Move(dst, src *Value)
- func ParseJSONPointer(pointer string) (*JSONPointer, JSONPointerError)
- func PopBackArrayElement(v *Value)
- func RemoveObjectValue(v *Value, index int)
- 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 JSONPointer
- type JSONPointerError
- type Member
- type ParseError
- type ParseOptions
- type StringifyError
- 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 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 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 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) Remove ¶
func (p *JSONPointer) Remove(root *Value) JSONPointerError
Remove 根据JSON指针删除值
func (*JSONPointer) Set ¶
func (p *JSONPointer) Set(root *Value, value *Value) JSONPointerError
Set 根据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 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 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 SetObjectValue ¶
SetObjectValue 设置对象的键值对,如果键已存在则返回其值指针,否则添加新的键值对并返回新值指针