2018-02-14 02:58:59 +02:00
|
|
|
package misc_tests
|
2017-06-19 15:24:59 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-02-24 16:04:41 +02:00
|
|
|
"github.com/json-iterator/go"
|
2017-07-07 03:13:25 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-09-21 14:18:45 +02:00
|
|
|
"strings"
|
2017-06-19 17:43:53 +02:00
|
|
|
"testing"
|
2017-06-19 15:24:59 +02:00
|
|
|
)
|
|
|
|
|
2017-06-19 17:10:20 +02:00
|
|
|
func Test_jsoniter_RawMessage(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2018-02-14 02:58:59 +02:00
|
|
|
var data jsoniter.RawMessage
|
|
|
|
should.Nil(jsoniter.Unmarshal([]byte(`[1,2,3]`), &data))
|
2017-06-19 17:10:20 +02:00
|
|
|
should.Equal(`[1,2,3]`, string(data))
|
2018-02-14 02:58:59 +02:00
|
|
|
str, err := jsoniter.MarshalToString(data)
|
2017-06-19 17:10:20 +02:00
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(`[1,2,3]`, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2018-02-14 02:58:59 +02:00
|
|
|
type RawMap map[string]*jsoniter.RawMessage
|
|
|
|
value := jsoniter.RawMessage("[]")
|
2017-06-19 17:10:20 +02:00
|
|
|
rawMap := RawMap{"hello": &value}
|
2018-02-14 02:58:59 +02:00
|
|
|
output, err := jsoniter.MarshalToString(rawMap)
|
2017-06-19 17:10:20 +02:00
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(`{"hello":[]}`, output)
|
2017-06-19 17:43:53 +02:00
|
|
|
}
|
2017-09-14 17:54:40 +02:00
|
|
|
|
|
|
|
func Test_marshal_invalid_json_raw_message(t *testing.T) {
|
|
|
|
type A struct {
|
|
|
|
Raw json.RawMessage `json:"raw"`
|
|
|
|
}
|
|
|
|
message := []byte(`{}`)
|
|
|
|
|
|
|
|
a := A{}
|
|
|
|
should := require.New(t)
|
2018-02-14 02:58:59 +02:00
|
|
|
should.Nil(jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
|
|
|
|
aout, aouterr := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&a)
|
2017-09-14 17:54:40 +02:00
|
|
|
should.Equal(`{"raw":null}`, string(aout))
|
|
|
|
should.Nil(aouterr)
|
|
|
|
}
|
2017-09-21 14:18:45 +02:00
|
|
|
|
2020-07-21 11:07:23 +02:00
|
|
|
func Test_marshal_nil_json_raw_message(t *testing.T) {
|
|
|
|
type A struct {
|
|
|
|
Nil1 jsoniter.RawMessage `json:"raw1"`
|
|
|
|
Nil2 json.RawMessage `json:"raw2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
a := A{}
|
|
|
|
should := require.New(t)
|
|
|
|
aout, aouterr := jsoniter.Marshal(&a)
|
|
|
|
should.Equal(`{"raw1":null,"raw2":null}`, string(aout))
|
|
|
|
should.Nil(aouterr)
|
|
|
|
|
|
|
|
a.Nil1 = []byte(`Any`)
|
|
|
|
a.Nil2 = []byte(`Any`)
|
|
|
|
should.Nil(jsoniter.Unmarshal(aout, &a))
|
|
|
|
should.Nil(a.Nil1)
|
|
|
|
should.Nil(a.Nil2)
|
|
|
|
}
|
|
|
|
|
2017-09-21 14:18:45 +02:00
|
|
|
func Test_raw_message_memory_not_copied_issue(t *testing.T) {
|
|
|
|
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"}}`
|
|
|
|
type IteratorObject struct {
|
2018-02-24 16:04:41 +02:00
|
|
|
Name *string `json:"name"`
|
|
|
|
BundleId *string `json:"bundle_id"`
|
|
|
|
AppCategory *string `json:"app_category"`
|
|
|
|
AppPlatform *string `json:"app_platform"`
|
|
|
|
BudgetDay *float32 `json:"budget_day"`
|
|
|
|
BiddingMax *float32 `json:"bidding_max"`
|
|
|
|
BiddingMin *float32 `json:"bidding_min"`
|
|
|
|
BiddingType *string `json:"bidding_type"`
|
2018-02-14 02:58:59 +02:00
|
|
|
Freq *jsoniter.RawMessage `json:"freq"`
|
|
|
|
Targeting *jsoniter.RawMessage `json:"targeting"`
|
|
|
|
Url *jsoniter.RawMessage `json:"url"`
|
2018-02-24 16:04:41 +02:00
|
|
|
Speed *int `json:"speed" db:"speed"`
|
2017-09-21 14:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := &IteratorObject{}
|
2018-02-14 02:58:59 +02:00
|
|
|
decoder := jsoniter.NewDecoder(strings.NewReader(jsonStream))
|
2017-09-21 14:18:45 +02:00
|
|
|
err := decoder.Decode(obj)
|
|
|
|
should := require.New(t)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(`{"open":true,"type":"day","num":100}`, string(*obj.Freq))
|
|
|
|
}
|