jsoniter_map_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func Test_read_map(t *testing.T) {
  9. should := require.New(t)
  10. iter := ParseString(ConfigDefault, `{"hello": "world"}`)
  11. m := map[string]string{"1": "2"}
  12. iter.ReadVal(&m)
  13. copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
  14. should.Equal(map[string]string{"1": "2", "hello": "world"}, m)
  15. }
  16. func Test_read_map_of_interface(t *testing.T) {
  17. should := require.New(t)
  18. iter := ParseString(ConfigDefault, `{"hello": "world"}`)
  19. m := map[string]interface{}{"1": "2"}
  20. iter.ReadVal(&m)
  21. should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
  22. iter = ParseString(ConfigDefault, `{"hello": "world"}`)
  23. should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
  24. }
  25. func Test_map_wrapper_any_get_all(t *testing.T) {
  26. should := require.New(t)
  27. any := Wrap(map[string][]int{"Field1": {1, 2}})
  28. should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
  29. should.Contains(any.Keys(), "Field1")
  30. should.Equal(any.GetObject()["Field1"].ToInt(), 1)
  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. }