You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-24 23:16:47 +02:00
RegisterTypeEncoder and RegisterTypeDecoder should have higher priority
This commit is contained in:
@ -300,6 +300,12 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
|||||||
if typeDecoder != nil {
|
if typeDecoder != nil {
|
||||||
return typeDecoder, nil
|
return typeDecoder, nil
|
||||||
}
|
}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
typeDecoder := typeDecoders[typ.Elem().String()]
|
||||||
|
if typeDecoder != nil {
|
||||||
|
return &optionalDecoder{typ.Elem(),typeDecoder}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
cacheKey := typ
|
cacheKey := typ
|
||||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
cachedDecoder := getDecoderFromCache(cacheKey)
|
||||||
if cachedDecoder != nil {
|
if cachedDecoder != nil {
|
||||||
@ -375,6 +381,12 @@ func encoderOfType(typ reflect.Type) (Encoder, error) {
|
|||||||
if typeEncoder != nil {
|
if typeEncoder != nil {
|
||||||
return typeEncoder, nil
|
return typeEncoder, nil
|
||||||
}
|
}
|
||||||
|
if typ.Kind() == reflect.Ptr {
|
||||||
|
typeEncoder := typeEncoders[typ.Elem().String()]
|
||||||
|
if typeEncoder != nil {
|
||||||
|
return &optionalEncoder{typeEncoder}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
cacheKey := typ
|
cacheKey := typ
|
||||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
cachedEncoder := getEncoderFromCache(cacheKey)
|
||||||
if cachedEncoder != nil {
|
if cachedEncoder != nil {
|
||||||
|
@ -151,6 +151,24 @@ func Test_marshaler(t *testing.T) {
|
|||||||
should.Equal(`{"Field":"hello"}`, str)
|
should.Equal(`{"Field":"hello"}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_marshaler_and_encoder(t *testing.T) {
|
||||||
|
type TestObject struct {
|
||||||
|
Field *ObjectImplementedMarshaler
|
||||||
|
}
|
||||||
|
should := require.New(t)
|
||||||
|
RegisterTypeEncoder("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
stream.WriteString("hello from encoder")
|
||||||
|
})
|
||||||
|
val := ObjectImplementedMarshaler(100)
|
||||||
|
obj := TestObject{&val}
|
||||||
|
bytes, err := json.Marshal(obj)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"Field":"hello"}`, string(bytes))
|
||||||
|
str, err := MarshalToString(obj)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"Field":"hello from encoder"}`, str)
|
||||||
|
}
|
||||||
|
|
||||||
type ObjectImplementedUnmarshaler int
|
type ObjectImplementedUnmarshaler int
|
||||||
|
|
||||||
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
|
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
|
||||||
@ -174,3 +192,24 @@ func Test_unmarshaler(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(100, int(*obj.Field))
|
should.Equal(100, int(*obj.Field))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_unmarshaler_and_decoder(t *testing.T) {
|
||||||
|
type TestObject struct {
|
||||||
|
Field *ObjectImplementedUnmarshaler
|
||||||
|
Field2 string
|
||||||
|
}
|
||||||
|
should := require.New(t)
|
||||||
|
RegisterTypeDecoder("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
|
*(*ObjectImplementedUnmarshaler)(ptr) = 10
|
||||||
|
iter.Skip()
|
||||||
|
})
|
||||||
|
obj := TestObject{}
|
||||||
|
val := ObjectImplementedUnmarshaler(0)
|
||||||
|
obj.Field = &val
|
||||||
|
err := json.Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(100, int(*obj.Field))
|
||||||
|
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(10, int(*obj.Field))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user