jsoniter_fixed_array_test.go 992 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/json-iterator/go/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_of_map(t *testing.T) {
  16. should := require.New(t)
  17. type FixedArray [2]map[string]string
  18. fixed := FixedArray{map[string]string{"1": "2"}, map[string]string{"3": "4"}}
  19. output, err := MarshalToString(fixed)
  20. should.Nil(err)
  21. should.Equal(`[{"1":"2"},{"3":"4"}]`, output)
  22. }
  23. func Test_decode_fixed_array(t *testing.T) {
  24. should := require.New(t)
  25. type FixedArray [2]float64
  26. var fixed FixedArray
  27. should.Nil(json.Unmarshal([]byte("[1,2,3]"), &fixed))
  28. should.Equal(float64(1), fixed[0])
  29. should.Equal(float64(2), fixed[1])
  30. should.Nil(Unmarshal([]byte("[1,2,3]"), &fixed))
  31. should.Equal(float64(1), fixed[0])
  32. should.Equal(float64(2), fixed[1])
  33. }