Skip to content

Commit

Permalink
fix: useint64 in decode SetOption (#268)
Browse files Browse the repository at this point in the history
* fix: useint64 in decode SetOption

* doc: add comments on `sonic.Config`

Co-authored-by: liuqiang <liuqiang.06@bytedance.com>
Co-authored-by: duanyi.aster <duanyi.aster@bytedance.com>
  • Loading branch information
3 people committed Jul 25, 2022
1 parent 755c025 commit 34026b9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
36 changes: 34 additions & 2 deletions api.go
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions decoder/decoder.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions decoder/decoder_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit 34026b9

Please sign in to comment.