Merge pull request #1610 from huangqian1985/master

gjson Example Function
This commit is contained in:
John Guo 2022-02-22 22:03:54 +08:00 committed by GitHub
commit d23b24dfba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1478 additions and 68 deletions

View File

@ -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]
}

View File

@ -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" : {

View File

@ -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 := `<?xml version="1.0" encoding="UTF-8"?>
<base>
<name>john</name>
<score>100</score>
</base>`
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 := `<?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() {
jsonContent := `{"name":"john", "score":"100"}`
xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
<base>
<name>john</name>
<score>100</score>
</base>`
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
}

View File

@ -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 := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
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
}

View File

@ -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()

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
[base]
name = "john"
score = 100

View File

@ -0,0 +1,3 @@
base:
name: john
score: 100