From 5d789e5e028dcd1c62ce56b6e20e72637df1f572 Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Mon, 6 Aug 2018 13:58:33 +0800 Subject: [PATCH] fix #291 omit empty was not handled properly for json raw message --- config.go | 2 +- value_tests/raw_message_test.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 59e1614c..8c58fcba 100644 --- a/config.go +++ b/config.go @@ -191,7 +191,7 @@ func (cfg *frozenConfig) validateJsonRawMessage(extension EncoderExtension) { stream.WriteRaw(string(rawMessage)) } }, func(ptr unsafe.Pointer) bool { - return false + return len(*((*json.RawMessage)(ptr))) == 0 }} extension[reflect2.TypeOfPtr((*json.RawMessage)(nil)).Elem()] = encoder extension[reflect2.TypeOfPtr((*RawMessage)(nil)).Elem()] = encoder diff --git a/value_tests/raw_message_test.go b/value_tests/raw_message_test.go index 210451d2..b024c4e0 100644 --- a/value_tests/raw_message_test.go +++ b/value_tests/raw_message_test.go @@ -1,10 +1,18 @@ package test -import "encoding/json" +import ( + "encoding/json" +) func init() { marshalCases = append(marshalCases, json.RawMessage("{}"), + selectedMarshalCase{struct { + Env string `json:"env"` + Extra json.RawMessage `json:"extra,omitempty"` + }{ + Env: "jfdk", + }}, ) unmarshalCases = append(unmarshalCases, unmarshalCase{ ptr: (*json.RawMessage)(nil),