mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
Merge branch 'gjson_example'
This commit is contained in:
@ -8,7 +8,6 @@ package gjson_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
)
|
||||
|
||||
@ -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]
|
||||
}
|
||||
|
||||
@ -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 := `<?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 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
|
||||
}
|
||||
|
||||
@ -12,16 +12,64 @@ 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 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 Example_newFromXml() {
|
||||
jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
|
||||
j := gjson.New(jsonContent)
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 :=
|
||||
`{
|
||||
|
||||
48
encoding/gjson/gjson_z_example_test.go
Normal file
48
encoding/gjson/gjson_z_example_test.go
Normal file
@ -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
|
||||
}
|
||||
3
encoding/gjson/testdata/toml/data1.toml
vendored
Normal file
3
encoding/gjson/testdata/toml/data1.toml
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[base]
|
||||
name = "john"
|
||||
score = 100
|
||||
3
encoding/gjson/testdata/yaml/data1.yaml
vendored
Normal file
3
encoding/gjson/testdata/yaml/data1.yaml
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
base:
|
||||
name: john
|
||||
score: 100
|
||||
Reference in New Issue
Block a user