You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-24 23:16:47 +02:00
#66 Make extension api like the java version
This commit is contained in:
@ -3,7 +3,6 @@ package jsoniter
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
@ -86,22 +85,30 @@ type TestObject1 struct {
|
||||
field1 string
|
||||
}
|
||||
|
||||
type testExtension struct {
|
||||
DummyExtension
|
||||
}
|
||||
|
||||
func (extension *testExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
|
||||
if structDescriptor.Type.String() != "jsoniter.TestObject1" {
|
||||
return
|
||||
}
|
||||
binding := structDescriptor.Fields["field1"]
|
||||
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *Stream) {
|
||||
str := *((*string)(ptr))
|
||||
val, _ := strconv.Atoi(str)
|
||||
stream.WriteInt(val)
|
||||
}}
|
||||
binding.Decoder = &funcDecoder{func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
}}
|
||||
binding.ToNames = []string{"field-1"}
|
||||
binding.FromNames = []string{"field-1"}
|
||||
}
|
||||
|
||||
func Test_customize_field_by_extension(t *testing.T) {
|
||||
should := require.New(t)
|
||||
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
|
||||
if type_.String() == "jsoniter.TestObject1" && field.Name == "field1" {
|
||||
encode := func(ptr unsafe.Pointer, stream *Stream) {
|
||||
str := *((*string)(ptr))
|
||||
val, _ := strconv.Atoi(str)
|
||||
stream.WriteInt(val)
|
||||
}
|
||||
decode := func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
}
|
||||
return []string{"field-1"}, encode, decode
|
||||
}
|
||||
return nil, nil, nil
|
||||
})
|
||||
RegisterExtension(&testExtension{})
|
||||
obj := TestObject1{}
|
||||
err := UnmarshalFromString(`{"field-1": 100}`, &obj)
|
||||
should.Nil(err)
|
||||
@ -111,24 +118,24 @@ func Test_customize_field_by_extension(t *testing.T) {
|
||||
should.Equal(`{"field-1":100}`, str)
|
||||
}
|
||||
|
||||
func Test_unexported_fields(t *testing.T) {
|
||||
jsoniter := Config{SupportUnexportedStructFields: true}.Froze()
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
field1 string
|
||||
field2 string `json:"field-2"`
|
||||
}
|
||||
obj := TestObject{}
|
||||
obj.field1 = "hello"
|
||||
should.Nil(jsoniter.UnmarshalFromString(`{}`, &obj))
|
||||
should.Equal("hello", obj.field1)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
|
||||
should.Equal("world", obj.field1)
|
||||
should.Equal("abc", obj.field2)
|
||||
str, err := jsoniter.MarshalToString(obj)
|
||||
should.Nil(err)
|
||||
should.Contains(str, `"field-2":"abc"`)
|
||||
}
|
||||
//func Test_unexported_fields(t *testing.T) {
|
||||
// jsoniter := Config{SupportUnexportedStructFields: true}.Froze()
|
||||
// should := require.New(t)
|
||||
// type TestObject struct {
|
||||
// field1 string
|
||||
// field2 string `json:"field-2"`
|
||||
// }
|
||||
// obj := TestObject{}
|
||||
// obj.field1 = "hello"
|
||||
// should.Nil(jsoniter.UnmarshalFromString(`{}`, &obj))
|
||||
// should.Equal("hello", obj.field1)
|
||||
// should.Nil(jsoniter.UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
|
||||
// should.Equal("world", obj.field1)
|
||||
// should.Equal("abc", obj.field2)
|
||||
// str, err := jsoniter.MarshalToString(obj)
|
||||
// should.Nil(err)
|
||||
// should.Contains(str, `"field-2":"abc"`)
|
||||
//}
|
||||
|
||||
type ObjectImplementedMarshaler int
|
||||
|
||||
@ -155,6 +162,7 @@ func Test_marshaler_and_encoder(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedMarshaler
|
||||
}
|
||||
ConfigDefault.cleanEncoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoderFunc("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
stream.WriteString("hello from encoder")
|
||||
@ -198,6 +206,7 @@ func Test_unmarshaler_and_decoder(t *testing.T) {
|
||||
Field *ObjectImplementedUnmarshaler
|
||||
Field2 string
|
||||
}
|
||||
ConfigDefault.cleanDecoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeDecoderFunc("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*(*ObjectImplementedUnmarshaler)(ptr) = 10
|
||||
|
Reference in New Issue
Block a user