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

support customize all fields

This commit is contained in:
Tao Wen
2016-12-12 19:06:33 +08:00
parent 39a6ced9d0
commit 9fe4625ee2
2 changed files with 46 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"time"
"unsafe"
"strconv"
"reflect"
)
func Test_customize_type_decoder(t *testing.T) {
@ -42,4 +43,23 @@ func Test_customize_field_decoder(t *testing.T) {
if err != nil {
t.Fatal(err)
}
}
func Test_customize_field_decoder_factory(t *testing.T) {
RegisterFieldCustomizer(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())
}
}
return nil, nil
})
tom := Tom{}
err := Unmarshal([]byte(`{"field-1": 100}`), &tom)
if err != nil {
t.Fatal(err)
}
if tom.field1 != "100" {
t.Fatal(tom.field1)
}
}