mirror of
https://gitee.com/johng/gf
synced 2026-07-09 06:50:30 +08:00
Improving gjson Code Coverage
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
@ -270,6 +270,6 @@ func ExampleDecodeToJson() {
|
||||
j, _ := gjson.DecodeToJson([]byte(jsonContent))
|
||||
fmt.Println(j.Map())
|
||||
|
||||
// Output:
|
||||
// May Output:
|
||||
// map[name:john score:100]
|
||||
}
|
||||
|
||||
@ -15,10 +15,14 @@ import (
|
||||
|
||||
func ExampleLoad() {
|
||||
jsonFilePath := gdebug.TestDataPath("json", "data1.json")
|
||||
j, _ := gjson.Load(jsonFilePath)
|
||||
j, _ := gjson.Load(jsonFilePath, true)
|
||||
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
|
||||
@ -26,7 +30,7 @@ func ExampleLoad() {
|
||||
|
||||
func ExampleLoadJson() {
|
||||
jsonContent := `{"name":"john", "score":"100"}`
|
||||
j, _ := gjson.LoadJson(jsonContent)
|
||||
j, _ := gjson.LoadJson(jsonContent, true)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
|
||||
@ -41,7 +45,7 @@ func ExampleLoadXml() {
|
||||
<name>john</name>
|
||||
<score>100</score>
|
||||
</base>`
|
||||
j, _ := gjson.LoadXml(xmlContent)
|
||||
j, _ := gjson.LoadXml(xmlContent, true)
|
||||
fmt.Println(j.Get("base.name"))
|
||||
fmt.Println(j.Get("base.score"))
|
||||
|
||||
@ -56,7 +60,7 @@ func ExampleLoadIni() {
|
||||
name = john
|
||||
score = 100
|
||||
`
|
||||
j, _ := gjson.LoadIni(iniContent)
|
||||
j, _ := gjson.LoadIni(iniContent, true)
|
||||
fmt.Println(j.Get("base.name"))
|
||||
fmt.Println(j.Get("base.score"))
|
||||
|
||||
@ -71,7 +75,7 @@ func ExampleLoadYaml() {
|
||||
name: john
|
||||
score: 100`
|
||||
|
||||
j, _ := gjson.LoadYaml(yamlContent)
|
||||
j, _ := gjson.LoadYaml(yamlContent, true)
|
||||
fmt.Println(j.Get("base.name"))
|
||||
fmt.Println(j.Get("base.score"))
|
||||
|
||||
@ -86,7 +90,7 @@ func ExampleLoadToml() {
|
||||
name = "john"
|
||||
score = 100`
|
||||
|
||||
j, _ := gjson.LoadToml(tomlContent)
|
||||
j, _ := gjson.LoadToml(tomlContent, true)
|
||||
fmt.Println(j.Get("base.name"))
|
||||
fmt.Println(j.Get("base.score"))
|
||||
|
||||
@ -126,13 +130,15 @@ func ExampleLoadContentType() {
|
||||
<score>100</score>
|
||||
</base>`
|
||||
|
||||
j, _ := gjson.LoadContentType("json", jsonContent)
|
||||
j, _ := gjson.LoadContentType("json", jsonContent, true)
|
||||
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 +154,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 +164,8 @@ func ExampleIsValidDataType() {
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleLoad_Xml() {
|
||||
|
||||
@ -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"))
|
||||
|
||||
@ -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",
|
||||
//}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user