jsoniter_raw_message_test.go 698 B

1234567891011121314151617181920212223242526272829
  1. package jsoniter
  2. import (
  3. "testing"
  4. "encoding/json"
  5. "github.com/json-iterator/go/require"
  6. )
  7. func Test_json_RawMessage(t *testing.T) {
  8. should := require.New(t)
  9. var data json.RawMessage
  10. should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
  11. should.Equal(`[1,2,3]`, string(data))
  12. str, err := MarshalToString(data)
  13. should.Nil(err)
  14. should.Equal(`[1,2,3]`, str)
  15. }
  16. func Test_json_RawMessage_in_struct(t *testing.T) {
  17. type TestObject struct {
  18. Field1 string
  19. Field2 json.RawMessage
  20. }
  21. should := require.New(t)
  22. var data TestObject
  23. should.Nil(Unmarshal([]byte(`{"field1": "hello", "field2": [1,2,3]}`), &data))
  24. should.Equal(` [1,2,3]`, string(data.Field2))
  25. should.Equal(`hello`, data.Field1)
  26. }