jsoniter_map_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. "strings"
  8. )
  9. func Test_read_map(t *testing.T) {
  10. should := require.New(t)
  11. iter := ParseString(ConfigDefault, `{"hello": "world"}`)
  12. m := map[string]string{"1": "2"}
  13. iter.ReadVal(&m)
  14. copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
  15. should.Equal(map[string]string{"1": "2", "hello": "world"}, m)
  16. }
  17. func Test_read_map_of_interface(t *testing.T) {
  18. should := require.New(t)
  19. iter := ParseString(ConfigDefault, `{"hello": "world"}`)
  20. m := map[string]interface{}{"1": "2"}
  21. iter.ReadVal(&m)
  22. should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
  23. iter = ParseString(ConfigDefault, `{"hello": "world"}`)
  24. should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
  25. }
  26. func Test_map_wrapper_any_get_all(t *testing.T) {
  27. should := require.New(t)
  28. any := Wrap(map[string][]int{"Field1": {1, 2}})
  29. should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
  30. should.Contains(any.Keys(), "Field1")
  31. // map write to
  32. stream := NewStream(ConfigDefault, nil, 0)
  33. any.WriteTo(stream)
  34. // TODO cannot pass
  35. //should.Equal(string(stream.buf), "")
  36. }
  37. func Test_write_val_map(t *testing.T) {
  38. should := require.New(t)
  39. val := map[string]string{"1": "2"}
  40. str, err := MarshalToString(val)
  41. should.Nil(err)
  42. should.Equal(`{"1":"2"}`, str)
  43. }
  44. func Test_slice_of_map(t *testing.T) {
  45. should := require.New(t)
  46. val := []map[string]string{{"1": "2"}}
  47. str, err := MarshalToString(val)
  48. should.Nil(err)
  49. should.Equal(`[{"1":"2"}]`, str)
  50. val = []map[string]string{}
  51. should.Nil(UnmarshalFromString(str, &val))
  52. should.Equal("2", val[0]["1"])
  53. }
  54. func Test_encode_int_key_map(t *testing.T) {
  55. should := require.New(t)
  56. val := map[int]string{1: "2"}
  57. str, err := MarshalToString(val)
  58. should.Nil(err)
  59. should.Equal(`{"1":"2"}`, str)
  60. }
  61. func Test_decode_int_key_map(t *testing.T) {
  62. should := require.New(t)
  63. var val map[int]string
  64. should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
  65. should.Equal(map[int]string{1: "2"}, val)
  66. }
  67. func Test_encode_TextMarshaler_key_map(t *testing.T) {
  68. should := require.New(t)
  69. f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
  70. val := map[*big.Float]string{f: "2"}
  71. str, err := MarshalToString(val)
  72. should.Nil(err)
  73. should.Equal(`{"1":"2"}`, str)
  74. }
  75. func Test_decode_TextMarshaler_key_map(t *testing.T) {
  76. should := require.New(t)
  77. var val map[*big.Float]string
  78. should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
  79. str, err := MarshalToString(val)
  80. should.Nil(err)
  81. should.Equal(`{"1":"2"}`, str)
  82. }
  83. func Test_map_key_with_escaped_char(t *testing.T) {
  84. type Ttest struct {
  85. Map map[string]string
  86. }
  87. var jsonBytes = []byte(`
  88. {
  89. "Map":{
  90. "k\"ey": "val"
  91. }
  92. }`)
  93. should := require.New(t)
  94. {
  95. var obj Ttest
  96. should.Nil(json.Unmarshal(jsonBytes, &obj))
  97. should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
  98. }
  99. {
  100. var obj Ttest
  101. should.Nil(Unmarshal(jsonBytes, &obj))
  102. should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
  103. }
  104. }
  105. func Test_encode_map_with_sorted_keys(t *testing.T) {
  106. should := require.New(t)
  107. m := map[string]interface{}{
  108. "3": 3,
  109. "1": 1,
  110. "2": 2,
  111. }
  112. bytes, err := json.Marshal(m)
  113. should.Nil(err)
  114. output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
  115. should.Nil(err)
  116. should.Equal(string(bytes), output)
  117. }
  118. func Test_encode_map_uint_keys(t *testing.T) {
  119. should := require.New(t)
  120. m := map[uint64]interface{}{
  121. uint64(1): "a",
  122. uint64(2): "a",
  123. uint64(4): "a",
  124. }
  125. bytes, err := json.Marshal(m)
  126. should.Nil(err)
  127. output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
  128. should.Nil(err)
  129. should.Equal(string(bytes), output)
  130. }
  131. func Test_read_map_with_reader(t *testing.T) {
  132. should := require.New(t)
  133. 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}`
  134. reader := strings.NewReader(input)
  135. decoder := ConfigCompatibleWithStandardLibrary.NewDecoder(reader)
  136. m1 := map[string]interface{}{}
  137. should.Nil(decoder.Decode(&m1))
  138. m2 := map[string]interface{}{}
  139. should.Nil(json.Unmarshal([]byte(input), &m2))
  140. should.Equal(m2, m1)
  141. should.Equal("1.0.76", m1["note"].(map[string]interface{})["CoreServices"].(map[string]interface{})["version_name"])
  142. }