1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

update extension

This commit is contained in:
Tao Wen
2016-12-17 17:38:13 +08:00
parent d0f45c663f
commit 7d5f2aed7b
3 changed files with 42 additions and 10 deletions

View File

@ -45,8 +45,8 @@ func Test_customize_field_decoder(t *testing.T) {
}
}
func Test_customize_field_decoder_factory(t *testing.T) {
RegisterFieldCustomizer(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
func Test_customize_field_by_extension(t *testing.T) {
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
if (type_.String() == "jsoniter.Tom" && field.Name == "field1") {
return []string{"field-1"}, func(ptr unsafe.Pointer, iter *Iterator) {
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
@ -62,4 +62,28 @@ func Test_customize_field_decoder_factory(t *testing.T) {
if tom.field1 != "100" {
t.Fatal(tom.field1)
}
}
type Jerry struct {
field1 string
}
func Test_customize_type_by_extension(t *testing.T) {
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) {
if (type_.String() == "jsoniter.Jerry" && field == nil) {
return nil, func(ptr unsafe.Pointer, iter *Iterator) {
obj := (*Jerry)(ptr)
obj.field1 = iter.ReadString()
}
}
return nil, nil
})
jerry := Jerry{}
err := Unmarshal([]byte(`"100"`), &jerry)
if err != nil {
t.Fatal(err)
}
if jerry.field1 != "100" {
t.Fatal(jerry.field1)
}
}