From 8cb6086f736961c82f7361f65782114d2292d9c8 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 12 Feb 2022 17:27:32 +0800 Subject: [PATCH 1/8] Implemented gjson Example 1. New 2.NewWithTag 3.NewWithOptions --- encoding/gjson/gjson_z_example_new_test.go | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index 644e567c4..a077f280c 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -72,3 +72,61 @@ func Example_newFromStructWithTag() { // 100 // engineer } + +func ExampleNew() { + jsonContent := `{"name":"john", "score":"100"}` + j := gjson.New(jsonContent) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleNewWithTag() { + type Me struct { + Name string `tag:"name"` + Score int `tag:"score"` + Title string + } + me := Me{ + Name: "john", + Score: 100, + Title: "engineer", + } + j := gjson.NewWithTag(me, "tag") + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + fmt.Println(j.Get("Title")) + + // Output: + // john + // 100 + // engineer +} + +func ExampleNewWithOptions() { + type Me struct { + Name string `tag:"name"` + Score int `tag:"score"` + Title string + } + me := Me{ + Name: "john", + Score: 100, + Title: "engineer", + } + + j := gjson.NewWithOptions(me, gjson.Options{ + Tags: "tag", + }) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + fmt.Println(j.Get("Title")) + + // Output: + // john + // 100 + // engineer +} From 4fc27f6509c91292134e11a5c79a895b59489d6a Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 15 Feb 2022 22:08:51 +0800 Subject: [PATCH 2/8] Implemented gjson Example 1. ExampleLoad 2.ExampleLoadJson 3.ExampleLoadXml 4.ExampleLoadIni 5.ExampleLoadYaml 6.ExampleLoadToml 7.ExampleLoadContent 8.ExampleLoadContentType 9.ExampleIsValidDataType --- encoding/gjson/gjson_z_example_load_test.go | 150 ++++++++++++++++++-- encoding/gjson/gjson_z_example_new_test.go | 89 ++++-------- encoding/gjson/testdata/toml/data1.toml | 3 + encoding/gjson/testdata/yaml/data1.yaml | 3 + 4 files changed, 173 insertions(+), 72 deletions(-) create mode 100644 encoding/gjson/testdata/toml/data1.toml create mode 100644 encoding/gjson/testdata/yaml/data1.yaml diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 84deb1fee..fe5e48fe7 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -13,11 +13,149 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func Example_loadJson() { +func ExampleLoad() { jsonFilePath := gdebug.TestDataPath("json", "data1.json") j, _ := gjson.Load(jsonFilePath) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleLoadJson() { + jsonContent := `{"name":"john", "score":"100"}` + j, _ := gjson.LoadJson(jsonContent) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleLoadXml() { + xmlContent := ` + + john + 100 + ` + j, _ := gjson.LoadXml(xmlContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadIni() { + iniContent := ` + [base] + name = john + score = 100 + ` + j, _ := gjson.LoadIni(iniContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadYaml() { + yamlContent := + `base: + name: john + score: 100` + + j, _ := gjson.LoadYaml(yamlContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadToml() { + tomlContent := + `[base] + name = "john" + score = 100` + + j, _ := gjson.LoadToml(tomlContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadContent() { + jsonContent := `{"name":"john", "score":"100"}` + xmlContent := ` + + john + 100 + ` + + 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() { + jsonContent := `{"name":"john", "score":"100"}` + xmlContent := ` + + john + 100 + ` + + j, _ := gjson.LoadContentType("json", jsonContent) + x, _ := gjson.LoadContentType("xml", 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 ExampleIsValidDataType() { + fmt.Println(gjson.IsValidDataType("json")) + fmt.Println(gjson.IsValidDataType("yml")) + fmt.Println(gjson.IsValidDataType("js")) + fmt.Println(gjson.IsValidDataType("mp4")) + fmt.Println(gjson.IsValidDataType("xsl")) + fmt.Println(gjson.IsValidDataType("txt")) + + // Output: + // true + // true + // true + // false + // false + // false } func Example_loadXml() { @@ -26,13 +164,3 @@ func Example_loadXml() { fmt.Println(j.Get("doc.name")) fmt.Println(j.Get("doc.score")) } - -func Example_loadContent() { - jsonContent := `{"name":"john", "score":"100"}` - j, _ := gjson.LoadContent(jsonContent) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - // Output: - // john - // 100 -} diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index a077f280c..242ea95ab 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -12,67 +12,6 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func Example_newFromJson() { - jsonContent := `{"name":"john", "score":"100"}` - j := gjson.New(jsonContent) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - // Output: - // john - // 100 -} - -func Example_newFromXml() { - jsonContent := `john100` - j := gjson.New(jsonContent) - // Note that there's root node in the XML content. - fmt.Println(j.Get("doc.name")) - fmt.Println(j.Get("doc.score")) - // Output: - // john - // 100 -} - -func Example_newFromStruct() { - type Me struct { - Name string `json:"name"` - Score int `json:"score"` - } - me := Me{ - Name: "john", - Score: 100, - } - j := gjson.New(me) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - // Output: - // john - // 100 -} - -func Example_newFromStructWithTag() { - type Me struct { - Name string `tag:"name"` - Score int `tag:"score"` - Title string - } - me := Me{ - Name: "john", - Score: 100, - Title: "engineer", - } - // The parameter `tags` specifies custom priority tags for struct conversion to map, - // multiple tags joined with char ','. - j := gjson.NewWithTag(me, "tag") - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - fmt.Println(j.Get("Title")) - // Output: - // john - // 100 - // engineer -} - func ExampleNew() { jsonContent := `{"name":"john", "score":"100"}` j := gjson.New(jsonContent) @@ -130,3 +69,31 @@ func ExampleNewWithOptions() { // 100 // engineer } + +func Example_newFromXml() { + jsonContent := `john100` + j := gjson.New(jsonContent) + // Note that there's root node in the XML content. + fmt.Println(j.Get("doc.name")) + fmt.Println(j.Get("doc.score")) + // Output: + // john + // 100 +} + +func Example_newFromStruct() { + type Me struct { + Name string `json:"name"` + Score int `json:"score"` + } + me := Me{ + Name: "john", + Score: 100, + } + j := gjson.New(me) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + // Output: + // john + // 100 +} diff --git a/encoding/gjson/testdata/toml/data1.toml b/encoding/gjson/testdata/toml/data1.toml new file mode 100644 index 000000000..21e3889c6 --- /dev/null +++ b/encoding/gjson/testdata/toml/data1.toml @@ -0,0 +1,3 @@ +[base] + name = "john" + score = 100 \ No newline at end of file diff --git a/encoding/gjson/testdata/yaml/data1.yaml b/encoding/gjson/testdata/yaml/data1.yaml new file mode 100644 index 000000000..5a428f55d --- /dev/null +++ b/encoding/gjson/testdata/yaml/data1.yaml @@ -0,0 +1,3 @@ +base: + name: john + score: 100 \ No newline at end of file From 33367fd3eef4eb9b18aaa4cecd1a0a072a373916 Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 15 Feb 2022 23:02:48 +0800 Subject: [PATCH 3/8] Implemented gjson Example 1.ExampleValid 2.ExampleMarshal 3.ExampleMarshalIndent --- .../gjson/gjson_z_example_conversion_test.go | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/encoding/gjson/gjson_z_example_conversion_test.go b/encoding/gjson/gjson_z_example_conversion_test.go index 4fbfa05a6..55e331425 100644 --- a/encoding/gjson/gjson_z_example_conversion_test.go +++ b/encoding/gjson/gjson_z_example_conversion_test.go @@ -8,7 +8,6 @@ package gjson_test import ( "fmt" - "github.com/gogf/gf/v2/encoding/gjson" ) @@ -111,3 +110,62 @@ func Example_conversionToStruct() { // Output: // &{Count:1 Array:[John Ming]} } + +func ExampleValid() { + data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) + data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) + fmt.Println(gjson.Valid(data1)) + fmt.Println(gjson.Valid(data2)) + + // Output: + // true + // false +} + +func ExampleMarshal() { + data := map[string]interface{}{ + "name": "john", + "score": 100, + } + + jsonData, _ := gjson.Marshal(data) + fmt.Println(string(jsonData)) + + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "Guo Qiang", + Age: 18, + } + + infoData, _ := gjson.Marshal(info) + fmt.Println(string(infoData)) + + // Output: + // {"name":"john","score":100} + // {"Name":"Guo Qiang","Age":18} +} + +func ExampleMarshalIndent() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + infoData, _ := gjson.MarshalIndent(info, "", "\t") + fmt.Println(string(infoData)) + + // Output: + // { + // "Name": "John", + // "Age": 18 + // } +} From f8f13bd90599be8231b9fc74d0c31e7e035147e8 Mon Sep 17 00:00:00 2001 From: huangqian Date: Wed, 16 Feb 2022 22:38:25 +0800 Subject: [PATCH 4/8] Implemented gjson Example 1.Unmarshal 2.Encode 3.MustEncode 4.EncodeString 5.MustEncodeString 6.Decode 7.DecodeTo 8.DecodeToJson --- .../gjson/gjson_z_example_conversion_test.go | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/encoding/gjson/gjson_z_example_conversion_test.go b/encoding/gjson/gjson_z_example_conversion_test.go index 55e331425..9f344fcbd 100644 --- a/encoding/gjson/gjson_z_example_conversion_test.go +++ b/encoding/gjson/gjson_z_example_conversion_test.go @@ -169,3 +169,107 @@ func ExampleMarshalIndent() { // "Age": 18 // } } + +func ExampleUnmarshal() { + type BaseInfo struct { + Name string + Score int + } + + var info BaseInfo + + jsonContent := "{\"name\":\"john\",\"score\":100}" + gjson.Unmarshal([]byte(jsonContent), &info) + fmt.Printf("%+v", info) + + // Output: + // {Name:john Score:100} +} + +func ExampleMustEncode() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + infoData := gjson.MustEncode(info) + fmt.Println(string(infoData)) + + // Output: + // {"Name":"John","Age":18} +} + +func ExampleEncodeString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + infoData, _ := gjson.EncodeString(info) + fmt.Println(infoData) + + // Output: + // {"Name":"John","Age":18} +} + +func ExampleMustEncodeString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + infoData := gjson.MustEncodeString(info) + fmt.Println(infoData) + + // Output: + // {"Name":"John","Age":18} +} + +func ExampleDecode() { + jsonContent := `{"name":"john","score":100}` + info, _ := gjson.Decode([]byte(jsonContent)) + fmt.Println(info) + + // Output: + // map[name:john score:100] +} + +func ExampleDecodeTo() { + type BaseInfo struct { + Name string + Score int + } + + var info BaseInfo + + jsonContent := "{\"name\":\"john\",\"score\":100}" + gjson.DecodeTo([]byte(jsonContent), &info) + fmt.Printf("%+v", info) + + // Output: + // {Name:john Score:100} +} + +func ExampleDecodeToJson() { + jsonContent := `{"name":"john","score":100}"` + j, _ := gjson.DecodeToJson([]byte(jsonContent)) + fmt.Println(j.Map()) + + // Output: + // map[name:john score:100] +} From 144249fcff0ca87dd805f7188fe3533a968b99ef Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 17 Feb 2022 20:29:38 +0800 Subject: [PATCH 5/8] Implemented gjson Example 1.SetSplitChar --- .../gjson/gjson_z_example_pattern_test.go | 21 -------- encoding/gjson/gjson_z_example_test.go | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 encoding/gjson/gjson_z_example_test.go diff --git a/encoding/gjson/gjson_z_example_pattern_test.go b/encoding/gjson/gjson_z_example_pattern_test.go index e6bc4aa91..c7dcb134a 100644 --- a/encoding/gjson/gjson_z_example_pattern_test.go +++ b/encoding/gjson/gjson_z_example_pattern_test.go @@ -32,27 +32,6 @@ func Example_patternGet() { // John Score: 99.5 } -func Example_patternCustomSplitChar() { - data := - `{ - "users" : { - "count" : 2, - "list" : [ - {"name" : "Ming", "score" : 60}, - {"name" : "John", "score" : 99.5} - ] - } - }` - if j, err := gjson.DecodeToJson(data); err != nil { - panic(err) - } else { - j.SetSplitChar('#') - fmt.Println("John Score:", j.Get("users#list#1#score").Float32()) - } - // Output: - // John Score: 99.5 -} - func Example_patternViolenceCheck() { data := `{ diff --git a/encoding/gjson/gjson_z_example_test.go b/encoding/gjson/gjson_z_example_test.go new file mode 100644 index 000000000..124dd1071 --- /dev/null +++ b/encoding/gjson/gjson_z_example_test.go @@ -0,0 +1,48 @@ +package gjson_test + +import ( + "fmt" + "github.com/gogf/gf/v2/encoding/gjson" +) + +func ExampleJson_SetSplitChar() { + data := + `{ + "users" : { + "count" : 2, + "list" : [ + {"name" : "Ming", "score" : 60}, + {"name" : "John", "score" : 99.5} + ] + } + }` + if j, err := gjson.DecodeToJson(data); err != nil { + panic(err) + } else { + j.SetSplitChar('#') + fmt.Println("John Score:", j.Get("users#list#1#score").Float32()) + } + // Output: + // John Score: 99.5 +} + +func ExampleJson_SetViolenceCheck() { + data := + `{ + "users" : { + "count" : 2, + "list" : [ + {"name" : "Ming", "score" : 60}, + {"name" : "John", "score" : 99.5} + ] + } + }` + if j, err := gjson.DecodeToJson(data); err != nil { + panic(err) + } else { + j.SetViolenceCheck(false) + fmt.Println("John Score:", j.Get("users.list.1.score").Float32()) + } + // Output: + // John Score: 99.5 +} From 0bd3537a78417084cb380366f4d64253fec175b4 Mon Sep 17 00:00:00 2001 From: huangqian Date: Thu, 17 Feb 2022 22:54:29 +0800 Subject: [PATCH 6/8] Implemented gjson Example 1.SetViolenceCheck 2.ToJson 3.ToJsonString 4.ToJsonIndent 5.ToJsonIndentString 6.MustToJson 7.MustToJsonString 8.MustToJsonIndent 9.MustToJsonIndentString 10.ToXml 11.ToXmlString 12.ToXmlIndent 13.ToXmlIndentString 14.MustToXml 15.MustToXmlString 16.MustToXmlIndent 17.MustToXmlIndentString 18.ToYaml 19.ToYamlString 20.ToYamlIndent 21.MustToYaml 22.MustToYamlString 23.ToToml 24.ToTomlString 25.MustToToml 26.MustToTomlString 27.ToIni 28.ToIniString 29.MustToIni 30.MustToIniString 31.MarshalJSON 32.UnmarshalJSON 33.UnmarshalValue --- encoding/gjson/gjson_z_example_new_test.go | 4 +- encoding/gjson/gjson_z_example_test.go | 661 ++++++++++++++++++++- 2 files changed, 653 insertions(+), 12 deletions(-) diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index 242ea95ab..997cc6974 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -70,7 +70,7 @@ func ExampleNewWithOptions() { // engineer } -func Example_newFromXml() { +func ExampleNew_Xml() { jsonContent := `john100` j := gjson.New(jsonContent) // Note that there's root node in the XML content. @@ -81,7 +81,7 @@ func Example_newFromXml() { // 100 } -func Example_newFromStruct() { +func ExampleNew_Struct() { type Me struct { Name string `json:"name"` Score int `json:"score"` diff --git a/encoding/gjson/gjson_z_example_test.go b/encoding/gjson/gjson_z_example_test.go index 124dd1071..06e1a4717 100644 --- a/encoding/gjson/gjson_z_example_test.go +++ b/encoding/gjson/gjson_z_example_test.go @@ -30,19 +30,660 @@ func ExampleJson_SetViolenceCheck() { data := `{ "users" : { - "count" : 2, - "list" : [ - {"name" : "Ming", "score" : 60}, - {"name" : "John", "score" : 99.5} - ] - } + "count" : 100 + }, + "users.count" : 101 }` if j, err := gjson.DecodeToJson(data); err != nil { - panic(err) + fmt.Println(err) } else { - j.SetViolenceCheck(false) - fmt.Println("John Score:", j.Get("users.list.1.score").Float32()) + j.SetViolenceCheck(true) + fmt.Println("Users Count:", j.Get("users.count")) } // Output: - // John Score: 99.5 + // Users Count: 101 +} + +// ======================================================================== +// JSON +// ======================================================================== +func ExampleJson_ToJson() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonBytes, _ := j.ToJson() + fmt.Println(string(jsonBytes)) + + // Output: + // {"Age":18,"Name":"John"} +} + +func ExampleJson_ToJsonString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonStr, _ := j.ToJsonString() + fmt.Println(jsonStr) + + // Output: + // {"Age":18,"Name":"John"} +} + +func ExampleJson_ToJsonIndent() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonBytes, _ := j.ToJsonIndent() + fmt.Println(string(jsonBytes)) + + // Output: + //{ + // "Age": 18, + // "Name": "John" + //} +} + +func ExampleJson_ToJsonIndentString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonStr, _ := j.ToJsonIndentString() + fmt.Println(jsonStr) + + // Output: + //{ + // "Age": 18, + // "Name": "John" + //} +} + +func ExampleJson_MustToJson() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonBytes := j.MustToJson() + fmt.Println(string(jsonBytes)) + + // Output: + // {"Age":18,"Name":"John"} +} + +func ExampleJson_MustToJsonString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonStr := j.MustToJsonString() + fmt.Println(jsonStr) + + // Output: + // {"Age":18,"Name":"John"} +} + +func ExampleJson_MustToJsonIndent() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonBytes := j.MustToJsonIndent() + fmt.Println(string(jsonBytes)) + + // Output: + //{ + // "Age": 18, + // "Name": "John" + //} +} + +func ExampleJson_MustToJsonIndentString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonStr := j.MustToJsonIndentString() + fmt.Println(jsonStr) + + // Output: + //{ + // "Age": 18, + // "Name": "John" + //} +} + +// ======================================================================== +// XML +// ======================================================================== +func ExampleJson_ToXml() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlBytes, _ := j.ToXml() + fmt.Println(string(xmlBytes)) + + // Output: + // 18John +} + +func ExampleJson_ToXmlString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlStr, _ := j.ToXmlString() + fmt.Println(string(xmlStr)) + + // Output: + // 18John +} + +func ExampleJson_ToXmlIndent() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlBytes, _ := j.ToXmlIndent() + fmt.Println(string(xmlBytes)) + + // Output: + // + // 18 + // John + // +} + +func ExampleJson_ToXmlIndentString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlStr, _ := j.ToXmlIndentString() + fmt.Println(string(xmlStr)) + + // Output: + // + // 18 + // John + // +} + +func ExampleJson_MustToXml() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlBytes := j.MustToXml() + fmt.Println(string(xmlBytes)) + + // Output: + // 18John +} + +func ExampleJson_MustToXmlString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlStr := j.MustToXmlString() + fmt.Println(string(xmlStr)) + + // Output: + // 18John +} + +func ExampleJson_MustToXmlIndent() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlBytes := j.MustToXmlIndent() + fmt.Println(string(xmlBytes)) + + // Output: + // + // 18 + // John + // +} + +func ExampleJson_MustToXmlIndentString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + xmlStr := j.MustToXmlIndentString() + fmt.Println(string(xmlStr)) + + // Output: + // + // 18 + // John + // +} + +// ======================================================================== +// YAML +// ======================================================================== +func ExampleJson_ToYaml() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + YamlBytes, _ := j.ToYaml() + fmt.Println(string(YamlBytes)) + + // Output: + //Age: 18 + //Name: John +} + +func ExampleJson_ToYamlString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + YamlStr, _ := j.ToYamlString() + fmt.Println(string(YamlStr)) + + // Output: + //Age: 18 + //Name: John +} + +func ExampleJson_ToYamlIndent() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + YamlBytes, _ := j.ToYamlIndent("") + fmt.Println(string(YamlBytes)) + + // Output: + //Age: 18 + //Name: John +} + +func ExampleJson_MustToYaml() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + YamlBytes := j.MustToYaml() + fmt.Println(string(YamlBytes)) + + // Output: + //Age: 18 + //Name: John +} + +func ExampleJson_MustToYamlString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + YamlStr := j.MustToYamlString() + fmt.Println(string(YamlStr)) + + // Output: + //Age: 18 + //Name: John +} + +// ======================================================================== +// TOML +// ======================================================================== +func ExampleJson_ToToml() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + TomlBytes, _ := j.ToToml() + fmt.Println(string(TomlBytes)) + + // Output: + //Age = 18 + //Name = "John" +} + +func ExampleJson_ToTomlString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + TomlStr, _ := j.ToTomlString() + fmt.Println(string(TomlStr)) + + // Output: + //Age = 18 + //Name = "John" +} + +func ExampleJson_MustToToml() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + TomlBytes := j.MustToToml() + fmt.Println(string(TomlBytes)) + + // Output: + //Age = 18 + //Name = "John" +} + +func ExampleJson_MustToTomlString() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + TomlStr := j.MustToTomlString() + fmt.Println(string(TomlStr)) + + // Output: + //Age = 18 + //Name = "John" +} + +// ======================================================================== +// INI +// ======================================================================== +func ExampleJson_ToIni() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + IniBytes, _ := j.ToIni() + fmt.Println(string(IniBytes)) + + // May Output: + //Name=John + //Age=18 +} + +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: + //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: + //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: + //Name=John + //Age=18 +} + +func ExampleJson_MarshalJSON() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + jsonBytes, _ := j.MarshalJSON() + fmt.Println(string(jsonBytes)) + + // Output: + // {"Age":18,"Name":"John"} +} + +func ExampleJson_UnmarshalJSON() { + jsonStr := `{"Age":18,"Name":"John"}` + + j := gjson.New("") + j.UnmarshalJSON([]byte(jsonStr)) + fmt.Println(j.Map()) + + // Output: + // map[Age:18 Name:John] +} + +func ExampleJson_UnmarshalValue_Xml() { + xmlStr := `18John` + + j := gjson.New("") + j.UnmarshalValue([]byte(xmlStr)) + fmt.Println(j.Var().String()) + + // Output: + // {"doc":{"Age":"18","Name":"John"}} } From 5eec9ce7b1934acc46e782906352d7e0ff08f058 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 19 Feb 2022 11:43:45 +0800 Subject: [PATCH 7/8] Modify Function Name --- .../gjson/gjson_z_example_conversion_test.go | 4 ++-- encoding/gjson/gjson_z_example_dataset_test.go | 6 +++--- encoding/gjson/gjson_z_example_load_test.go | 2 +- encoding/gjson/gjson_z_example_pattern_test.go | 6 +++--- encoding/gjson/gjson_z_example_test.go | 18 ++++++++++++++++-- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/encoding/gjson/gjson_z_example_conversion_test.go b/encoding/gjson/gjson_z_example_conversion_test.go index 9f344fcbd..e2c16c7f0 100644 --- a/encoding/gjson/gjson_z_example_conversion_test.go +++ b/encoding/gjson/gjson_z_example_conversion_test.go @@ -60,7 +60,7 @@ func Example_conversionNormalFormats() { // count = 1.0 } -func Example_conversionGetStruct() { +func ExampleJson_ConversionGetStruct() { data := `{ "users" : { @@ -86,7 +86,7 @@ func Example_conversionGetStruct() { // &{Count:1 Array:[John Ming]} } -func Example_conversionToStruct() { +func ExampleJson_ConversionToStruct() { data := ` { diff --git a/encoding/gjson/gjson_z_example_dataset_test.go b/encoding/gjson/gjson_z_example_dataset_test.go index a15ef4c80..b0f6dee04 100644 --- a/encoding/gjson/gjson_z_example_dataset_test.go +++ b/encoding/gjson/gjson_z_example_dataset_test.go @@ -12,7 +12,7 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func Example_dataSetCreate1() { +func ExampleJson_DataSetCreate1() { j := gjson.New(nil) j.Set("name", "John") j.Set("score", 99.5) @@ -28,7 +28,7 @@ func Example_dataSetCreate1() { // {"name":"John","score":99.5} } -func Example_dataSetCreate2() { +func ExampleJson_DataSetCreate2() { j := gjson.New(nil) for i := 0; i < 5; i++ { j.Set(fmt.Sprintf(`%d.id`, i), i) @@ -40,7 +40,7 @@ func Example_dataSetCreate2() { // [{"id":0,"name":"student-0"},{"id":1,"name":"student-1"},{"id":2,"name":"student-2"},{"id":3,"name":"student-3"},{"id":4,"name":"student-4"}] } -func Example_dataSetRuntimeEdit() { +func ExampleJson_DataSetRuntimeEdit() { data := `{ "users" : { diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index fe5e48fe7..9891fa8dd 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -158,7 +158,7 @@ func ExampleIsValidDataType() { // false } -func Example_loadXml() { +func ExampleLoad_Xml() { jsonFilePath := gdebug.TestDataPath("xml", "data1.xml") j, _ := gjson.Load(jsonFilePath) fmt.Println(j.Get("doc.name")) diff --git a/encoding/gjson/gjson_z_example_pattern_test.go b/encoding/gjson/gjson_z_example_pattern_test.go index c7dcb134a..db5f1308a 100644 --- a/encoding/gjson/gjson_z_example_pattern_test.go +++ b/encoding/gjson/gjson_z_example_pattern_test.go @@ -12,7 +12,7 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func Example_patternGet() { +func ExampleDecodeToJson_PatternGet() { data := `{ "users" : { @@ -32,7 +32,7 @@ func Example_patternGet() { // John Score: 99.5 } -func Example_patternViolenceCheck() { +func ExampleDecodeToJson_PatternViolenceCheck() { data := `{ "users" : { @@ -50,7 +50,7 @@ func Example_patternViolenceCheck() { // Users Count: 101 } -func Example_mapSliceChange() { +func ExampleJson_Get_MapSliceChange() { jsonContent := `{"map":{"key":"value"}, "slice":[59,90]}` j, _ := gjson.LoadJson(jsonContent) m := j.Get("map").Map() diff --git a/encoding/gjson/gjson_z_example_test.go b/encoding/gjson/gjson_z_example_test.go index 06e1a4717..ed2dce2b1 100644 --- a/encoding/gjson/gjson_z_example_test.go +++ b/encoding/gjson/gjson_z_example_test.go @@ -677,13 +677,27 @@ func ExampleJson_UnmarshalJSON() { // map[Age:18 Name:John] } +func ExampleJson_UnmarshalValue_Yaml() { + yamlContent := + `base: + name: john + score: 100` + + j := gjson.New("") + j.UnmarshalValue([]byte(yamlContent)) + fmt.Println(j.Var().String()) + + // Output: + // {"base":{"name":"john","score":100}} +} + func ExampleJson_UnmarshalValue_Xml() { - xmlStr := `18John` + xmlStr := `john100` j := gjson.New("") j.UnmarshalValue([]byte(xmlStr)) fmt.Println(j.Var().String()) // Output: - // {"doc":{"Age":"18","Name":"John"}} + // {"doc":{"name":"john","score":"100"}} } From b2b0a1828e6911e7c4fb432581eaa793f5d2c6bb Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 19 Feb 2022 19:48:50 +0800 Subject: [PATCH 8/8] Implemented gjson Example 1.MapStrAny 2.Interfaces 3.Interface 4.Var 5.IsNil 6.Get 7.GetJson 8.GetJsons 9.GetJsonMap 10.Set 11.MustSet 12.Remove 13.MustRemove 14.Contains 15.Len 16.Append 17.MustAppend 18. Map 19.Array 20.Scan 21.Dump --- .../gjson/gjson_z_example_dataset_test.go | 4 +- encoding/gjson/gjson_z_example_test.go | 407 ++++++++++++++++++ 2 files changed, 409 insertions(+), 2 deletions(-) diff --git a/encoding/gjson/gjson_z_example_dataset_test.go b/encoding/gjson/gjson_z_example_dataset_test.go index b0f6dee04..d80ce2eb0 100644 --- a/encoding/gjson/gjson_z_example_dataset_test.go +++ b/encoding/gjson/gjson_z_example_dataset_test.go @@ -12,7 +12,7 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func ExampleJson_DataSetCreate1() { +func ExampleJson_Set_DataSetCreate1() { j := gjson.New(nil) j.Set("name", "John") j.Set("score", 99.5) @@ -28,7 +28,7 @@ func ExampleJson_DataSetCreate1() { // {"name":"John","score":99.5} } -func ExampleJson_DataSetCreate2() { +func ExampleJson_Set_DataSetCreate2() { j := gjson.New(nil) for i := 0; i < 5; i++ { j.Set(fmt.Sprintf(`%d.id`, i), i) diff --git a/encoding/gjson/gjson_z_example_test.go b/encoding/gjson/gjson_z_example_test.go index ed2dce2b1..e4740fd28 100644 --- a/encoding/gjson/gjson_z_example_test.go +++ b/encoding/gjson/gjson_z_example_test.go @@ -701,3 +701,410 @@ func ExampleJson_UnmarshalValue_Xml() { // Output: // {"doc":{"name":"john","score":"100"}} } + +func ExampleJson_MapStrAny() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + fmt.Println(j.MapStrAny()) + + // Output: + // map[Age:18 Name:John] +} + +func ExampleJson_Interfaces() { + type BaseInfo struct { + Name string + Age int + } + + infoList := []BaseInfo{ + BaseInfo{ + Name: "John", + Age: 18, + }, + BaseInfo{ + Name: "Tom", + Age: 20, + }, + } + + j := gjson.New(infoList) + fmt.Println(j.Interfaces()) + + // Output: + // [{John 18} {Tom 20}] +} + +func ExampleJson_Interface() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + fmt.Println(j.Interface()) + + // Output: + // map[Age:18 Name:John] +} + +func ExampleJson_Var() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + fmt.Println(j.Var().String()) + fmt.Println(j.Var().Map()) + + // Output: + // {"Age":18,"Name":"John"} + // map[Age:18 Name:John] +} + +func ExampleJson_IsNil() { + data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) + data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) + + j1, _ := gjson.LoadContent(data1) + fmt.Println(j1.IsNil()) + + j2, _ := gjson.LoadContent(data2) + fmt.Println(j2.IsNil()) + + // Output: + // false + // true +} + +func ExampleJson_Get() { + data := + `{ + "users" : { + "count" : 1, + "array" : ["John", "Ming"] + } + }` + + j, _ := gjson.LoadContent(data) + fmt.Println(j.Get("users")) + fmt.Println(j.Get("users.count")) + fmt.Println(j.Get("users.array")) + + // Output: + // {"array":["John","Ming"],"count":1} + // 1 + // ["John","Ming"] +} + +func ExampleJson_GetJson() { + data := + `{ + "users" : { + "count" : 1, + "array" : ["John", "Ming"] + } + }` + + j, _ := gjson.LoadContent(data) + + fmt.Println(j.GetJson("users.array").Array()) + + // Output: + // [John Ming] +} + +func ExampleJson_GetJsons() { + data := + `{ + "users" : { + "count" : 3, + "array" : [{"Age":18,"Name":"John"}, {"Age":20,"Name":"Tom"}] + } + }` + + j, _ := gjson.LoadContent(data) + + jsons := j.GetJsons("users.array") + for _, json := range jsons { + fmt.Println(json.Interface()) + } + + // Output: + // map[Age:18 Name:John] + // map[Age:20 Name:Tom] +} + +func ExampleJson_GetJsonMap() { + data := + `{ + "users" : { + "count" : 1, + "array" : { + "info" : {"Age":18,"Name":"John"}, + "addr" : {"City":"Chengdu","Company":"Tencent"} + } + } + }` + + j, _ := gjson.LoadContent(data) + + jsonMap := j.GetJsonMap("users.array") + + for _, json := range jsonMap { + fmt.Println(json.Interface()) + } + + // May Output: + // map[City:Chengdu Company:Tencent] + // map[Age:18 Name:John] +} + +func ExampleJson_Set() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + j.Set("Addr", "ChengDu") + fmt.Println(j.Var().String()) + + // Output: + // {"Addr":"ChengDu","Age":18,"Name":"John"} +} + +func ExampleJson_MustSet() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + j.MustSet("Addr", "ChengDu") + fmt.Println(j.Var().String()) + + // Output: + // {"Addr":"ChengDu","Age":18,"Name":"John"} +} + +func ExampleJson_Remove() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + j.Remove("Age") + fmt.Println(j.Var().String()) + + // Output: + // {"Name":"John"} +} + +func ExampleJson_MustRemove() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + j.MustRemove("Age") + fmt.Println(j.Var().String()) + + // Output: + // {"Name":"John"} +} + +func ExampleJson_Contains() { + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{ + Name: "John", + Age: 18, + } + + j := gjson.New(info) + fmt.Println(j.Contains("Age")) + fmt.Println(j.Contains("Addr")) + + // Output: + // true + // false +} + +func ExampleJson_Len() { + data := + `{ + "users" : { + "count" : 1, + "nameArray" : ["Join", "Tom"], + "infoMap" : { + "name" : "Join", + "age" : 18, + "addr" : "ChengDu" + } + } + }` + + j, _ := gjson.LoadContent(data) + + fmt.Println(j.Len("users.nameArray")) + fmt.Println(j.Len("users.infoMap")) + + // Output: + // 2 + // 3 +} + +func ExampleJson_Append() { + data := + `{ + "users" : { + "count" : 1, + "array" : ["John", "Ming"] + } + }` + + j, _ := gjson.LoadContent(data) + + j.Append("users.array", "Lily") + + fmt.Println(j.Get("users.array").Array()) + + // Output: + // [John Ming Lily] +} + +func ExampleJson_MustAppend() { + data := + `{ + "users" : { + "count" : 1, + "array" : ["John", "Ming"] + } + }` + + j, _ := gjson.LoadContent(data) + + j.MustAppend("users.array", "Lily") + + fmt.Println(j.Get("users.array").Array()) + + // Output: + // [John Ming Lily] +} + +func ExampleJson_Map() { + data := + `{ + "users" : { + "count" : 1, + "info" : { + "name" : "John", + "age" : 18, + "addr" : "ChengDu" + } + } + }` + + j, _ := gjson.LoadContent(data) + + fmt.Println(j.Get("users.info").Map()) + + // Output: + // map[addr:ChengDu age:18 name:John] +} + +func ExampleJson_Array() { + data := + `{ + "users" : { + "count" : 1, + "array" : ["John", "Ming"] + } + }` + + j, _ := gjson.LoadContent(data) + + fmt.Println(j.Get("users.array")) + + // Output: + // ["John","Ming"] +} + +func ExampleJson_Scan() { + data := `{"name":"john","age":"18"}` + + type BaseInfo struct { + Name string + Age int + } + + info := BaseInfo{} + + j, _ := gjson.LoadContent(data) + j.Scan(&info) + + fmt.Println(info) + + // Output: + // {john 18} +} + +func ExampleJson_Dump() { + data := `{"name":"john","age":"18"}` + + j, _ := gjson.LoadContent(data) + + j.Dump() + + // May Output: + //{ + // "age": "18", + // "name": "john", + //} +}