Skip to content

Commit 5bce16d

Browse files
committed
fix issue #469
1 parent a1ca083 commit 5bce16d

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

‎misc_tests/jsoniter_raw_message_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
4242
should.Nil(aouterr)
4343
}
4444

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+
4564
func Test_raw_message_memory_not_copied_issue(t *testing.T) {
4665
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"}}`
4766
type IteratorObject struct {

‎reflect_json_raw_message.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ type jsonRawMessageCodec struct {
3333
}
3434

3535
func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
36-
*((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
36+
if iter.ReadNil() {
37+
*((*json.RawMessage)(ptr)) = nil
38+
} else {
39+
*((*json.RawMessage)(ptr)) = iter.SkipAndReturnBytes()
40+
}
3741
}
3842

3943
func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
40-
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
44+
if *((*json.RawMessage)(ptr)) == nil {
45+
stream.WriteNil()
46+
} else {
47+
stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
48+
}
4149
}
4250

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

5058
func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
51-
*((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
59+
if iter.ReadNil() {
60+
*((*RawMessage)(ptr)) = nil
61+
} else {
62+
*((*RawMessage)(ptr)) = iter.SkipAndReturnBytes()
63+
}
5264
}
5365

5466
func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
55-
stream.WriteRaw(string(*((*RawMessage)(ptr))))
67+
if *((*RawMessage)(ptr)) == nil {
68+
stream.WriteNil()
69+
} else {
70+
stream.WriteRaw(string(*((*RawMessage)(ptr))))
71+
}
5672
}
5773

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

0 commit comments

Comments
 (0)