gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 8a0ec2b7608704406e868bcc7499ed64487c73c5
parent dc6a3b6c7a636104a5dc118dd848a2812a5fa8ff
Author: francoispqt <francois@parquet.ninja>
Date:   Sat, 23 Mar 2019 00:50:44 +0800

update decode slices methods

Diffstat:
Mdecode_slices.go | 128++++++++++++++++++++++++++-----------------------------------------------------
1 file changed, 42 insertions(+), 86 deletions(-)

diff --git a/decode_slices.go b/decode_slices.go @@ -1,89 +1,49 @@ package gojay -// SliceString is a *[]string implementing gojay.UnmarshalerJSONArray interface -type SliceString []string - -// UnmarshalJSONArray implements gojay.UnmarshalerJSONArray -func (s *SliceString) UnmarshalJSONArray(dec *Decoder) error { - var str string - if err := dec.String(&str); err != nil { - return err - } - *s = append(*s, str) - return nil -} - -// IsNil implements gojay.UnmarshalerJSONArray -func (s *SliceString) IsNil() bool { - return s == nil || len(*s) == 0 -} - -// AddSliceString unmarshals the next JSON array of strings to the given *[]string s +// SliceString unmarshals the next JSON array of strings to the given *[]string s func (dec *Decoder) AddSliceString(s *[]string) error { return dec.SliceString(s) } // SliceString unmarshals the next JSON array of strings to the given *[]string s func (dec *Decoder) SliceString(s *[]string) error { - var st = SliceString(*s) - if err := dec.Array(&st); err != nil { + err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error { + var str string + if err := dec.String(&str); err != nil { + return err + } + *s = append(*s, str) + return nil + })) + + if err != nil { return err } - *s = st return nil } -// SliceInt is a *[]int implementing gojay.UnmarshalerJSONArray interface -type SliceInt []int - -// UnmarshalJSONArray implements gojay.UnmarshalerJSONArray -func (s *SliceInt) UnmarshalJSONArray(dec *Decoder) error { - var i int - if err := dec.Int(&i); err != nil { - return err - } - *s = append(*s, i) - return nil -} - -// IsNil implements gojay.UnmarshalerJSONArray -func (s *SliceInt) IsNil() bool { - return s == nil || len(*s) == 0 -} - -// AddInt unmarshals the next JSON array of integers to the given *[]int s +// SliceInt unmarshals the next JSON array of integers to the given *[]int s func (dec *Decoder) AddSliceInt(s *[]int) error { return dec.SliceInt(s) } // SliceInt unmarshals the next JSON array of integers to the given *[]int s func (dec *Decoder) SliceInt(s *[]int) error { - var st = SliceInt(*s) - if err := dec.Array(&st); err != nil { + err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error { + var i int + if err := dec.Int(&i); err != nil { + return err + } + *s = append(*s, i) + return nil + })) + + if err != nil { return err } - *s = st return nil } -// SliceFloat64 is a *[]float64 implementing gojay.UnmarshalerJSONArray interface -type SliceFloat64 []float64 - -// UnmarshalJSONArray implements gojay.UnmarshalerJSONArray -func (s *SliceFloat64) UnmarshalJSONArray(dec *Decoder) error { - var i float64 - if err := dec.Float64(&i); err != nil { - return err - } - *s = append(*s, i) - return nil -} - -// IsNil implements gojay.UnmarshalerJSONArray -func (s *SliceFloat64) IsNil() bool { - return s == nil || len(*s) == 0 -} - // AddFloat64 unmarshals the next JSON array of floats to the given *[]float64 s func (dec *Decoder) AddSliceFloat64(s *[]float64) error { return dec.SliceFloat64(s) @@ -91,32 +51,21 @@ func (dec *Decoder) AddSliceFloat64(s *[]float64) error { // SliceFloat64 unmarshals the next JSON array of floats to the given *[]float64 s func (dec *Decoder) SliceFloat64(s *[]float64) error { - var st = SliceFloat64(*s) - if err := dec.Array(&st); err != nil { + err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error { + var i float64 + if err := dec.Float64(&i); err != nil { + return err + } + *s = append(*s, i) + return nil + })) + + if err != nil { return err } - *s = st return nil } -// SliceBool is a *[]bool implementing gojay.UnmarshalerJSONArray boolerface -type SliceBool []bool - -// UnmarshalJSONArray implements gojay.UnmarshalerJSONArray -func (s *SliceBool) UnmarshalJSONArray(dec *Decoder) error { - var i bool - if err := dec.Bool(&i); err != nil { - return err - } - *s = append(*s, i) - return nil -} - -// IsNil implements gojay.UnmarshalerJSONArray -func (s *SliceBool) IsNil() bool { - return s == nil || len(*s) == 0 -} - // AddBool unmarshals the next JSON array of boolegers to the given *[]bool s func (dec *Decoder) AddSliceBool(s *[]bool) error { return dec.SliceBool(s) @@ -124,10 +73,17 @@ func (dec *Decoder) AddSliceBool(s *[]bool) error { // SliceBool unmarshals the next JSON array of boolegers to the given *[]bool s func (dec *Decoder) SliceBool(s *[]bool) error { - var st = SliceBool(*s) - if err := dec.Array(&st); err != nil { + err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error { + var b bool + if err := dec.Bool(&b); err != nil { + return err + } + *s = append(*s, b) + return nil + })) + + if err != nil { return err } - *s = st return nil }