1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-23 11:37:32 +02:00

expose ValEncoder & ValDecoder

This commit is contained in:
Tao Wen 2017-06-20 08:08:59 +08:00
parent aa01f57b7f
commit 14588726a1
3 changed files with 30 additions and 16 deletions

View File

@ -47,6 +47,7 @@ func writeToStream(val interface{}, stream *Stream, encoder ValEncoder) {
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator) type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream) type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc)
type funcDecoder struct { type funcDecoder struct {
@ -105,25 +106,38 @@ func init() {
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
} }
// RegisterTypeDecoder can register a type for json object func RegisterTypeDecoderFunc(typ string, fun DecoderFunc) {
func RegisterTypeDecoder(typ string, fun DecoderFunc) {
typeDecoders[typ] = &funcDecoder{fun} typeDecoders[typ] = &funcDecoder{fun}
} }
// RegisterFieldDecoder can register a type for json field func RegisterTypeDecoder(typ string, decoder ValDecoder) {
func RegisterFieldDecoder(typ string, field string, fun DecoderFunc) { typeDecoders[typ] = decoder
fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = &funcDecoder{fun}
} }
func RegisterTypeEncoder(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) { func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc) {
RegisterFieldDecoder(typ, field, &funcDecoder{fun})
}
func RegisterFieldDecoder(typ string, field string, decoder ValDecoder) {
fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = decoder
}
func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc} typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc}
} }
func RegisterFieldEncoder(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) { func RegisterTypeEncoder(typ string, encoder ValEncoder) {
fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = &funcEncoder{fun, isEmptyFunc} typeEncoders[typ] = encoder
}
func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
RegisterFieldEncoder(typ, field, &funcEncoder{fun, isEmptyFunc})
}
func RegisterFieldEncoder(typ string, field string, encoder ValEncoder) {
fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = encoder
} }
// RegisterExtension can register a custom extension
func RegisterExtension(extension ExtensionFunc) { func RegisterExtension(extension ExtensionFunc) {
extensions = append(extensions, extension) extensions = append(extensions, extension)
} }

View File

@ -11,7 +11,7 @@ import (
) )
func Test_customize_type_decoder(t *testing.T) { func Test_customize_type_decoder(t *testing.T) {
RegisterTypeDecoder("time.Time", func(ptr unsafe.Pointer, iter *Iterator) { RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *Iterator) {
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC) t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
if err != nil { if err != nil {
iter.Error = err iter.Error = err
@ -33,7 +33,7 @@ func Test_customize_type_decoder(t *testing.T) {
func Test_customize_type_encoder(t *testing.T) { func Test_customize_type_encoder(t *testing.T) {
should := require.New(t) should := require.New(t)
RegisterTypeEncoder("time.Time", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoderFunc("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
t := *((*time.Time)(ptr)) t := *((*time.Time)(ptr))
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05")) stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
}, nil) }, nil)
@ -47,7 +47,7 @@ func Test_customize_type_encoder(t *testing.T) {
func Test_customize_byte_array_encoder(t *testing.T) { func Test_customize_byte_array_encoder(t *testing.T) {
ConfigDefault.cleanEncoders() ConfigDefault.cleanEncoders()
should := require.New(t) should := require.New(t)
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
t := *((*[]byte)(ptr)) t := *((*[]byte)(ptr))
stream.WriteString(string(t)) stream.WriteString(string(t))
}, nil) }, nil)
@ -71,7 +71,7 @@ type Tom struct {
} }
func Test_customize_field_decoder(t *testing.T) { func Test_customize_field_decoder(t *testing.T) {
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) { RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt()) *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
}) })
defer ConfigDefault.cleanDecoders() defer ConfigDefault.cleanDecoders()
@ -156,7 +156,7 @@ func Test_marshaler_and_encoder(t *testing.T) {
Field *ObjectImplementedMarshaler Field *ObjectImplementedMarshaler
} }
should := require.New(t) should := require.New(t)
RegisterTypeEncoder("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoderFunc("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
stream.WriteString("hello from encoder") stream.WriteString("hello from encoder")
}, nil) }, nil)
val := ObjectImplementedMarshaler(100) val := ObjectImplementedMarshaler(100)
@ -199,7 +199,7 @@ func Test_unmarshaler_and_decoder(t *testing.T) {
Field2 string Field2 string
} }
should := require.New(t) should := require.New(t)
RegisterTypeDecoder("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) { RegisterTypeDecoderFunc("jsoniter.ObjectImplementedUnmarshaler", func(ptr unsafe.Pointer, iter *Iterator) {
*(*ObjectImplementedUnmarshaler)(ptr) = 10 *(*ObjectImplementedUnmarshaler)(ptr) = 10
iter.Skip() iter.Skip()
}) })

View File

@ -86,7 +86,7 @@ func Test_read_interface(t *testing.T) {
func Test_read_custom_interface(t *testing.T) { func Test_read_custom_interface(t *testing.T) {
should := require.New(t) should := require.New(t)
var val MyInterface var val MyInterface
RegisterTypeDecoder("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) { RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
*((*MyInterface)(ptr)) = MyString(iter.ReadString()) *((*MyInterface)(ptr)) = MyString(iter.ReadString())
}) })
err := UnmarshalFromString(`"hello"`, &val) err := UnmarshalFromString(`"hello"`, &val)