github.com/gogf/gf/v2@v2.7.4/encoding/gjson/gjson_z_unit_feature_new_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gjson_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/encoding/gjson" 13 "github.com/gogf/gf/v2/test/gtest" 14 ) 15 16 func Test_NewWithTag(t *testing.T) { 17 type User struct { 18 Age int `xml:"age-xml" json:"age-json"` 19 Name string `xml:"name-xml" json:"name-json"` 20 Addr string `xml:"addr-xml" json:"addr-json"` 21 } 22 data := User{ 23 Age: 18, 24 Name: "john", 25 Addr: "chengdu", 26 } 27 // JSON 28 gtest.C(t, func(t *gtest.T) { 29 j := gjson.New(data) 30 t.AssertNE(j, nil) 31 t.Assert(j.Get("age-xml"), nil) 32 t.Assert(j.Get("age-json"), data.Age) 33 t.Assert(j.Get("name-xml"), nil) 34 t.Assert(j.Get("name-json"), data.Name) 35 t.Assert(j.Get("addr-xml"), nil) 36 t.Assert(j.Get("addr-json"), data.Addr) 37 }) 38 // XML 39 gtest.C(t, func(t *gtest.T) { 40 j := gjson.NewWithTag(data, "xml") 41 t.AssertNE(j, nil) 42 t.Assert(j.Get("age-xml"), data.Age) 43 t.Assert(j.Get("age-json"), nil) 44 t.Assert(j.Get("name-xml"), data.Name) 45 t.Assert(j.Get("name-json"), nil) 46 t.Assert(j.Get("addr-xml"), data.Addr) 47 t.Assert(j.Get("addr-json"), nil) 48 }) 49 } 50 51 func Test_New_CustomStruct(t *testing.T) { 52 type Base struct { 53 Id int 54 } 55 type User struct { 56 Base 57 Name string 58 } 59 user := new(User) 60 user.Id = 1 61 user.Name = "john" 62 63 gtest.C(t, func(t *gtest.T) { 64 j := gjson.New(user) 65 t.AssertNE(j, nil) 66 67 s, err := j.ToJsonString() 68 t.AssertNil(err) 69 t.Assert(s == `{"Id":1,"Name":"john"}` || s == `{"Name":"john","Id":1}`, true) 70 }) 71 } 72 73 func Test_New_HierarchicalStruct(t *testing.T) { 74 gtest.C(t, func(t *gtest.T) { 75 type Me struct { 76 Name string `json:"name"` 77 Score int `json:"score"` 78 Children []Me `json:"children"` 79 } 80 me := Me{ 81 Name: "john", 82 Score: 100, 83 Children: []Me{ 84 { 85 Name: "Bean", 86 Score: 99, 87 }, 88 { 89 Name: "Sam", 90 Score: 98, 91 }, 92 }, 93 } 94 j := gjson.New(me) 95 t.Assert(j.Remove("children.0.score"), nil) 96 t.Assert(j.Remove("children.1.score"), nil) 97 t.Assert(j.MustToJsonString(), `{"children":[{"children":null,"name":"Bean"},{"children":null,"name":"Sam"}],"name":"john","score":100}`) 98 }) 99 } 100 101 func Test_NewWithOptions(t *testing.T) { 102 gtest.C(t, func(t *gtest.T) { 103 data := []byte("[9223372036854775807, 9223372036854775806]") 104 array := gjson.New(data).Var().Array() 105 t.Assert(array, []uint64{9223372036854776000, 9223372036854776000}) 106 }) 107 gtest.C(t, func(t *gtest.T) { 108 data := []byte("[9223372036854775807, 9223372036854775806]") 109 array := gjson.NewWithOptions(data, gjson.Options{StrNumber: true}).Var().Array() 110 t.Assert(array, []uint64{9223372036854775807, 9223372036854775806}) 111 }) 112 } 113 114 func Test_LoadContentType(t *testing.T) { 115 gtest.C(t, func(t *gtest.T) { 116 data := []byte("value = 79937385836643329") 117 j, err := gjson.LoadContentType("toml", data) 118 t.AssertNil(err) 119 value := j.Get("value").Int64() 120 t.Assert(value, 79937385836643329) 121 }) 122 }