123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package decoder
- import (
- `bytes`
- `encoding/json`
- `io`
- `reflect`
- `unsafe`
- `github.com/bytedance/sonic/internal/native/types`
- `github.com/bytedance/sonic/option`
- )
- func init() {
- println("WARNING: sonic only supports Go1.16~1.20 && CPU amd64, but your environment is not suitable")
- }
- const (
- _F_use_int64 = 0
- _F_disable_urc = 2
- _F_disable_unknown = 3
- _F_copy_string = 4
-
- _F_use_number = types.B_USE_NUMBER
- _F_validate_string = types.B_VALIDATE_STRING
- _F_allow_control = types.B_ALLOW_CONTROL
- )
- type Options uint64
- const (
- OptionUseInt64 Options = 1 << _F_use_int64
- OptionUseNumber Options = 1 << _F_use_number
- OptionUseUnicodeErrors Options = 1 << _F_disable_urc
- OptionDisableUnknown Options = 1 << _F_disable_unknown
- OptionCopyString Options = 1 << _F_copy_string
- OptionValidateString Options = 1 << _F_validate_string
- )
- func (self *Decoder) SetOptions(opts Options) {
- if (opts & OptionUseNumber != 0) && (opts & OptionUseInt64 != 0) {
- panic("can't set OptionUseInt64 and OptionUseNumber both!")
- }
- self.f = uint64(opts)
- }
- type Decoder struct {
- i int
- f uint64
- s string
- }
- func NewDecoder(s string) *Decoder {
- return &Decoder{s: s}
- }
- func (self *Decoder) Pos() int {
- return self.i
- }
- func (self *Decoder) Reset(s string) {
- self.s = s
- self.i = 0
-
- }
- func (self *Decoder) CheckTrailings() error {
- pos := self.i
- buf := self.s
-
- if pos != len(buf) {
- for pos < len(buf) && (types.SPACE_MASK & (1 << buf[pos])) != 0 {
- pos++
- }
- }
-
- if pos == len(buf) {
- return nil
- }
-
- return nil
- }
- func (self *Decoder) Decode(val interface{}) error {
- r := bytes.NewBufferString(self.s)
- dec := json.NewDecoder(r)
- if (self.f & uint64(OptionUseNumber)) != 0 {
- dec.UseNumber()
- }
- if (self.f & uint64(OptionDisableUnknown)) != 0 {
- dec.DisallowUnknownFields()
- }
- return dec.Decode(val)
- }
- func (self *Decoder) UseInt64() {
- self.f |= 1 << _F_use_int64
- self.f &^= 1 << _F_use_number
- }
- func (self *Decoder) UseNumber() {
- self.f &^= 1 << _F_use_int64
- self.f |= 1 << _F_use_number
- }
- func (self *Decoder) UseUnicodeErrors() {
- self.f |= 1 << _F_disable_urc
- }
- func (self *Decoder) DisallowUnknownFields() {
- self.f |= 1 << _F_disable_unknown
- }
- func (self *Decoder) CopyString() {
- self.f |= 1 << _F_copy_string
- }
- func (self *Decoder) ValidateString() {
- self.f |= 1 << _F_validate_string
- }
- func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
- return nil
- }
- type StreamDecoder = json.Decoder
- func NewStreamDecoder(r io.Reader) *StreamDecoder {
- return json.NewDecoder(r)
- }
- type SyntaxError json.SyntaxError
- func (s SyntaxError) Description() string {
- return (*json.SyntaxError)(unsafe.Pointer(&s)).Error()
- }
- func (s SyntaxError) Error() string {
- return (*json.SyntaxError)(unsafe.Pointer(&s)).Error()
- }
- type MismatchTypeError json.UnmarshalTypeError
|