jsoniter_map_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package jsoniter
  2. import (
  3. "github.com/json-iterator/go/require"
  4. "math/big"
  5. "testing"
  6. "encoding/json"
  7. )
  8. func Test_read_map(t *testing.T) {
  9. should := require.New(t)
  10. iter := ParseString(`{"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(`{"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(`{"hello": "world"}`)
  23. should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
  24. }
  25. func Test_wrap_map(t *testing.T) {
  26. should := require.New(t)
  27. any := Wrap(map[string]string{"Field1": "hello"})
  28. should.Equal("hello", any.Get("Field1").ToString())
  29. any = Wrap(map[string]string{"Field1": "hello"})
  30. should.Equal(1, any.Size())
  31. any = Wrap(map[string]string{"Field1": "hello"})
  32. vals := map[string]string{}
  33. var k string
  34. var v Any
  35. for next, hasNext := any.IterateObject(); hasNext; {
  36. k, v, hasNext = next()
  37. if v.ValueType() == String {
  38. vals[k] = v.ToString()
  39. }
  40. }
  41. should.Equal(map[string]string{"Field1": "hello"}, vals)
  42. }
  43. func Test_map_wrapper_any_get_all(t *testing.T) {
  44. should := require.New(t)
  45. any := Wrap(map[string][]int{"Field1": {1, 2}})
  46. should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
  47. }
  48. func Test_write_val_map(t *testing.T) {
  49. should := require.New(t)
  50. val := map[string]string{"1": "2"}
  51. str, err := MarshalToString(val)
  52. should.Nil(err)
  53. should.Equal(`{"1":"2"}`, str)
  54. }
  55. func Test_slice_of_map(t *testing.T) {
  56. should := require.New(t)
  57. val := []map[string]string{{"1": "2"}}
  58. str, err := MarshalToString(val)
  59. should.Nil(err)
  60. should.Equal(`[{"1":"2"}]`, str)
  61. val = []map[string]string{}
  62. should.Nil(UnmarshalFromString(str, &val))
  63. should.Equal("2", val[0]["1"])
  64. }
  65. func Test_encode_int_key_map(t *testing.T) {
  66. should := require.New(t)
  67. val := map[int]string{1: "2"}
  68. str, err := MarshalToString(val)
  69. should.Nil(err)
  70. should.Equal(`{"1":"2"}`, str)
  71. }
  72. func Test_decode_int_key_map(t *testing.T) {
  73. should := require.New(t)
  74. var val map[int]string
  75. should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
  76. should.Equal(map[int]string{1: "2"}, val)
  77. }
  78. func Test_encode_TextMarshaler_key_map(t *testing.T) {
  79. should := require.New(t)
  80. f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
  81. val := map[*big.Float]string{f: "2"}
  82. str, err := MarshalToString(val)
  83. should.Nil(err)
  84. should.Equal(`{"1":"2"}`, str)
  85. }
  86. func Test_decode_TextMarshaler_key_map(t *testing.T) {
  87. should := require.New(t)
  88. var val map[*big.Float]string
  89. should.Nil(UnmarshalFromString(`{"1":"2"}`, &val))
  90. str, err := MarshalToString(val)
  91. should.Nil(err)
  92. should.Equal(`{"1":"2"}`, str)
  93. }
  94. func Test_map_key_with_escaped_char(t *testing.T) {
  95. type Ttest struct {
  96. Map map[string]string
  97. }
  98. var jsonBytes = []byte(`
  99. {
  100. "Map":{
  101. "k\"ey": "val"
  102. }
  103. }`)
  104. should := require.New(t)
  105. {
  106. var obj Ttest
  107. should.Nil(json.Unmarshal(jsonBytes, &obj))
  108. should.Equal(map[string]string{"k\"ey":"val"}, obj.Map)
  109. }
  110. {
  111. var obj Ttest
  112. should.Nil(Unmarshal(jsonBytes, &obj))
  113. should.Equal(map[string]string{"k\"ey":"val"}, obj.Map)
  114. }
  115. }