mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
add Api interface to allow save the frozen config
This commit is contained in:
parent
7e9017caa2
commit
31afe6450e
@ -29,6 +29,16 @@ type frozenConfig struct {
|
||||
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{
|
||||
EscapeHtml: true,
|
||||
}.Froze()
|
||||
@ -151,15 +161,15 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) Encoder {
|
||||
return cache[cacheKey]
|
||||
}
|
||||
|
||||
// CleanDecoders cleans decoders registered or cached
|
||||
func (cfg *frozenConfig) CleanDecoders() {
|
||||
// cleanDecoders cleans decoders registered or cached
|
||||
func (cfg *frozenConfig) cleanDecoders() {
|
||||
typeDecoders = map[string]Decoder{}
|
||||
fieldDecoders = map[string]Decoder{}
|
||||
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
|
||||
}
|
||||
|
||||
// CleanEncoders cleans encoders registered or cached
|
||||
func (cfg *frozenConfig) CleanEncoders() {
|
||||
// cleanEncoders cleans encoders registered or cached
|
||||
func (cfg *frozenConfig) cleanEncoders() {
|
||||
typeEncoders = map[string]Encoder{}
|
||||
fieldEncoders = map[string]Encoder{}
|
||||
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
||||
|
@ -19,7 +19,7 @@ func Test_customize_type_decoder(t *testing.T) {
|
||||
}
|
||||
*((*time.Time)(ptr)) = t
|
||||
})
|
||||
defer ConfigDefault.CleanDecoders()
|
||||
defer ConfigDefault.cleanDecoders()
|
||||
val := time.Time{}
|
||||
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
||||
if err != nil {
|
||||
@ -37,7 +37,7 @@ func Test_customize_type_encoder(t *testing.T) {
|
||||
t := *((*time.Time)(ptr))
|
||||
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
|
||||
})
|
||||
defer ConfigDefault.CleanEncoders()
|
||||
defer ConfigDefault.cleanEncoders()
|
||||
val := time.Unix(0, 0)
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
@ -45,13 +45,13 @@ func Test_customize_type_encoder(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_customize_byte_array_encoder(t *testing.T) {
|
||||
ConfigDefault.CleanEncoders()
|
||||
ConfigDefault.cleanEncoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
t := *((*[]byte)(ptr))
|
||||
stream.WriteString(string(t))
|
||||
})
|
||||
defer ConfigDefault.CleanEncoders()
|
||||
defer ConfigDefault.cleanEncoders()
|
||||
val := []byte("abc")
|
||||
str, err := MarshalToString(val)
|
||||
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) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
})
|
||||
defer ConfigDefault.CleanDecoders()
|
||||
defer ConfigDefault.cleanDecoders()
|
||||
tom := Tom{}
|
||||
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
||||
if err != nil {
|
||||
|
@ -141,8 +141,8 @@ func Test_encode_object_contain_non_empty_interface(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_nil_non_empty_interface(t *testing.T) {
|
||||
ConfigDefault.CleanEncoders()
|
||||
ConfigDefault.CleanDecoders()
|
||||
ConfigDefault.cleanEncoders()
|
||||
ConfigDefault.cleanDecoders()
|
||||
type TestObject struct {
|
||||
Field []MyInterface
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user