jsoniter_raw_message_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package misc_tests
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/require"
  5. "strings"
  6. "testing"
  7. "github.com/json-iterator/go"
  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_raw_message_memory_not_copied_issue(t *testing.T) {
  40. 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"}}`
  41. type IteratorObject struct {
  42. Name *string `json:"name"`
  43. BundleId *string `json:"bundle_id"`
  44. AppCategory *string `json:"app_category"`
  45. AppPlatform *string `json:"app_platform"`
  46. BudgetDay *float32 `json:"budget_day"`
  47. BiddingMax *float32 `json:"bidding_max"`
  48. BiddingMin *float32 `json:"bidding_min"`
  49. BiddingType *string `json:"bidding_type"`
  50. Freq *jsoniter.RawMessage `json:"freq"`
  51. Targeting *jsoniter.RawMessage `json:"targeting"`
  52. Url *jsoniter.RawMessage `json:"url"`
  53. Speed *int `json:"speed" db:"speed"`
  54. }
  55. obj := &IteratorObject{}
  56. decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
  57. err := decoder.Decode(obj)
  58. should := require.New(t)
  59. should.Nil(err)
  60. should.Equal(`{"open":true,"type":"day","num":100}`, string(*obj.Freq))
  61. }