Skip to content

Commit 90574c5

Browse files
committed
#166 support ValidateJsonRawMessage in ConfigCompatibleWithStandardLibrary
1 parent 6a4ba7b commit 90574c5

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

feature_config.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
SortMapKeys bool
1919
UseNumber bool
2020
TagKey string
21+
ValidateJsonRawMessage bool
2122
}
2223

2324
type frozenConfig struct {
@@ -53,8 +54,9 @@ var ConfigDefault = Config{
5354

5455
// ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior
5556
var ConfigCompatibleWithStandardLibrary = Config{
56-
EscapeHTML: true,
57-
SortMapKeys: true,
57+
EscapeHTML: true,
58+
SortMapKeys: true,
59+
ValidateJsonRawMessage: true,
5860
}.Froze()
5961

6062
// ConfigFastest marshals float with only 6 digits precision
@@ -83,10 +85,31 @@ func (cfg Config) Froze() API {
8385
if cfg.UseNumber {
8486
frozenConfig.useNumber()
8587
}
88+
if cfg.ValidateJsonRawMessage {
89+
frozenConfig.validateJsonRawMessage()
90+
}
8691
frozenConfig.configBeforeFrozen = cfg
8792
return frozenConfig
8893
}
8994

95+
func (cfg *frozenConfig) validateJsonRawMessage() {
96+
encoder := &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
97+
rawMessage := *(*json.RawMessage)(ptr)
98+
iter := cfg.BorrowIterator([]byte(rawMessage))
99+
iter.Read()
100+
if iter.Error != nil {
101+
stream.WriteRaw("null")
102+
} else {
103+
cfg.ReturnIterator(iter)
104+
stream.WriteRaw(string(rawMessage))
105+
}
106+
}, func(ptr unsafe.Pointer) bool {
107+
return false
108+
}}
109+
cfg.addEncoderToCache(reflect.TypeOf((*json.RawMessage)(nil)).Elem(), encoder)
110+
cfg.addEncoderToCache(reflect.TypeOf((*RawMessage)(nil)).Elem(), encoder)
111+
}
112+
90113
func (cfg *frozenConfig) useNumber() {
91114
cfg.addDecoderToCache(reflect.TypeOf((*interface{})(nil)).Elem(), &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
92115
if iter.WhatIsNext() == NumberValue {

feature_reflect_native.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding"
55
"encoding/base64"
66
"encoding/json"
7-
"unsafe"
87
"reflect"
8+
"unsafe"
99
)
1010

1111
type stringCodec struct {

jsoniter_interface_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package jsoniter
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"github.com/stretchr/testify/require"
67
"testing"
78
"unsafe"
8-
"fmt"
99
)
1010

1111
func Test_write_array_of_interface(t *testing.T) {

jsoniter_raw_message_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,17 @@ func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
7272
should.Nil(err)
7373
should.Equal(`{"hello":[]}`, output)
7474
}
75+
76+
func Test_marshal_invalid_json_raw_message(t *testing.T) {
77+
type A struct {
78+
Raw json.RawMessage `json:"raw"`
79+
}
80+
message := []byte(`{}`)
81+
82+
a := A{}
83+
should := require.New(t)
84+
should.Nil(ConfigCompatibleWithStandardLibrary.Unmarshal(message, &a))
85+
aout, aouterr := ConfigCompatibleWithStandardLibrary.Marshal(&a)
86+
should.Equal(`{"raw":null}`, string(aout))
87+
should.Nil(aouterr)
88+
}

0 commit comments

Comments
 (0)