diff --git a/api.go b/api.go index c694119bd..9456d81b0 100644 --- a/api.go +++ b/api.go @@ -22,16 +22,48 @@ import ( // Config is a combination of sonic/encoder.Options and sonic/decoder.Options type Config struct { + // EscapeHTML indicates encoder to escape all HTML characters + // after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape). + // WARNING: This hurts performance A LOT, USE WITH CARE. EscapeHTML bool + + // SortMapKeys indicates encoder that the keys of a map needs to be sorted + // before serializing into JSON. + // WARNING: This hurts performance A LOT, USE WITH CARE. SortMapKeys bool + + // CompactMarshaler indicates encoder that the output JSON from json.Marshaler + // is always compact and needs no validation CompactMarshaler bool + + // NoQuoteTextMarshaler indicates encoder that the output text from encoding.TextMarshaler + // is always escaped string and needs no quoting NoQuoteTextMarshaler bool + + // NoNullSliceOrMap indicates encoder that all empty Array or Object are encoded as '[]' or '{}', + // instead of 'null' + NoNullSliceOrMap bool + + // UseInt64 indicates decoder to unmarshal an integer into an interface{} as an + // int64 instead of as a float64. UseInt64 bool + + // UseNumber indicates decoder to unmarshal a number into an interface{} as a + // json.Number instead of as a float64. UseNumber bool + + // UseUnicodeErrors indicates decoder to return an error when encounter invalid + // UTF-8 escape sequences. UseUnicodeErrors bool + + // DisallowUnknownFields indicates decoder to return an error when the destination + // is a struct and the input contains object keys which do not match any + // non-ignored, exported fields in the destination. DisallowUnknownFields bool + + // CopyString indicates decoder to decode string values by copying instead of referring. CopyString bool - NoNullSliceOrMap bool + } var ( @@ -53,7 +85,7 @@ import ( ) - // API a binding of specific config. + // API is a binding of specific config. // This interface is inspired by github.com/json-iterator/go, // and has same behaviors under equavilent config. type API interface { diff --git a/decoder/decoder.go b/decoder/decoder.go index d59473e9a..3b7b5e0f4 100644 --- a/decoder/decoder.go +++ b/decoder/decoder.go @@ -48,7 +48,7 @@ const ( ) func (self *Decoder) SetOptions(opts Options) { - if (opts & 1<<_F_use_number != 0) && (opts & 1<<_F_use_int64 != 0) { + if (opts & OptionUseNumber != 0) && (opts & OptionUseInt64 != 0) { panic("can't set OptionUseInt64 and OptionUseNumber both!") } self.f = uint64(opts) @@ -107,34 +107,34 @@ func (self *Decoder) Decode(val interface{}) error { return err } -// UseInt64 causes the Decoder to unmarshal an integer into an interface{} as an +// UseInt64 indicates the Decoder to unmarshal an integer into an interface{} as an // int64 instead of as a float64. func (self *Decoder) UseInt64() { self.f |= 1 << _F_use_int64 self.f &^= 1 << _F_use_number } -// UseNumber causes the Decoder to unmarshal a number into an interface{} as a +// UseNumber indicates the Decoder to unmarshal a number into an interface{} as a // json.Number instead of as a float64. func (self *Decoder) UseNumber() { self.f &^= 1 << _F_use_int64 self.f |= 1 << _F_use_number } -// UseUnicodeErrors causes the Decoder to return an error when encounter invalid +// UseUnicodeErrors indicates the Decoder to return an error when encounter invalid // UTF-8 escape sequences. func (self *Decoder) UseUnicodeErrors() { self.f |= 1 << _F_disable_urc } -// DisallowUnknownFields causes the Decoder to return an error when the destination +// DisallowUnknownFields indicates the Decoder to return an error when the destination // is a struct and the input contains object keys which do not match any // non-ignored, exported fields in the destination. func (self *Decoder) DisallowUnknownFields() { self.f |= 1 << _F_disable_unknown } -// CopyString causes the Decoder to decode string values by copying instead of referring. +// CopyString indicates the Decoder to decode string values by copying instead of referring. func (self *Decoder) CopyString() { self.f |= 1 << _F_copy_string } diff --git a/decoder/decoder_test.go b/decoder/decoder_test.go index 6c1f67fb0..6a0f48779 100644 --- a/decoder/decoder_test.go +++ b/decoder/decoder_test.go @@ -275,6 +275,15 @@ func TestDecoder_Binding(t *testing.T) { spew.Dump(v) } +func TestDecoder_SetOption(t *testing.T) { + var v interface{} + d := NewDecoder("123") + d.SetOptions(OptionUseInt64) + err := d.Decode(&v) + assert.NoError(t, err) + assert.Equal(t, v, int64(123)) +} + func TestDecoder_MapWithIndirectElement(t *testing.T) { var v map[string]struct { A [129]byte } _, err := decode(`{"":{"A":[1,2,3,4,5]}}`, &v, false)