Files
gf/encoding/gjson/gjson_z_unit_test.go

618 lines
16 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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 (
"fmt"
2019-07-18 18:59:49 +08:00
"testing"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/container/gmap"
2022-01-15 22:15:10 +08:00
"github.com/gogf/gf/v2/container/gvar"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2022-04-11 20:49:33 +08:00
"github.com/gogf/gf/v2/util/gconv"
)
func Test_New(t *testing.T) {
2022-01-15 22:15:10 +08:00
// New with json map.
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2022-01-15 22:15:10 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2019-06-12 22:14:31 +08:00
j := gjson.New(data)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("a").Array(), g.Slice{1, 2, 3})
2019-06-12 22:14:31 +08:00
})
2022-01-15 22:15:10 +08:00
// New with json array map.
gtest.C(t, func(t *gtest.T) {
j := gjson.New(`[{"a":1},{"b":2},{"c":3}]`)
t.Assert(j.Get(".").String(), `[{"a":1},{"b":2},{"c":3}]`)
t.Assert(j.Get("2.c").String(), `3`)
})
// New with gvar.
// https://github.com/gogf/gf/issues/1571
gtest.C(t, func(t *gtest.T) {
v := gvar.New(`[{"a":1},{"b":2},{"c":3}]`)
j := gjson.New(v)
t.Assert(j.Get(".").String(), `[{"a":1},{"b":2},{"c":3}]`)
t.Assert(j.Get("2.c").String(), `3`)
})
// New with gmap.
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
})
j := gjson.New(m)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("k1"), "v1")
t.Assert(j.Get("k2"), "v2")
t.Assert(j.Get("k3"), nil)
})
2024-01-11 22:14:22 +08:00
// https://github.com/gogf/gf/issues/3253
gtest.C(t, func(t *gtest.T) {
type TestStruct struct {
Result []map[string]string `json:"result"`
}
ts := &TestStruct{
Result: []map[string]string{
{
"Name": "gf",
"Role": "",
},
},
}
gjson.New(ts)
})
}
func Test_Valid(t *testing.T) {
2019-06-12 22:14:31 +08:00
data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gjson.Valid(data1), true)
t.Assert(gjson.Valid(data2), false)
2019-06-12 22:14:31 +08:00
})
}
func Test_Encode(t *testing.T) {
2019-06-12 22:14:31 +08:00
value := g.Slice{1, 2, 3}
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
b, err := gjson.Encode(value)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(b, []byte(`[1,2,3]`))
2019-06-12 22:14:31 +08:00
})
}
func Test_Decode(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
v, err := gjson.Decode(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(v, g.Map{
2019-06-12 22:14:31 +08:00
"n": 123456789,
"a": g.Slice{1, 2, 3},
"m": g.Map{
"k": "v",
},
})
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
var v any
2019-06-12 22:14:31 +08:00
err := gjson.DecodeTo(data, &v)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(v, g.Map{
2019-06-12 22:14:31 +08:00
"n": 123456789,
"a": g.Slice{1, 2, 3},
"m": g.Map{
"k": "v",
},
})
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("m.k"), "v")
t.Assert(j.Get("a").Array(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
}
func Test_SplitChar(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
j.SetSplitChar(byte('#'))
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
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)
2019-06-12 22:14:31 +08:00
})
}
func Test_ViolenceCheck(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"m":{"a":[1,2,3], "v1.v2":"4"}}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("m.a.2"), 3)
t.Assert(j.Get("m.v1.v2"), nil)
2019-06-12 22:14:31 +08:00
j.SetViolenceCheck(true)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("m.v1.v2"), 4)
2019-06-12 22:14:31 +08:00
})
}
func Test_GetVar(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("a").Interfaces(), g.Slice{1, 2, 3})
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a").Array(), g.Slice{1, 2, 3})
2019-06-12 22:14:31 +08:00
})
}
func Test_GetMap(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").Map(), nil)
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("a").Map(), g.Map{"1": "2", "3": nil})
2019-06-12 22:14:31 +08:00
})
}
func Test_GetJson(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-06-12 22:14:31 +08:00
j2 := j.GetJson("m")
2020-03-19 22:56:12 +08:00
t.AssertNE(j2, nil)
t.Assert(j2.Get("k"), "v")
t.Assert(j2.Get("a"), nil)
t.Assert(j2.Get("n"), nil)
2019-06-12 22:14:31 +08:00
})
}
func Test_GetArray(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").Array(), g.Array{123456789})
t.Assert(j.Get("m").Array(), g.Array{g.Map{"k": "v"}})
t.Assert(j.Get("a").Array(), g.Array{1, 2, 3})
2019-06-12 22:14:31 +08:00
})
}
func Test_GetString(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.AssertEQ(j.Get("n").String(), "123456789")
t.AssertEQ(j.Get("m").String(), `{"k":"v"}`)
t.AssertEQ(j.Get("a").String(), `[1,2,3]`)
t.AssertEQ(j.Get("i").String(), "")
2019-06-12 22:14:31 +08:00
})
}
func Test_GetStrings(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.AssertEQ(j.Get("n").Strings(), g.SliceStr{"123456789"})
t.AssertEQ(j.Get("m").Strings(), g.SliceStr{`{"k":"v"}`})
t.AssertEQ(j.Get("a").Strings(), g.SliceStr{"1", "2", "3"})
t.AssertEQ(j.Get("i").Strings(), nil)
2019-06-12 22:14:31 +08:00
})
}
func Test_GetInterfaces(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.DecodeToJson(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.AssertEQ(j.Get("n").Interfaces(), g.Array{123456789})
t.AssertEQ(j.Get("m").Interfaces(), g.Array{g.Map{"k": "v"}})
t.AssertEQ(j.Get("a").Interfaces(), g.Array{1, 2, 3})
2019-06-12 22:14:31 +08:00
})
}
func Test_Len(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
p := gjson.New(nil)
p.Append("a", 1)
p.Append("a", 2)
2020-03-19 22:56:12 +08:00
t.Assert(p.Len("a"), 2)
2019-06-12 22:14:31 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
p := gjson.New(nil)
p.Append("a.b", 1)
p.Append("a.c", 2)
2020-03-19 22:56:12 +08:00
t.Assert(p.Len("a"), 2)
2019-06-12 22:14:31 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
p := gjson.New(nil)
p.Set("a", 1)
2020-03-19 22:56:12 +08:00
t.Assert(p.Len("a"), -1)
2019-06-12 22:14:31 +08:00
})
}
func Test_Append(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-05-11 17:56:14 +08:00
p := gjson.New(nil)
p.Append("a", 1)
p.Append("a", 2)
2020-03-19 22:56:12 +08:00
t.Assert(p.Get("a"), g.Slice{1, 2})
2019-05-11 17:56:14 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-05-11 17:56:14 +08:00
p := gjson.New(nil)
p.Append("a.b", 1)
p.Append("a.c", 2)
t.Assert(p.Get("a").Map(), g.Map{
2019-06-12 22:14:31 +08:00
"b": g.Slice{1},
"c": g.Slice{2},
2019-05-11 17:56:14 +08:00
})
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-05-11 17:56:14 +08:00
p := gjson.New(nil)
p.Set("a", 1)
err := p.Append("a", 2)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
t.Assert(p.Get("a"), 1)
2019-05-11 17:56:14 +08:00
})
}
func Test_RawArray(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
j := gjson.New(nil)
t.AssertNil(j.Set("0", 1))
t.AssertNil(j.Set("1", 2))
t.Assert(j.MustToJsonString(), `[1,2]`)
})
gtest.C(t, func(t *gtest.T) {
j := gjson.New(nil)
t.AssertNil(j.Append(".", 1))
t.AssertNil(j.Append(".", 2))
t.Assert(j.MustToJsonString(), `[1,2]`)
})
}
2019-05-11 17:56:14 +08:00
func TestJson_ToJson(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
p := gjson.New(1)
2019-05-11 17:56:14 +08:00
s, e := p.ToJsonString()
2020-03-19 22:56:12 +08:00
t.Assert(e, nil)
t.Assert(s, "1")
2019-05-11 17:56:14 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-05-11 17:56:14 +08:00
p := gjson.New("a")
s, e := p.ToJsonString()
2020-03-19 22:56:12 +08:00
t.Assert(e, nil)
t.Assert(s, `"a"`)
2019-05-11 17:56:14 +08:00
})
}
func TestJson_Default(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
j := gjson.New(nil)
t.AssertEQ(j.Get("no", 100).Int(), 100)
t.AssertEQ(j.Get("no", 100).String(), "100")
t.AssertEQ(j.Get("no", "on").Bool(), true)
t.AssertEQ(j.Get("no", 100).Int(), 100)
t.AssertEQ(j.Get("no", 100).Int8(), int8(100))
t.AssertEQ(j.Get("no", 100).Int16(), int16(100))
t.AssertEQ(j.Get("no", 100).Int32(), int32(100))
t.AssertEQ(j.Get("no", 100).Int64(), int64(100))
t.AssertEQ(j.Get("no", 100).Uint(), uint(100))
t.AssertEQ(j.Get("no", 100).Uint8(), uint8(100))
t.AssertEQ(j.Get("no", 100).Uint16(), uint16(100))
t.AssertEQ(j.Get("no", 100).Uint32(), uint32(100))
t.AssertEQ(j.Get("no", 100).Uint64(), uint64(100))
t.AssertEQ(j.Get("no", 123.456).Float32(), float32(123.456))
t.AssertEQ(j.Get("no", 123.456).Float64(), float64(123.456))
t.AssertEQ(j.Get("no", g.Slice{1, 2, 3}).Array(), g.Slice{1, 2, 3})
t.AssertEQ(j.Get("no", g.Slice{1, 2, 3}).Ints(), g.SliceInt{1, 2, 3})
t.AssertEQ(j.Get("no", g.Slice{1, 2, 3}).Floats(), []float64{1, 2, 3})
t.AssertEQ(j.Get("no", g.Map{"k": "v"}).Map(), g.Map{"k": "v"})
t.AssertEQ(j.Get("no", 123.456).Float64(), float64(123.456))
t.AssertEQ(j.GetJson("no", g.Map{"k": "v"}).Get("k").String(), "v")
2020-03-19 22:56:12 +08:00
t.AssertEQ(j.GetJsons("no", g.Slice{
2019-06-12 22:14:31 +08:00
g.Map{"k1": "v1"},
g.Map{"k2": "v2"},
g.Map{"k3": "v3"},
})[0].Get("k1").String(), "v1")
2020-03-19 22:56:12 +08:00
t.AssertEQ(j.GetJsonMap("no", g.Map{
2019-06-12 22:14:31 +08:00
"m1": g.Map{"k1": "v1"},
"m2": g.Map{"k2": "v2"},
})["m2"].Get("k2").String(), "v2")
})
}
2019-06-12 22:14:31 +08:00
func Test_Convert(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j := gjson.New(`{"name":"gf"}`)
arr, err := j.ToXml()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "<name>gf</name>")
2019-06-12 22:14:31 +08:00
arr, err = j.ToXmlIndent()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "<name>gf</name>")
2019-06-12 22:14:31 +08:00
str, err := j.ToXmlString()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(str, "<name>gf</name>")
2019-06-12 22:14:31 +08:00
str, err = j.ToXmlIndentString()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(str, "<name>gf</name>")
2019-06-12 22:14:31 +08:00
arr, err = j.ToJsonIndent()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "{\n\t\"name\": \"gf\"\n}")
2019-06-12 22:14:31 +08:00
str, err = j.ToJsonIndentString()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "{\n\t\"name\": \"gf\"\n}")
2019-06-12 22:14:31 +08:00
arr, err = j.ToYaml()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "name: gf\n")
2019-06-12 22:14:31 +08:00
str, err = j.ToYamlString()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "name: gf\n")
2019-06-12 22:14:31 +08:00
arr, err = j.ToToml()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "name = \"gf\"\n")
2019-06-12 22:14:31 +08:00
str, err = j.ToTomlString()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(string(arr), "name = \"gf\"\n")
2019-06-12 22:14:31 +08:00
})
}
func Test_Convert2(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
name := struct {
Name string
}{}
j := gjson.New(`{"name":"gf","time":"2019-06-12"}`)
t.Assert(j.Interface().(g.Map)["name"], "gf")
t.Assert(j.Get("name1").Map(), nil)
t.Assert(j.GetJson("name1"), nil)
2020-03-19 22:56:12 +08:00
t.Assert(j.GetJsons("name1"), nil)
t.Assert(j.GetJsonMap("name1"), nil)
t.Assert(j.Contains("name1"), false)
t.Assert(j.Get("name1").IsNil(), true)
t.Assert(j.Get("name").IsNil(), false)
2020-03-19 22:56:12 +08:00
t.Assert(j.Len("name1"), -1)
t.Assert(j.Get("time").Time().Format("2006-01-02"), "2019-06-12")
t.Assert(j.Get("time").GTime().Format("Y-m-d"), "2019-06-12")
t.Assert(j.Get("time").Duration().String(), "0s")
2019-06-12 22:14:31 +08:00
err := j.Var().Scan(&name)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(name.Name, "gf")
2021-11-13 23:23:55 +08:00
// j.Dump()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-06-12 22:14:31 +08:00
j = gjson.New(`{"person":{"name":"gf"}}`)
err = j.Get("person").Scan(&name)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(name.Name, "gf")
2019-06-12 22:14:31 +08:00
j = gjson.New(`{"name":"gf""}`)
2021-11-13 23:23:55 +08:00
// j.Dump()
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-06-12 22:14:31 +08:00
j = gjson.New(`[1,2,3]`)
t.Assert(len(j.Var().Array()), 3)
2019-06-12 22:14:31 +08:00
})
}
2019-06-13 11:58:43 +08:00
func Test_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-13 11:58:43 +08:00
j := gjson.New(`{"name":"gf","time":"2019-06-12"}`)
j.SetViolenceCheck(true)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get(""), nil)
t.Assert(j.Get(".").Interface().(g.Map)["name"], "gf")
t.Assert(j.Get(".").Interface().(g.Map)["name1"], nil)
2019-06-13 11:58:43 +08:00
j.SetViolenceCheck(false)
t.Assert(j.Get(".").Interface().(g.Map)["name"], "gf")
2019-06-13 11:58:43 +08:00
err := j.Set("name", "gf1")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("name"), "gf1")
2019-06-13 11:58:43 +08:00
j = gjson.New(`[1,2,3]`)
err = j.Set("\"0\".1", 11)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("1"), 11)
2019-06-13 11:58:43 +08:00
j = gjson.New(`[1,2,3]`)
err = j.Set("11111111111111111111111", 11)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-13 11:58:43 +08:00
2019-06-19 09:06:52 +08:00
j = gjson.New(`[1,2,3]`)
2019-06-13 11:58:43 +08:00
err = j.Remove("1")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("0"), 1)
t.Assert(len(j.Var().Array()), 2)
2020-03-21 21:32:02 +08:00
j = gjson.New(`[1,2,3]`)
// If index 0 is delete, its next item will be at index 0.
t.Assert(j.Remove("0"), nil)
t.Assert(j.Remove("0"), nil)
t.Assert(j.Remove("0"), nil)
t.Assert(j.Get("0"), nil)
t.Assert(len(j.Var().Array()), 0)
2019-06-13 11:58:43 +08:00
j = gjson.New(`[1,2,3]`)
err = j.Remove("3")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("0"), 1)
t.Assert(len(j.Var().Array()), 3)
2019-06-13 11:58:43 +08:00
j = gjson.New(`[1,2,3]`)
err = j.Remove("0.3")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-08-10 23:03:35 +08:00
t.Assert(j.Get("0"), 1)
2019-06-13 11:58:43 +08:00
j = gjson.New(`[1,2,3]`)
err = j.Remove("0.a")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-21 21:32:02 +08:00
t.Assert(j.Get("0"), 1)
2019-06-13 11:58:43 +08:00
name := struct {
Name string
}{Name: "gf"}
j = gjson.New(name)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("Name"), "gf")
2019-06-13 11:58:43 +08:00
err = j.Remove("Name")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("Name"), nil)
2019-06-13 11:58:43 +08:00
err = j.Set("Name", "gf1")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("Name"), "gf1")
2019-06-13 11:58:43 +08:00
j = gjson.New(nil)
err = j.Remove("Name")
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("Name"), nil)
2019-06-13 11:58:43 +08:00
j = gjson.New(name)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("Name"), "gf")
2019-06-13 11:58:43 +08:00
err = j.Set("Name1", g.Map{"Name": "gf1"})
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("Name1").Interface().(g.Map)["Name"], "gf1")
2019-06-13 11:58:43 +08:00
err = j.Set("Name2", g.Slice{1, 2, 3})
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("Name2").Interface().(g.Slice)[0], 1)
2019-06-13 11:58:43 +08:00
err = j.Set("Name3", name)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("Name3").Interface().(g.Map)["Name"], "gf")
2019-06-13 11:58:43 +08:00
err = j.Set("Name4", &name)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("Name4").Interface().(g.Map)["Name"], "gf")
2019-06-13 11:58:43 +08:00
arr := [3]int{1, 2, 3}
err = j.Set("Name5", arr)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("Name5").Interface().(g.Array)[0], 1)
2019-06-13 11:58:43 +08:00
})
}
2019-07-18 18:59:49 +08:00
2021-01-19 14:26:17 +08:00
func TestJson_Var(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
data := []byte("[9223372036854775807, 9223372036854775806]")
array := gjson.New(data).Var().Array()
t.Assert(array, []uint64{9223372036854776000, 9223372036854776000})
})
gtest.C(t, func(t *gtest.T) {
data := []byte("[9223372036854775807, 9223372036854775806]")
array := gjson.NewWithOptions(data, gjson.Options{StrNumber: true}).Var().Array()
2021-01-19 14:26:17 +08:00
t.Assert(array, []uint64{9223372036854775807, 9223372036854775806})
})
}
func TestJson_IsNil(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-07-18 18:59:49 +08:00
j := gjson.New(nil)
2020-03-19 22:56:12 +08:00
t.Assert(j.IsNil(), true)
2019-07-18 18:59:49 +08:00
})
}
2021-07-08 22:44:16 +08:00
func TestJson_Set_With_Struct(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
v := gjson.New(g.Map{
"user1": g.Map{"name": "user1"},
"user2": g.Map{"name": "user2"},
"user3": g.Map{"name": "user3"},
})
user1 := v.GetJson("user1")
2021-12-20 00:32:23 +08:00
t.AssertNil(user1.Set("id", 111))
t.AssertNil(v.Set("user1", user1))
2021-07-08 22:44:16 +08:00
t.Assert(v.Get("user1.id"), 111)
})
}
func TestJson_Options(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type S struct {
Id int64
}
s := S{
Id: 53687091200,
}
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395) This pull request standardizes the use of the Go 1.18+ `any` type alias instead of `interface{}` throughout the codebase. The change improves code readability and aligns with modern Go best practices. The update touches many files, including core data structures, code generation templates, logging utilities, and test data, ensuring consistency across all usages. **Type alias migration to `any`:** * Replaced all instances of `interface{}` with `any` in core data structures such as `garray` and in generated model structs (e.g., `TableUser`, `User1`, `User2`) to modernize type usage. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) * Updated function signatures, method parameters, and return types from `interface{}` to `any` in various parts of the codebase, including code generation, service logic, and logging utilities (e.g., `mlog`). [[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74) [[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) [[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107) [[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121) [[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112) [[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36) [[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74) [[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96) **Generated code and templates:** * Adjusted generated files and code generation templates to output `any` instead of `interface{}` for relevant struct fields and function signatures, ensuring that new code generation aligns with the updated convention. [[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19) [[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18) [[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19) [[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19) [[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19) [[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55) [[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73) [[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41) **Container and utility updates:** * Refactored the `garray` container implementation and related constructors/methods to use `[]any` instead of `[]interface{}`, along with corresponding function signatures. [[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31) [[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52) [[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62) [[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86) [[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97) [[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114) [[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124) [[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143) [[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167) These changes collectively modernize the codebase and prepare it for future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
m := make(map[string]any)
t.AssertNil(gjson.DecodeTo(gjson.MustEncode(s), &m, gjson.Options{
StrNumber: false,
}))
t.Assert(fmt.Sprintf(`%v`, m["Id"]), `5.36870912e+10`)
t.AssertNil(gjson.DecodeTo(gjson.MustEncode(s), &m, gjson.Options{
StrNumber: true,
}))
t.Assert(fmt.Sprintf(`%v`, m["Id"]), `53687091200`)
})
}
2022-02-22 10:43:31 +08:00
// https://github.com/gogf/gf/issues/1617
func Test_Issue1617(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type MyJsonName struct {
F中文 int64 `json:"F中文"`
F英文 int64 `json:"F英文"`
F法文 int64 `json:"F法文"`
F西班牙语 int64 `json:"F西班牙语"`
}
jso := `{"F中文":1,"F英文":2,"F法文":3,"F西班牙语":4}`
var a MyJsonName
json, err := gjson.DecodeToJson(jso)
t.AssertNil(err)
err = json.Scan(&a)
t.AssertNil(err)
t.Assert(a, MyJsonName{
F中文: 1,
F英文: 2,
F法文: 3,
F西班牙语: 4,
})
})
}
2022-04-11 20:49:33 +08:00
// https://github.com/gogf/gf/issues/1747
func Test_Issue1747(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var j *gjson.Json
err := gconv.Struct(gvar.New("[1, 2, 336371793314971759]"), &j)
t.AssertNil(err)
t.Assert(j.Get("2"), `336371793314971759`)
})
}
// https://github.com/gogf/gf/issues/2520
func Test_Issue2520(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type test struct {
Unique *gvar.Var `json:"unique"`
}
t2 := test{Unique: gvar.New(gtime.Date())}
t.Assert(gjson.MustEncodeString(t2), gjson.New(t2).MustToJsonString())
})
}