github.com/wangyougui/gf/v2@v2.6.5/encoding/gjson/gjson_z_unit_implements_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/wangyougui/gf. 6 7 package gjson_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/encoding/gjson" 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/gf/v2/internal/json" 15 "github.com/wangyougui/gf/v2/test/gtest" 16 "github.com/wangyougui/gf/v2/util/gconv" 17 ) 18 19 func TestJson_UnmarshalJSON(t *testing.T) { 20 // Json Array 21 gtest.C(t, func(t *gtest.T) { 22 var ( 23 data = []byte(`["a", "b", "c"]`) 24 j = gjson.New(nil) 25 err = json.UnmarshalUseNumber(data, j) 26 ) 27 t.AssertNil(err) 28 t.Assert(j.Get(".").String(), `["a","b","c"]`) 29 t.Assert(j.Get("2").String(), `c`) 30 }) 31 // Json Array Map 32 gtest.C(t, func(t *gtest.T) { 33 var ( 34 data = []byte(`[{"a":1}, {"b":2}, {"c":3}]`) 35 j = gjson.New(nil) 36 err = json.UnmarshalUseNumber(data, j) 37 ) 38 t.AssertNil(err) 39 t.Assert(j.Get(".").String(), `[{"a":1},{"b":2},{"c":3}]`) 40 t.Assert(j.Get("2.c").String(), `3`) 41 }) 42 // Json Map 43 gtest.C(t, func(t *gtest.T) { 44 var ( 45 data = []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) 46 j = gjson.New(nil) 47 err = json.UnmarshalUseNumber(data, j) 48 ) 49 t.AssertNil(err) 50 t.Assert(j.Get("n").String(), "123456789") 51 t.Assert(j.Get("m").Map(), g.Map{"k": "v"}) 52 t.Assert(j.Get("m.k").String(), "v") 53 t.Assert(j.Get("a").Array(), g.Slice{1, 2, 3}) 54 t.Assert(j.Get("a.1").Int(), 2) 55 }) 56 57 } 58 59 func TestJson_UnmarshalValue(t *testing.T) { 60 type V struct { 61 Name string 62 Json *gjson.Json 63 } 64 // Json Map. 65 gtest.C(t, func(t *gtest.T) { 66 var v *V 67 err := gconv.Struct(g.Map{ 68 "name": "john", 69 "json": []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`), 70 }, &v) 71 t.AssertNil(err) 72 t.Assert(v.Name, "john") 73 t.Assert(v.Json.Get("n").String(), "123456789") 74 t.Assert(v.Json.Get("m").Map(), g.Map{"k": "v"}) 75 t.Assert(v.Json.Get("m.k").String(), "v") 76 t.Assert(v.Json.Get("a").Slice(), g.Slice{1, 2, 3}) 77 t.Assert(v.Json.Get("a.1").Int(), 2) 78 }) 79 // Json Array. 80 gtest.C(t, func(t *gtest.T) { 81 var v *V 82 err := gconv.Struct(g.Map{ 83 "name": "john", 84 "json": `["a", "b", "c"]`, 85 }, &v) 86 t.AssertNil(err) 87 t.Assert(v.Name, "john") 88 t.Assert(v.Json.Get(".").String(), `["a","b","c"]`) 89 t.Assert(v.Json.Get("2").String(), `c`) 90 }) 91 // Json Array Map. 92 gtest.C(t, func(t *gtest.T) { 93 var v *V 94 err := gconv.Struct(g.Map{ 95 "name": "john", 96 "json": `[{"a":1},{"b":2},{"c":3}]`, 97 }, &v) 98 t.AssertNil(err) 99 t.Assert(v.Name, "john") 100 t.Assert(v.Json.Get(".").String(), `[{"a":1},{"b":2},{"c":3}]`) 101 t.Assert(v.Json.Get("2.c").String(), `3`) 102 }) 103 // Map 104 gtest.C(t, func(t *gtest.T) { 105 var v *V 106 err := gconv.Struct(g.Map{ 107 "name": "john", 108 "json": g.Map{ 109 "n": 123456789, 110 "m": g.Map{"k": "v"}, 111 "a": g.Slice{1, 2, 3}, 112 }, 113 }, &v) 114 t.AssertNil(err) 115 t.Assert(v.Name, "john") 116 t.Assert(v.Json.Get("n").String(), "123456789") 117 t.Assert(v.Json.Get("m").Map(), g.Map{"k": "v"}) 118 t.Assert(v.Json.Get("m.k").String(), "v") 119 t.Assert(v.Json.Get("a").Slice(), g.Slice{1, 2, 3}) 120 t.Assert(v.Json.Get("a.1").Int(), 2) 121 }) 122 }