config_test.go 901 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. }