Skip to content

Commit

Permalink
fix issue json-iterator#469
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenX2018 committed Jul 21, 2020
1 parent 53df7f7 commit e28033b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
19 changes: 19 additions & 0 deletions misc_tests/jsoniter_raw_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
should.Nil(aouterr)
}

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)
}

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 {
Expand Down
24 changes: 20 additions & 4 deletions reflect_json_raw_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ type jsonRawMessageCodec struct {
}

func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
*((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
if iter.ReadNil() {
*((*json.RawMessage)(ptr)) = nil
} else {
*((*json.RawMessage)(ptr)) = iter.SkipAndReturnBytes()
}
}

func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
if *((*json.RawMessage)(ptr)) == nil {
stream.WriteNil()
} else {
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
}
}

func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
Expand All @@ -48,11 +56,19 @@ type jsoniterRawMessageCodec struct {
}

func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
*((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
if iter.ReadNil() {
*((*RawMessage)(ptr)) = nil
} else {
*((*RawMessage)(ptr)) = iter.SkipAndReturnBytes()
}
}

func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteRaw(string(*((*RawMessage)(ptr))))
if *((*RawMessage)(ptr)) == nil {
stream.WriteNil()
} else {
stream.WriteRaw(string(*((*RawMessage)(ptr))))
}
}

func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
Expand Down

0 comments on commit e28033b

Please sign in to comment.