diff --git a/encoding/gjson/gjson_z_example_conversion_test.go b/encoding/gjson/gjson_z_example_conversion_test.go
index 4fbfa05a6..e2c16c7f0 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"
)
@@ -61,7 +60,7 @@ func Example_conversionNormalFormats() {
// count = 1.0
}
-func Example_conversionGetStruct() {
+func ExampleJson_ConversionGetStruct() {
data :=
`{
"users" : {
@@ -87,7 +86,7 @@ func Example_conversionGetStruct() {
// &{Count:1 Array:[John Ming]}
}
-func Example_conversionToStruct() {
+func ExampleJson_ConversionToStruct() {
data :=
`
{
@@ -111,3 +110,166 @@ 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
+ // }
+}
+
+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]
+}
diff --git a/encoding/gjson/gjson_z_example_dataset_test.go b/encoding/gjson/gjson_z_example_dataset_test.go
index a15ef4c80..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 Example_dataSetCreate1() {
+func ExampleJson_Set_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_Set_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 84deb1fee..9891fa8dd 100644
--- a/encoding/gjson/gjson_z_example_load_test.go
+++ b/encoding/gjson/gjson_z_example_load_test.go
@@ -13,26 +13,154 @@ 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 Example_loadXml() {
+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 ExampleLoad_Xml() {
jsonFilePath := gdebug.TestDataPath("xml", "data1.xml")
j, _ := gjson.Load(jsonFilePath)
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 644e567c4..997cc6974 100644
--- a/encoding/gjson/gjson_z_example_new_test.go
+++ b/encoding/gjson/gjson_z_example_new_test.go
@@ -12,17 +12,65 @@ import (
"github.com/gogf/gf/v2/encoding/gjson"
)
-func Example_newFromJson() {
+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 Example_newFromXml() {
+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
+}
+
+func ExampleNew_Xml() {
jsonContent := `john100`
j := gjson.New(jsonContent)
// Note that there's root node in the XML content.
@@ -33,7 +81,7 @@ func Example_newFromXml() {
// 100
}
-func Example_newFromStruct() {
+func ExampleNew_Struct() {
type Me struct {
Name string `json:"name"`
Score int `json:"score"`
@@ -49,26 +97,3 @@ func Example_newFromStruct() {
// 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
-}
diff --git a/encoding/gjson/gjson_z_example_pattern_test.go b/encoding/gjson/gjson_z_example_pattern_test.go
index e6bc4aa91..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,28 +32,7 @@ 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() {
+func ExampleDecodeToJson_PatternViolenceCheck() {
data :=
`{
"users" : {
@@ -71,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
new file mode 100644
index 000000000..e4740fd28
--- /dev/null
+++ b/encoding/gjson/gjson_z_example_test.go
@@ -0,0 +1,1110 @@
+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" : 100
+ },
+ "users.count" : 101
+ }`
+ if j, err := gjson.DecodeToJson(data); err != nil {
+ fmt.Println(err)
+ } else {
+ j.SetViolenceCheck(true)
+ fmt.Println("Users Count:", j.Get("users.count"))
+ }
+ // Output:
+ // 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_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 := `john100`
+
+ j := gjson.New("")
+ j.UnmarshalValue([]byte(xmlStr))
+ fmt.Println(j.Var().String())
+
+ // 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",
+ //}
+}
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