You've already forked json-iterator
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:
@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user