jsoniter_raw_message_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package misc_tests
  2. import (
  3. "encoding/json"
  4. "github.com/json-iterator/go"
  5. "github.com/stretchr/testify/require"
  6. "strings"
  7. "testing"
  8. )
  9. func Test_jsoniter_RawMessage(t *testing.T) {
  10. should := require.New(t)
  11. var data jsoniter.RawMessage
  12. should.Nil(jsoniter.Unmarshal([]byte(`[1,2,3]`), &data))
  13. should.Equal(`[1,2,3]`, string(data))
  14. str, err := jsoniter.MarshalToString(data)
  15. should.Nil(err)
  16. should.Equal(`[1,2,3]`, str)
  17. }
  18. func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
  19. should := require.New(t)
  20. type RawMap map[string]*jsoniter.RawMessage
  21. value := jsoniter.RawMessage("[]")
  22. rawMap := RawMap{"hello": &value}
  23. output, err := jsoniter.MarshalToString(rawMap)
  24. should.Nil(err)
  25. should.Equal(`{"hello":[]}`, output)
  26. }
  27. func Test_marshal_invalid_json_raw_message(t *testing.T) {
  28. type A struct {
  29. Raw json.RawMessage `json:"raw"`
  30. }
  31. message := []byte(`{}`)
  32. a := A{}
  33. should := require.New(t)
  34. should.Nil(jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
  35. aout, aouterr := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&a)
  36. should.Equal(`{"raw":null}`, string(aout))
  37. should.Nil(aouterr)
  38. }
  39. func Test_marshal_nil_json_raw_message(t *testing.T) {
  40. type A struct {
  41. Nil1 jsoniter.RawMessage `json:"raw1"`
  42. Nil2 json.RawMessage `json:"raw2"`
  43. }
  44. a := A{}
  45. should := require.New(t)
  46. aout, aouterr := jsoniter.Marshal(&a)
  47. should.Equal(`{"raw1":null,"raw2":null}`, string(aout))
  48. should.Nil(aouterr)
  49. a.Nil1 = []byte(`Any`)
  50. a.Nil2 = []byte(`Any`)
  51. should.Nil(jsoniter.Unmarshal(aout, &a))
  52. should.Nil(a.Nil1)
  53. should.Nil(a.Nil2)
  54. }
  55. func Test_raw_message_memory_not_copied_issue(t *testing.T) {
  56. jsonStream := `{"name":"xxxxx","bundle_id":"com.zonst.majiang","app_platform":"ios","app_category":"100103", "budget_day":1000,"bidding_min":1,"bidding_max":2,"bidding_type":"CPM", "freq":{"open":true,"type":"day","num":100},"speed":1, "targeting":{"vendor":{"open":true,"list":["zonst"]}, "geo_code":{"open":true,"list":["156110100"]},"app_category":{"open":true,"list":["100101"]}, "day_parting":{"open":true,"list":["100409","100410"]},"device_type":{"open":true,"list":["ipad"]}, "os_version":{"open":true,"list":[10]},"carrier":{"open":true,"list":["mobile"]}, "network":{"open":true,"list":["4G"]}},"url":{"tracking_imp_url":"http://www.baidu.com", "tracking_clk_url":"http://www.baidu.com","jump_url":"http://www.baidu.com","deep_link_url":"http://www.baidu.com"}}`
  57. type IteratorObject struct {
  58. Name *string `json:"name"`
  59. BundleId *string `json:"bundle_id"`
  60. AppCategory *string `json:"app_category"`
  61. AppPlatform *string `json:"app_platform"`
  62. BudgetDay *float32 `json:"budget_day"`
  63. BiddingMax *float32 `json:"bidding_max"`
  64. BiddingMin *float32 `json:"bidding_min"`
  65. BiddingType *string `json:"bidding_type"`
  66. Freq *jsoniter.RawMessage `json:"freq"`
  67. Targeting *jsoniter.RawMessage `json:"targeting"`
  68. Url *jsoniter.RawMessage `json:"url"`
  69. Speed *int `json:"speed" db:"speed"`
  70. }
  71. obj := &IteratorObject{}
  72. decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
  73. err := decoder.Decode(obj)
  74. should := require.New(t)
  75. should.Nil(err)
  76. should.Equal(`{"open":true,"type":"day","num":100}`, string(*obj.Freq))
  77. }