jsoniter_map_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // map write to
  31. stream := NewStream(ConfigDefault, nil, 0)
  32. any.WriteTo(stream)
  33. // TODO cannot pass
  34. //should.Equal(string(stream.buf), "")
  35. }
  36. func Test_write_val_map(t *testing.T) {
  37. should := require.New(t)
  38. val := map[string]string{"1": "2"}
  39. str, err := MarshalToString(val)
  40. should.Nil(err)
  41. should.Equal(`{"1":"2"}`, str)
  42. }
  43. func Test_slice_of_map(t *testing.T) {
  44. should := require.New(t)
  45. val := []map[string]string{{"1": "2"}}
  46. str, err := MarshalToString(val)
  47. should.Nil(err)
  48. should.Equal(`[{"1":"2"}]`, str)
  49. val = []map[string]string{}
  50. should.Nil(UnmarshalFromString(str, &val))
  51. should.Equal("2", val[0]["1"])
  52. }
  53. func Test_encode_int_key_map(t *testing.T) {
  54. should := require.New(t)
  55. val := map[int]string{1: "2"}
  56. str, err := MarshalToString(val)
  57. should.Nil(err)
  58. should.Equal(`{"1":"2"}`, str)
  59. }
  60. func Test_decode_int_key_map(t *testing.T) {
  61. should := require.New(t)
  62. var val map[int]string
  63. should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
  64. should.Equal(map[int]string{1: "2"}, val)
  65. }
  66. func Test_encode_TextMarshaler_key_map(t *testing.T) {
  67. should := require.New(t)
  68. f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
  69. val := map[*big.Float]string{f: "2"}
  70. str, err := MarshalToString(val)
  71. should.Nil(err)
  72. should.Equal(`{"1":"2"}`, str)
  73. }
  74. func Test_decode_TextMarshaler_key_map(t *testing.T) {
  75. should := require.New(t)
  76. var val map[*big.Float]string
  77. should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
  78. str, err := MarshalToString(val)
  79. should.Nil(err)
  80. should.Equal(`{"1":"2"}`, str)
  81. }
  82. func Test_map_key_with_escaped_char(t *testing.T) {
  83. type Ttest struct {
  84. Map map[string]string
  85. }
  86. var jsonBytes = []byte(`
  87. {
  88. "Map":{
  89. "k\"ey": "val"
  90. }
  91. }`)
  92. should := require.New(t)
  93. {
  94. var obj Ttest
  95. should.Nil(json.Unmarshal(jsonBytes, &obj))
  96. should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
  97. }
  98. {
  99. var obj Ttest
  100. should.Nil(Unmarshal(jsonBytes, &obj))
  101. should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
  102. }
  103. }
  104. func Test_encode_map_with_sorted_keys(t *testing.T) {
  105. should := require.New(t)
  106. m := map[string]interface{}{
  107. "3": 3,
  108. "1": 1,
  109. "2": 2,
  110. }
  111. bytes, err := json.Marshal(m)
  112. should.Nil(err)
  113. output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
  114. should.Nil(err)
  115. should.Equal(string(bytes), output)
  116. }
  117. func Test_encode_map_uint_keys(t *testing.T) {
  118. should := require.New(t)
  119. m := map[uint64]interface{}{
  120. uint64(1): "a",
  121. uint64(2): "a",
  122. uint64(4): "a",
  123. }
  124. bytes, err := json.Marshal(m)
  125. should.Nil(err)
  126. output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
  127. should.Nil(err)
  128. should.Equal(string(bytes), output)
  129. }