1 package misc_tests
2
3 import (
4 "encoding/json"
5 "github.com/json-iterator/go"
6 "github.com/stretchr/testify/require"
7 "strings"
8 "testing"
9 )
10
11 func Test_jsoniter_RawMessage(t *testing.T) {
12 should := require.New(t)
13 var data jsoniter.RawMessage
14 should.Nil(jsoniter.Unmarshal([]byte(`[1,2,3]`), &data))
15 should.Equal(`[1,2,3]`, string(data))
16 str, err := jsoniter.MarshalToString(data)
17 should.Nil(err)
18 should.Equal(`[1,2,3]`, str)
19 }
20
21 func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
22 should := require.New(t)
23 type RawMap map[string]*jsoniter.RawMessage
24 value := jsoniter.RawMessage("[]")
25 rawMap := RawMap{"hello": &value}
26 output, err := jsoniter.MarshalToString(rawMap)
27 should.Nil(err)
28 should.Equal(`{"hello":[]}`, output)
29 }
30
31 func Test_marshal_invalid_json_raw_message(t *testing.T) {
32 type A struct {
33 Raw json.RawMessage `json:"raw"`
34 }
35 message := []byte(`{}`)
36
37 a := A{}
38 should := require.New(t)
39 should.Nil(jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
40 aout, aouterr := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&a)
41 should.Equal(`{"raw":null}`, string(aout))
42 should.Nil(aouterr)
43 }
44
45 func Test_marshal_nil_json_raw_message(t *testing.T) {
46 type A struct {
47 Nil1 jsoniter.RawMessage `json:"raw1"`
48 Nil2 json.RawMessage `json:"raw2"`
49 }
50
51 a := A{}
52 should := require.New(t)
53 aout, aouterr := jsoniter.Marshal(&a)
54 should.Equal(`{"raw1":null,"raw2":null}`, string(aout))
55 should.Nil(aouterr)
56
57 a.Nil1 = []byte(`Any`)
58 a.Nil2 = []byte(`Any`)
59 should.Nil(jsoniter.Unmarshal(aout, &a))
60 should.Nil(a.Nil1)
61 should.Nil(a.Nil2)
62 }
63
64 func Test_raw_message_memory_not_copied_issue(t *testing.T) {
65 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"}}`
66 type IteratorObject struct {
67 Name *string `json:"name"`
68 BundleId *string `json:"bundle_id"`
69 AppCategory *string `json:"app_category"`
70 AppPlatform *string `json:"app_platform"`
71 BudgetDay *float32 `json:"budget_day"`
72 BiddingMax *float32 `json:"bidding_max"`
73 BiddingMin *float32 `json:"bidding_min"`
74 BiddingType *string `json:"bidding_type"`
75 Freq *jsoniter.RawMessage `json:"freq"`
76 Targeting *jsoniter.RawMessage `json:"targeting"`
77 Url *jsoniter.RawMessage `json:"url"`
78 Speed *int `json:"speed" db:"speed"`
79 }
80
81 obj := &IteratorObject{}
82 decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
83 err := decoder.Decode(obj)
84 should := require.New(t)
85 should.Nil(err)
86 should.Equal(`{"open":true,"type":"day","num":100}`, string(*obj.Freq))
87 }
88
View as plain text