2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-01-20 19:56:42 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gjson_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2020-01-20 19:56:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJson_UnmarshalJSON(t *testing.T) {
|
2022-01-15 20:25:29 +08:00
|
|
|
// Json Array
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-01-15 20:25:29 +08:00
|
|
|
var (
|
|
|
|
data = []byte(`["a", "b", "c"]`)
|
|
|
|
j = gjson.New(nil)
|
|
|
|
err = json.UnmarshalUseNumber(data, j)
|
|
|
|
)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(j.Get(".").String(), `["a","b","c"]`)
|
|
|
|
t.Assert(j.Get("2").String(), `c`)
|
|
|
|
})
|
|
|
|
// Json Map
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var (
|
|
|
|
data = []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
|
|
|
j = gjson.New(nil)
|
|
|
|
err = json.UnmarshalUseNumber(data, j)
|
|
|
|
)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2021-09-27 22:47:39 +08:00
|
|
|
t.Assert(j.Get("n").String(), "123456789")
|
|
|
|
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
|
|
|
|
t.Assert(j.Get("m.k").String(), "v")
|
|
|
|
t.Assert(j.Get("a").Array(), g.Slice{1, 2, 3})
|
|
|
|
t.Assert(j.Get("a.1").Int(), 2)
|
2020-01-20 19:56:42 +08:00
|
|
|
})
|
2022-01-15 20:25:29 +08:00
|
|
|
|
2020-01-20 19:56:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJson_UnmarshalValue(t *testing.T) {
|
2020-03-20 22:34:56 +08:00
|
|
|
type V struct {
|
2020-01-20 19:56:42 +08:00
|
|
|
Name string
|
|
|
|
Json *gjson.Json
|
|
|
|
}
|
2022-01-15 20:25:29 +08:00
|
|
|
// Json Map.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-20 22:34:56 +08:00
|
|
|
var v *V
|
2020-01-20 19:56:42 +08:00
|
|
|
err := gconv.Struct(g.Map{
|
|
|
|
"name": "john",
|
|
|
|
"json": []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`),
|
2020-03-19 23:53:03 +08:00
|
|
|
}, &v)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-19 23:53:03 +08:00
|
|
|
t.Assert(v.Name, "john")
|
2021-09-27 22:47:39 +08:00
|
|
|
t.Assert(v.Json.Get("n").String(), "123456789")
|
|
|
|
t.Assert(v.Json.Get("m").Map(), g.Map{"k": "v"})
|
|
|
|
t.Assert(v.Json.Get("m.k").String(), "v")
|
|
|
|
t.Assert(v.Json.Get("a").Slice(), g.Slice{1, 2, 3})
|
|
|
|
t.Assert(v.Json.Get("a.1").Int(), 2)
|
2020-01-20 19:56:42 +08:00
|
|
|
})
|
2022-01-15 20:25:29 +08:00
|
|
|
// Json Array.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var v *V
|
|
|
|
err := gconv.Struct(g.Map{
|
|
|
|
"name": "john",
|
|
|
|
"json": `["a", "b", "c"]`,
|
|
|
|
}, &v)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(v.Name, "john")
|
|
|
|
t.Assert(v.Json.Get(".").String(), `["a","b","c"]`)
|
|
|
|
t.Assert(v.Json.Get("2").String(), `c`)
|
|
|
|
})
|
2020-01-20 19:56:42 +08:00
|
|
|
// Map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-20 22:34:56 +08:00
|
|
|
var v *V
|
2020-01-20 19:56:42 +08:00
|
|
|
err := gconv.Struct(g.Map{
|
|
|
|
"name": "john",
|
|
|
|
"json": g.Map{
|
|
|
|
"n": 123456789,
|
|
|
|
"m": g.Map{"k": "v"},
|
|
|
|
"a": g.Slice{1, 2, 3},
|
|
|
|
},
|
2020-03-19 23:53:03 +08:00
|
|
|
}, &v)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-19 23:53:03 +08:00
|
|
|
t.Assert(v.Name, "john")
|
2021-09-27 22:47:39 +08:00
|
|
|
t.Assert(v.Json.Get("n").String(), "123456789")
|
|
|
|
t.Assert(v.Json.Get("m").Map(), g.Map{"k": "v"})
|
|
|
|
t.Assert(v.Json.Get("m.k").String(), "v")
|
|
|
|
t.Assert(v.Json.Get("a").Slice(), g.Slice{1, 2, 3})
|
|
|
|
t.Assert(v.Json.Get("a.1").Int(), 2)
|
2020-01-20 19:56:42 +08:00
|
|
|
})
|
|
|
|
}
|