1
0
mirror of https://github.com/json-iterator/go.git synced 2025-02-13 19:41:56 +02:00

add Api interface to allow save the frozen config

This commit is contained in:
Tao Wen 2017-06-19 13:43:22 +08:00
parent 7e9017caa2
commit 31afe6450e
3 changed files with 21 additions and 11 deletions

View File

@ -29,6 +29,16 @@ type frozenConfig struct {
iteratorPool chan *Iterator iteratorPool chan *Iterator
} }
type Api interface {
MarshalToString(v interface{}) (string, error)
Marshal(v interface{}) ([]byte, error)
UnmarshalFromString(str string, v interface{}) error
Unmarshal(data []byte, v interface{}) error
Get(data []byte, path ...interface{}) Any
NewEncoder(writer io.Writer) *AdaptedEncoder
NewDecoder(reader io.Reader) *AdaptedDecoder
}
var ConfigDefault = Config{ var ConfigDefault = Config{
EscapeHtml: true, EscapeHtml: true,
}.Froze() }.Froze()
@ -151,15 +161,15 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) Encoder {
return cache[cacheKey] return cache[cacheKey]
} }
// CleanDecoders cleans decoders registered or cached // cleanDecoders cleans decoders registered or cached
func (cfg *frozenConfig) CleanDecoders() { func (cfg *frozenConfig) cleanDecoders() {
typeDecoders = map[string]Decoder{} typeDecoders = map[string]Decoder{}
fieldDecoders = map[string]Decoder{} fieldDecoders = map[string]Decoder{}
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{})) atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
} }
// CleanEncoders cleans encoders registered or cached // cleanEncoders cleans encoders registered or cached
func (cfg *frozenConfig) CleanEncoders() { func (cfg *frozenConfig) cleanEncoders() {
typeEncoders = map[string]Encoder{} typeEncoders = map[string]Encoder{}
fieldEncoders = map[string]Encoder{} fieldEncoders = map[string]Encoder{}
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{})) atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))

View File

@ -19,7 +19,7 @@ func Test_customize_type_decoder(t *testing.T) {
} }
*((*time.Time)(ptr)) = t *((*time.Time)(ptr)) = t
}) })
defer ConfigDefault.CleanDecoders() defer ConfigDefault.cleanDecoders()
val := time.Time{} val := time.Time{}
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val) err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
if err != nil { if err != nil {
@ -37,7 +37,7 @@ func Test_customize_type_encoder(t *testing.T) {
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"))
}) })
defer ConfigDefault.CleanEncoders() defer ConfigDefault.cleanEncoders()
val := time.Unix(0, 0) val := time.Unix(0, 0)
str, err := MarshalToString(val) str, err := MarshalToString(val)
should.Nil(err) should.Nil(err)
@ -45,13 +45,13 @@ 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) { RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
t := *((*[]byte)(ptr)) t := *((*[]byte)(ptr))
stream.WriteString(string(t)) stream.WriteString(string(t))
}) })
defer ConfigDefault.CleanEncoders() defer ConfigDefault.cleanEncoders()
val := []byte("abc") val := []byte("abc")
str, err := MarshalToString(val) str, err := MarshalToString(val)
should.Nil(err) should.Nil(err)
@ -74,7 +74,7 @@ func Test_customize_field_decoder(t *testing.T) {
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) { RegisterFieldDecoder("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()
tom := Tom{} tom := Tom{}
err := Unmarshal([]byte(`{"field1": 100}`), &tom) err := Unmarshal([]byte(`{"field1": 100}`), &tom)
if err != nil { if err != nil {

View File

@ -141,8 +141,8 @@ func Test_encode_object_contain_non_empty_interface(t *testing.T) {
} }
func Test_nil_non_empty_interface(t *testing.T) { func Test_nil_non_empty_interface(t *testing.T) {
ConfigDefault.CleanEncoders() ConfigDefault.cleanEncoders()
ConfigDefault.CleanDecoders() ConfigDefault.cleanDecoders()
type TestObject struct { type TestObject struct {
Field []MyInterface Field []MyInterface
} }