jsoniter_fixed_array_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/require"
  5. "testing"
  6. )
  7. func Test_encode_fixed_array(t *testing.T) {
  8. should := require.New(t)
  9. type FixedArray [2]float64
  10. fixed := FixedArray{0.1, 1.0}
  11. output, err := MarshalToString(fixed)
  12. should.Nil(err)
  13. should.Equal("[0.1,1]", output)
  14. }
  15. func Test_encode_fixed_array_empty(t *testing.T) {
  16. should := require.New(t)
  17. type FixedArray [0]float64
  18. fixed := FixedArray{}
  19. output, err := MarshalToString(fixed)
  20. should.Nil(err)
  21. should.Equal("[]", output)
  22. }
  23. func Test_encode_fixed_array_of_map(t *testing.T) {
  24. should := require.New(t)
  25. type FixedArray [2]map[string]string
  26. fixed := FixedArray{map[string]string{"1": "2"}, map[string]string{"3": "4"}}
  27. output, err := MarshalToString(fixed)
  28. should.Nil(err)
  29. should.Equal(`[{"1":"2"},{"3":"4"}]`, output)
  30. }
  31. func Test_decode_fixed_array(t *testing.T) {
  32. should := require.New(t)
  33. type FixedArray [2]float64
  34. var fixed FixedArray
  35. should.Nil(json.Unmarshal([]byte("[1,2,3]"), &fixed))
  36. should.Equal(float64(1), fixed[0])
  37. should.Equal(float64(2), fixed[1])
  38. should.Nil(Unmarshal([]byte("[1,2,3]"), &fixed))
  39. should.Equal(float64(1), fixed[0])
  40. should.Equal(float64(2), fixed[1])
  41. }