From 5bce16d299eb21bb5a80aa37151d805c37acd79d Mon Sep 17 00:00:00 2001 From: AllenX2018 Date: Tue, 21 Jul 2020 17:07:23 +0800 Subject: [PATCH] fix issue #469 --- misc_tests/jsoniter_raw_message_test.go | 19 +++++++++++++++++++ reflect_json_raw_message.go | 24 ++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/misc_tests/jsoniter_raw_message_test.go b/misc_tests/jsoniter_raw_message_test.go index 9af7cd73..86af5efe 100644 --- a/misc_tests/jsoniter_raw_message_test.go +++ b/misc_tests/jsoniter_raw_message_test.go @@ -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 { diff --git a/reflect_json_raw_message.go b/reflect_json_raw_message.go index f2619936..eba434f2 100644 --- a/reflect_json_raw_message.go +++ b/reflect_json_raw_message.go @@ -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 { @@ -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 {