Documentation
¶
Overview ¶
leptjson.go - Go语言版JSON库
Index ¶
- func ClearArray(v *Value)
- func ClearObject(v *Value)
- func Copy(dst, src *Value)
- 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 Move(dst, src *Value)
- func PopBackArrayElement(v *Value)
- func RemoveObjectValue(v *Value, index int)
- func ReserveArray(v *Value, capacity int)
- func ReserveObject(v *Value, capacity int)
- func SetArray(v *Value, capacity int)
- func SetBoolean(v *Value, b bool)
- func SetNumber(v *Value, n float64)
- func SetObject(v *Value)
- func SetString(v *Value, s string)
- func ShrinkArray(v *Value)
- func ShrinkObject(v *Value)
- func Swap(lhs, rhs *Value)
- type EnhancedError
- type Member
- type ParseError
- type ParseOptions
- type StringifyError
- type 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 InsertArrayElement(v *Value, index int) *Value
- func PushBackArrayElement(v *Value) *Value
- func SetObjectValue(v *Value, key string) *Value
- type ValueType
Examples ¶
- Parse (Array)
- Parse (Error)
- Parse (Error02)
- Parse (False)
- Parse (NestedArray)
- Parse (NestedObject)
- Parse (Null)
- Parse (Number)
- Parse (Object)
- Parse (String)
- Parse (Stringify)
- Parse (True)
- Stringify (Array)
- Stringify (Boolean)
- Stringify (Complex)
- Stringify (Null)
- Stringify (Number)
- Stringify (Object)
- Stringify (String)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EraseArrayElement ¶
EraseArrayElement 删除数组中从index开始的count个元素
func FindObjectIndex ¶
FindObjectIndex 查找JSON对象中指定键的索引
Types ¶
type EnhancedError ¶
type EnhancedError struct {
Code ParseError // 错误码
Message string // 错误消息
Line int // 行号
Column int // 列号
Context string // 错误发生的上下文
Pointer string // 错误位置指针(比如 "----^")
SourceInput string // 输入源
IsRecoverable bool // 是否可恢复
}
EnhancedError 定义了一个增强的错误类型,包含详细信息
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 Parse ¶
func Parse(v *Value, json string) ParseError
Parse 解析JSON文本(使用默认选项)
Example (Array) ¶
示例 - 解析数组
v := Value{}
Parse(&v, "[1,2,3]")
fmt.Println(v.String())
Output: [1,2,3]
Example (Error) ¶
示例 - 错误处理
v := Value{}
err := Parse(&v, "[1,2,")
fmt.Println(err)
Output: 缺少逗号或方括号
Example (Error02) ¶
v := Value{}
err := Parse(&v, "{\"name\":\"John\",\"age\":")
fmt.Println(err)
Output: 期望一个值
Example (False) ¶
示例 - 解析false值
v := Value{}
Parse(&v, "false")
fmt.Println(v.String())
Output: false
Example (NestedArray) ¶
示例 - 解析嵌套数组
v := Value{}
Parse(&v, "[[1,2],[3,4],5]")
fmt.Println(v.String())
Output: [[1,2],[3,4],5]
Example (NestedObject) ¶
示例 - 解析嵌套对象
v := Value{}
Parse(&v, "{\"person\":{\"name\":\"John\",\"age\":30},\"isActive\":true}")
fmt.Println(v.String())
Output: {"person":{"name":"John","age":30},"isActive":true}
Example (Null) ¶
示例 - 解析null值
v := Value{}
Parse(&v, "null")
fmt.Println(v.String())
Output: null
Example (Number) ¶
示例 - 解析数字
v := Value{}
Parse(&v, "123.456")
fmt.Println(v.String())
Output: 123.456
Example (Object) ¶
示例 - 解析对象
v := Value{}
Parse(&v, "{\"name\":\"John\",\"age\":30}")
fmt.Println(v.String())
Output: {"name":"John","age":30}
Example (String) ¶
示例 - 解析字符串
v := Value{}
Parse(&v, "\"Hello, World!\"")
fmt.Println(v.String())
Output: "Hello, World!"
Example (Stringify) ¶
ExampleParse_stringify 展示解析和字符串化的结合使用
jsonText := `{"a":[1,2],"b":{"c":0,"d":[false,true,null,"Hello"]}}`
var v Value
if err := Parse(&v, jsonText); err == PARSE_OK {
jsonOut, _ := Stringify(&v)
fmt.Println(jsonOut)
}
Output: {"a":[1,2],"b":{"c":0,"d":[false,true,null,"Hello"]}}
Example (True) ¶
示例 - 解析true值
v := Value{}
Parse(&v, "true")
fmt.Println(v.String())
Output: true
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 // 字符串化成功
)
字符串化错误常量
func Stringify ¶
func Stringify(v *Value) (string, StringifyError)
Stringify 将Value转换为JSON字符串
Example (Array) ¶
ExampleStringify_array 展示如何字符串化数组值
v := Value{
Type: ARRAY,
A: []*Value{
{Type: NULL},
{Type: FALSE},
{Type: TRUE},
{Type: NUMBER, N: 123},
{Type: STRING, S: "abc"},
},
}
json, _ := Stringify(&v)
fmt.Println(json)
Output: [null,false,true,123,"abc"]
Example (Boolean) ¶
ExampleStringify_boolean 展示如何字符串化布尔值
v1 := Value{Type: TRUE}
json1, _ := Stringify(&v1)
fmt.Println(json1)
v2 := Value{Type: FALSE}
json2, _ := Stringify(&v2)
fmt.Println(json2)
Output: true false
Example (Complex) ¶
ExampleStringify_complex 展示如何字符串化复杂值
v := Value{
Type: OBJECT,
O: []Member{
{K: "a", V: &Value{
Type: ARRAY,
A: []*Value{
{Type: NUMBER, N: 1},
{Type: NUMBER, N: 2},
},
}},
{K: "b", V: &Value{
Type: OBJECT,
O: []Member{
{K: "c", V: &Value{Type: NUMBER, N: 0}},
{K: "d", V: &Value{
Type: ARRAY,
A: []*Value{
{Type: FALSE},
{Type: TRUE},
{Type: NULL},
{Type: STRING, S: "Hello"},
},
}},
},
}},
},
}
json, _ := Stringify(&v)
fmt.Println(json)
Output: {"a":[1,2],"b":{"c":0,"d":[false,true,null,"Hello"]}}
Example (Null) ¶
ExampleStringify_null 展示如何字符串化null值
v := Value{Type: NULL}
json, _ := Stringify(&v)
fmt.Println(json)
Output: null
Example (Number) ¶
ExampleStringify_number 展示如何字符串化数字值
v := Value{Type: NUMBER, N: 123.456}
json, _ := Stringify(&v)
fmt.Println(json)
Output: 123.456
Example (Object) ¶
ExampleStringify_object 展示如何字符串化对象值
v := Value{
Type: OBJECT,
O: []Member{
{K: "null", V: &Value{Type: NULL}},
{K: "false", V: &Value{Type: FALSE}},
{K: "true", V: &Value{Type: TRUE}},
{K: "number", V: &Value{Type: NUMBER, N: 123}},
{K: "string", V: &Value{Type: STRING, S: "abc"}},
},
}
json, _ := Stringify(&v)
fmt.Println(json)
Output: {"null":null,"false":false,"true":true,"number":123,"string":"abc"}
Example (String) ¶
ExampleStringify_string 展示如何字符串化字符串值
v := Value{Type: STRING, S: "Hello, World!"}
json, _ := Stringify(&v)
fmt.Println(json)
Output: "Hello, World!"
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 FindObjectKey ¶
FindObjectKey 根据键名在对象中查找对应值,如果找到返回值和true,否则返回nil和false
func GetObjectValueByKey ¶
GetObjectValueByKey 根据键获取JSON对象的值
func InsertArrayElement ¶
InsertArrayElement 在指定位置插入元素,并返回该元素
func PushBackArrayElement ¶
PushBackArrayElement 在数组末尾添加一个新元素,并返回该元素
func SetObjectValue ¶
SetObjectValue 设置对象的键值对,如果键已存在则返回其值指针,否则添加新的键值对并返回新值指针