From 8cb6086f736961c82f7361f65782114d2292d9c8 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 12 Feb 2022 17:27:32 +0800 Subject: [PATCH 1/5] 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/5] 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/5] 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/5] 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/5] 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 +}