config_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/json-iterator/go"
  6. "encoding/json"
  7. )
  8. func Test_use_number_for_unmarshal(t *testing.T) {
  9. should := require.New(t)
  10. api := jsoniter.Config{UseNumber: true}.Froze()
  11. var obj interface{}
  12. should.Nil(api.UnmarshalFromString("123", &obj))
  13. should.Equal(json.Number("123"), obj)
  14. }
  15. func Test_customize_float_marshal(t *testing.T) {
  16. should := require.New(t)
  17. json := jsoniter.Config{MarshalFloatWith6Digits: true}.Froze()
  18. str, err := json.MarshalToString(float32(1.23456789))
  19. should.Nil(err)
  20. should.Equal("1.234568", str)
  21. }
  22. func Test_customize_tag_key(t *testing.T) {
  23. type TestObject struct {
  24. Field string `orm:"field"`
  25. }
  26. should := require.New(t)
  27. json := jsoniter.Config{TagKey: "orm"}.Froze()
  28. str, err := json.MarshalToString(TestObject{"hello"})
  29. should.Nil(err)
  30. should.Equal(`{"field":"hello"}`, str)
  31. }
  32. func Test_read_large_number_as_interface(t *testing.T) {
  33. should := require.New(t)
  34. var val interface{}
  35. err := jsoniter.Config{UseNumber: true}.Froze().UnmarshalFromString(`123456789123456789123456789`, &val)
  36. should.Nil(err)
  37. output, err := jsoniter.MarshalToString(val)
  38. should.Nil(err)
  39. should.Equal(`123456789123456789123456789`, output)
  40. }