1
0
mirror of https://github.com/json-iterator/go.git synced 2025-02-01 19:14:29 +02:00
json-iterator/jsoniter_map_test.go

161 lines
5.0 KiB
Go
Raw Normal View History

2016-12-11 00:38:07 +08:00
package jsoniter
import (
2017-06-13 16:58:53 +08:00
"encoding/json"
2017-06-06 00:09:33 +08:00
"math/big"
2017-06-06 23:27:00 +08:00
"testing"
2017-07-06 11:44:39 +08:00
"github.com/stretchr/testify/require"
"strings"
2016-12-11 00:38:07 +08:00
)
func Test_read_map(t *testing.T) {
2017-01-21 16:09:38 +08:00
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, `{"hello": "world"}`)
2016-12-11 00:38:07 +08:00
m := map[string]string{"1": "2"}
2017-01-09 17:47:21 +08:00
iter.ReadVal(&m)
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
2017-01-21 16:09:38 +08:00
should.Equal(map[string]string{"1": "2", "hello": "world"}, m)
2016-12-11 00:38:07 +08:00
}
2016-12-11 10:04:26 +08:00
func Test_read_map_of_interface(t *testing.T) {
2017-01-21 16:09:38 +08:00
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, `{"hello": "world"}`)
2016-12-11 10:04:26 +08:00
m := map[string]interface{}{"1": "2"}
2017-01-09 17:47:21 +08:00
iter.ReadVal(&m)
2017-01-21 16:09:38 +08:00
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
2017-06-17 10:21:37 +08:00
iter = ParseString(ConfigDefault, `{"hello": "world"}`)
2017-01-21 16:09:38 +08:00
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
2016-12-11 10:04:26 +08:00
}
2017-01-31 23:16:40 +08:00
func Test_map_wrapper_any_get_all(t *testing.T) {
should := require.New(t)
2017-06-06 23:27:00 +08:00
any := Wrap(map[string][]int{"Field1": {1, 2}})
2017-01-31 23:16:40 +08:00
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
2017-07-06 11:44:39 +08:00
should.Contains(any.Keys(), "Field1")
// map write to
stream := NewStream(ConfigDefault, nil, 0)
any.WriteTo(stream)
// TODO cannot pass
//should.Equal(string(stream.buf), "")
2017-01-31 23:16:40 +08:00
}
2017-01-09 20:51:09 +08:00
func Test_write_val_map(t *testing.T) {
should := require.New(t)
val := map[string]string{"1": "2"}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
}
2017-04-16 14:05:08 +08:00
func Test_slice_of_map(t *testing.T) {
should := require.New(t)
val := []map[string]string{{"1": "2"}}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`[{"1":"2"}]`, str)
val = []map[string]string{}
should.Nil(UnmarshalFromString(str, &val))
should.Equal("2", val[0]["1"])
2017-06-05 23:01:00 +08:00
}
2017-06-05 23:53:48 +08:00
func Test_encode_int_key_map(t *testing.T) {
2017-06-05 23:01:00 +08:00
should := require.New(t)
val := map[int]string{1: "2"}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
2017-06-05 23:53:48 +08:00
}
func Test_decode_int_key_map(t *testing.T) {
should := require.New(t)
var val map[int]string
should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
should.Equal(map[int]string{1: "2"}, val)
2017-06-06 00:09:33 +08:00
}
func Test_encode_TextMarshaler_key_map(t *testing.T) {
should := require.New(t)
2017-06-21 00:26:18 +08:00
f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
val := map[*big.Float]string{f: "2"}
str, err := MarshalToString(val)
2017-06-06 00:09:33 +08:00
should.Nil(err)
2017-06-21 00:26:18 +08:00
should.Equal(`{"1":"2"}`, str)
2017-06-06 00:09:33 +08:00
}
func Test_decode_TextMarshaler_key_map(t *testing.T) {
should := require.New(t)
var val map[*big.Float]string
should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"1":"2"}`, str)
2017-06-06 23:27:00 +08:00
}
func Test_map_key_with_escaped_char(t *testing.T) {
type Ttest struct {
Map map[string]string
}
var jsonBytes = []byte(`
{
"Map":{
"k\"ey": "val"
}
}`)
should := require.New(t)
{
var obj Ttest
should.Nil(json.Unmarshal(jsonBytes, &obj))
2017-06-13 16:58:53 +08:00
should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
}
{
var obj Ttest
should.Nil(Unmarshal(jsonBytes, &obj))
2017-06-13 16:58:53 +08:00
should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
}
}
2017-06-16 16:46:30 +08:00
func Test_encode_map_with_sorted_keys(t *testing.T) {
should := require.New(t)
2017-06-17 21:11:23 +08:00
m := map[string]interface{}{
"3": 3,
"1": 1,
"2": 2,
}
2017-06-16 16:46:30 +08:00
bytes, err := json.Marshal(m)
should.Nil(err)
output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
should.Nil(err)
should.Equal(string(bytes), output)
}
2017-07-07 17:07:00 +08:00
func Test_encode_map_uint_keys(t *testing.T) {
should := require.New(t)
m := map[uint64]interface{}{
uint64(1): "a",
uint64(2): "a",
uint64(4): "a",
}
bytes, err := json.Marshal(m)
should.Nil(err)
output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
2017-07-09 11:12:37 +08:00
should.Nil(err)
2017-07-07 17:07:00 +08:00
should.Equal(string(bytes), output)
}
2017-07-07 17:07:00 +08:00
func Test_read_map_with_reader(t *testing.T) {
should := require.New(t)
input := `{"branch":"beta","change_log":"add the rows{10}","channel":"fros","create_time":"2017-06-13 16:39:08","firmware_list":"","md5":"80dee2bf7305bcf179582088e29fd7b9","note":{"CoreServices":{"md5":"d26975c0a8c7369f70ed699f2855cc2e","package_name":"CoreServices","version_code":"76","version_name":"1.0.76"},"FrDaemon":{"md5":"6b1f0626673200bc2157422cd2103f5d","package_name":"FrDaemon","version_code":"390","version_name":"1.0.390"},"FrGallery":{"md5":"90d767f0f31bcd3c1d27281ec979ba65","package_name":"FrGallery","version_code":"349","version_name":"1.0.349"},"FrLocal":{"md5":"f15a215b2c070a80a01f07bde4f219eb","package_name":"FrLocal","version_code":"791","version_name":"1.0.791"}},"pack_region_urls":{"CN":"https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip","default":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip","local":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip"},"pack_version":"1.5.3.344.393","pack_version_code":393,"region":"all","release_flag":0,"revision":62,"size":38966875,"status":3}`
reader := strings.NewReader(input)
decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(reader)
m1 := map[string]interface{}{}
should.Nil(decoder.Decode(&m1))
m2 := map[string]interface{}{}
should.Nil(json.Unmarshal([]byte(input), &m2))
should.Equal(m2, m1)
should.Equal("1.0.76", m1["note"].(map[string]interface{})["CoreServices"].(map[string]interface{})["version_name"])
2017-07-07 17:07:00 +08:00
}