This commit is contained in:
John Guo
2022-02-28 17:49:16 +08:00
8 changed files with 159 additions and 31 deletions

View File

@ -60,12 +60,7 @@ func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
return nil
}
var result *interface{}
if j.vc {
result = j.getPointerByPattern(pattern)
} else {
result = j.getPointerByPatternWithoutViolenceCheck(pattern)
}
result := j.getPointerByPattern(pattern)
if result != nil {
return gvar.New(*result)
}

View File

@ -270,6 +270,6 @@ func ExampleDecodeToJson() {
j, _ := gjson.DecodeToJson([]byte(jsonContent))
fmt.Println(j.Map())
// Output:
// May Output:
// map[name:john score:100]
}

View File

@ -19,6 +19,10 @@ func ExampleLoad() {
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
notExistFilePath := gdebug.TestDataPath("json", "data2.json")
j2, _ := gjson.Load(notExistFilePath)
fmt.Println(j2.Get("name"))
// Output:
// john
// 100
@ -97,25 +101,51 @@ func ExampleLoadToml() {
func ExampleLoadContent() {
jsonContent := `{"name":"john", "score":"100"}`
j, _ := gjson.LoadContent(jsonContent)
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
// Output:
// john
// 100
}
func ExampleLoadContent_UTF8BOM() {
jsonContent := `{"name":"john", "score":"100"}`
content := make([]byte, 3, len(jsonContent)+3)
content[0] = 0xEF
content[1] = 0xBB
content[2] = 0xBF
content = append(content, jsonContent...)
j, _ := gjson.LoadContent(content)
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
// Output:
// john
// 100
}
func ExampleLoadContent_Xml() {
xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
<base>
<name>john</name>
<score>100</score>
</base>`
j, _ := gjson.LoadContent(jsonContent)
x, _ := gjson.LoadContent(xmlContent)
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
fmt.Println(x.Get("base.name"))
fmt.Println(x.Get("base.score"))
// Output:
// john
// 100
// john
// 100
}
func ExampleLoadContentType() {
@ -128,11 +158,13 @@ func ExampleLoadContentType() {
j, _ := gjson.LoadContentType("json", jsonContent)
x, _ := gjson.LoadContentType("xml", xmlContent)
j1, _ := gjson.LoadContentType("json", "")
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
fmt.Println(x.Get("base.name"))
fmt.Println(x.Get("base.score"))
fmt.Println(j1.Get(""))
// Output:
// john
@ -148,6 +180,8 @@ func ExampleIsValidDataType() {
fmt.Println(gjson.IsValidDataType("mp4"))
fmt.Println(gjson.IsValidDataType("xsl"))
fmt.Println(gjson.IsValidDataType("txt"))
fmt.Println(gjson.IsValidDataType(""))
fmt.Println(gjson.IsValidDataType(".json"))
// Output:
// true
@ -156,6 +190,8 @@ func ExampleIsValidDataType() {
// false
// false
// false
// false
// true
}
func ExampleLoad_Xml() {

View File

@ -34,7 +34,7 @@ func ExampleNewWithTag() {
Score: 100,
Title: "engineer",
}
j := gjson.NewWithTag(me, "tag")
j := gjson.NewWithTag(me, "tag", true)
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
fmt.Println(j.Get("Title"))
@ -70,6 +70,26 @@ func ExampleNewWithOptions() {
// engineer
}
func ExampleNewWithOptions_UTF8BOM() {
jsonContent := `{"name":"john", "score":"100"}`
content := make([]byte, 3, len(jsonContent)+3)
content[0] = 0xEF
content[1] = 0xBB
content[2] = 0xBF
content = append(content, jsonContent...)
j := gjson.NewWithOptions(content, gjson.Options{
Tags: "tag",
})
fmt.Println(j.Get("name"))
fmt.Println(j.Get("score"))
// Output:
// john
// 100
}
func ExampleNew_Xml() {
jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
j := gjson.New(jsonContent)

View File

@ -590,61 +590,52 @@ func ExampleJson_ToIni() {
func ExampleJson_ToIniString() {
type BaseInfo struct {
Name string
Age int
}
info := BaseInfo{
Name: "John",
Age: 18,
}
j := gjson.New(info)
IniStr, _ := j.ToIniString()
fmt.Println(string(IniStr))
// May Output:
// Output:
//Name=John
//Age=18
}
func ExampleJson_MustToIni() {
type BaseInfo struct {
Name string
Age int
}
info := BaseInfo{
Name: "John",
Age: 18,
}
j := gjson.New(info)
IniBytes := j.MustToIni()
fmt.Println(string(IniBytes))
// May Output:
// Output:
//Name=John
//Age=18
}
func ExampleJson_MustToIniString() {
type BaseInfo struct {
Name string
Age int
}
info := BaseInfo{
Name: "John",
Age: 18,
}
j := gjson.New(info)
IniStr := j.MustToIniString()
fmt.Println(string(IniStr))
// May Output:
// Output:
//Name=John
//Age=18
}
func ExampleJson_MarshalJSON() {
@ -758,8 +749,12 @@ func ExampleJson_Interface() {
j := gjson.New(info)
fmt.Println(j.Interface())
var nilJ *gjson.Json = nil
fmt.Println(nilJ.Interface())
// Output:
// map[Age:18 Name:John]
// <nil>
}
func ExampleJson_Var() {
@ -807,11 +802,16 @@ func ExampleJson_Get() {
}`
j, _ := gjson.LoadContent(data)
fmt.Println(j.Get("."))
fmt.Println(j.Get("users"))
fmt.Println(j.Get("users.count"))
fmt.Println(j.Get("users.array"))
var nilJ *gjson.Json = nil
fmt.Println(nilJ.Get("."))
// Output:
// {"users":{"array":["John","Ming"],"count":1}}
// {"array":["John","Ming"],"count":1}
// 1
// ["John","Ming"]
@ -893,10 +893,11 @@ func ExampleJson_Set() {
j := gjson.New(info)
j.Set("Addr", "ChengDu")
j.Set("Friends.0", "Tom")
fmt.Println(j.Var().String())
// Output:
// {"Addr":"ChengDu","Age":18,"Name":"John"}
// {"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"}
}
func ExampleJson_MustSet() {
@ -1091,7 +1092,7 @@ func ExampleJson_Scan() {
fmt.Println(info)
// Output:
// May Output:
// {john 18}
}
@ -1099,12 +1100,11 @@ func ExampleJson_Dump() {
data := `{"name":"john","age":"18"}`
j, _ := gjson.LoadContent(data)
j.Dump()
// May Output:
//{
// "age": "18",
// "name": "john",
// "age": "18",
//}
}

View File

@ -67,6 +67,11 @@ func Test_MapAttributeConvert(t *testing.T) {
t.Assert(tx.Title, g.Map{
"l1": "标签1", "l2": "标签2",
})
j.Dump()
var nilJ *gjson.Json = nil
nilJ.Dump()
})
gtest.C(t, func(t *gtest.T) {

View File

@ -28,11 +28,17 @@ func Test_Load_JSON1(t *testing.T) {
t.Assert(j.Get("a.1").Int(), 2)
})
// JSON
gtest.C(t, func(t *gtest.T) {
errData := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
_, err := gjson.LoadContentType("json", errData, true)
t.AssertNE(err, nil)
})
// JSON
gtest.C(t, func(t *gtest.T) {
path := "test.json"
gfile.PutBytes(path, data)
defer gfile.Remove(path)
j, err := gjson.Load(path)
j, err := gjson.Load(path, true)
t.Assert(err, nil)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
@ -68,6 +74,22 @@ func Test_Load_XML(t *testing.T) {
t.Assert(j.Get("doc.a.1").Int(), 2)
})
// XML
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadXml(data, true)
t.Assert(err, nil)
t.Assert(j.Get("doc.n").String(), "123456789")
t.Assert(j.Get("doc.m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("doc.m.k").String(), "v")
t.Assert(j.Get("doc.a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("doc.a.1").Int(), 2)
})
// XML
gtest.C(t, func(t *gtest.T) {
errData := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n><doc>`)
_, err := gjson.LoadContentType("xml", errData, true)
t.AssertNE(err, nil)
})
// XML
gtest.C(t, func(t *gtest.T) {
path := "test.xml"
gfile.PutBytes(path, data)
@ -121,6 +143,16 @@ m:
t.Assert(j.Get("a.1").Int(), 2)
})
// YAML
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadYaml(data, true)
t.Assert(err, nil)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
})
// YAML
gtest.C(t, func(t *gtest.T) {
path := "test.yaml"
gfile.PutBytes(path, data)
@ -142,6 +174,11 @@ func Test_Load_YAML2(t *testing.T) {
t.Assert(err, nil)
t.Assert(j.Get("i"), "123456789")
})
gtest.C(t, func(t *gtest.T) {
errData := []byte("i # 123456789")
_, err := gjson.LoadContentType("yaml", errData, true)
t.AssertNE(err, nil)
})
}
func Test_Load_TOML1(t *testing.T) {
@ -163,6 +200,16 @@ n = 123456789
t.Assert(j.Get("a.1").Int(), 2)
})
// TOML
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadToml(data, true)
t.Assert(err, nil)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("a.1").Int(), 2)
})
// TOML
gtest.C(t, func(t *gtest.T) {
path := "test.toml"
gfile.PutBytes(path, data)
@ -184,6 +231,11 @@ func Test_Load_TOML2(t *testing.T) {
t.Assert(err, nil)
t.Assert(j.Get("i"), "123456789")
})
gtest.C(t, func(t *gtest.T) {
errData := []byte("i : 123456789")
_, err := gjson.LoadContentType("toml", errData, true)
t.AssertNE(err, nil)
})
}
func Test_Load_Basic(t *testing.T) {
@ -245,6 +297,26 @@ enable=true
gtest.Fatal(err)
}
})
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadIni(data, true)
if err != nil {
gtest.Fatal(err)
}
t.Assert(j.Get("addr.ip").String(), "127.0.0.1")
t.Assert(j.Get("addr.port").String(), "9001")
t.Assert(j.Get("addr.enable").String(), "true")
t.Assert(j.Get("DBINFO.type").String(), "mysql")
t.Assert(j.Get("DBINFO.user").String(), "root")
t.Assert(j.Get("DBINFO.password").String(), "password")
})
gtest.C(t, func(t *gtest.T) {
errData := []byte("i : 123456789")
_, err := gjson.LoadContentType("ini", errData, true)
t.AssertNE(err, nil)
})
}
func Test_Load_YamlWithV3(t *testing.T) {

View File

@ -197,7 +197,7 @@ func Test_Struct1(t *testing.T) {
}]
}`
data := new(UserCollectionAddReq)
j, err := gjson.LoadJson(jsonContent)
j, err := gjson.LoadJson(jsonContent, true)
t.Assert(err, nil)
err = j.Scan(data)
t.Assert(err, nil)