commit 0994f7ac683d3d422741cf47395d251b42db54ca
parent 8a0ec2b7608704406e868bcc7499ed64487c73c5
Author: francoispqt <francois@parquet.ninja>
Date: Sat, 23 Mar 2019 01:17:53 +0800
add encoding built in slices
Diffstat:
5 files changed, 291 insertions(+), 89 deletions(-)
diff --git a/decode_slice.go b/decode_slice.go
@@ -0,0 +1,89 @@
+package gojay
+
+// AddSliceString 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 {
+ 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
+ }
+ return nil
+}
+
+// AddSliceInt 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 {
+ 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
+ }
+ return nil
+}
+
+// AddFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
+func (dec *Decoder) AddSliceFloat64(s *[]float64) error {
+ return dec.SliceFloat64(s)
+}
+
+// SliceFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
+func (dec *Decoder) SliceFloat64(s *[]float64) error {
+ 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
+ }
+ return nil
+}
+
+// AddBool unmarshals the next JSON array of boolegers to the given *[]bool s
+func (dec *Decoder) AddSliceBool(s *[]bool) error {
+ return dec.SliceBool(s)
+}
+
+// SliceBool unmarshals the next JSON array of boolegers to the given *[]bool s
+func (dec *Decoder) SliceBool(s *[]bool) error {
+ 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
+ }
+ return nil
+}
diff --git a/decode_slices_test.go b/decode_slice_test.go
diff --git a/decode_slices.go b/decode_slices.go
@@ -1,89 +0,0 @@
-package gojay
-
-// 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 {
- 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
- }
- return nil
-}
-
-// 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 {
- 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
- }
- return nil
-}
-
-// AddFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
-func (dec *Decoder) AddSliceFloat64(s *[]float64) error {
- return dec.SliceFloat64(s)
-}
-
-// SliceFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
-func (dec *Decoder) SliceFloat64(s *[]float64) error {
- 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
- }
- return nil
-}
-
-// AddBool unmarshals the next JSON array of boolegers to the given *[]bool s
-func (dec *Decoder) AddSliceBool(s *[]bool) error {
- return dec.SliceBool(s)
-}
-
-// SliceBool unmarshals the next JSON array of boolegers to the given *[]bool s
-func (dec *Decoder) SliceBool(s *[]bool) error {
- 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
- }
- return nil
-}
diff --git a/encode_slice.go b/encode_slice.go
@@ -0,0 +1,113 @@
+package gojay
+
+// AddSliceString marshals the given []string s
+func (enc *Encoder) AddSliceString(s []string) {
+ enc.SliceString(s)
+}
+
+// SliceString marshals the given []string s
+func (enc *Encoder) SliceString(s []string) {
+ enc.Array(EncodeArrayFunc(func(enc *Encoder) {
+ for _, str := range s {
+ enc.String(str)
+ }
+ }))
+}
+
+// AddSliceStringKey marshals the given []string s
+func (enc *Encoder) AddSliceStringKey(k string, s []string) {
+ enc.SliceStringKey(k, s)
+}
+
+// SliceStringKey marshals the given []string s
+func (enc *Encoder) SliceStringKey(k string, s []string) {
+ enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
+ for _, str := range s {
+ enc.String(str)
+ }
+ }))
+}
+
+// AddSliceInt marshals the given []int s
+func (enc *Encoder) AddSliceInt(s []int) {
+ enc.SliceInt(s)
+}
+
+// SliceInt marshals the given []int s
+func (enc *Encoder) SliceInt(s []int) {
+ enc.Array(EncodeArrayFunc(func(enc *Encoder) {
+ for _, i := range s {
+ enc.Int(i)
+ }
+ }))
+}
+
+// AddSliceIntKey marshals the given []int s
+func (enc *Encoder) AddSliceIntKey(k string, s []int) {
+ enc.SliceIntKey(k, s)
+}
+
+// SliceIntKey marshals the given []int s
+func (enc *Encoder) SliceIntKey(k string, s []int) {
+ enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
+ for _, i := range s {
+ enc.Int(i)
+ }
+ }))
+}
+
+// AddSliceFloat64 marshals the given []float64 s
+func (enc *Encoder) AddSliceFloat64(s []float64) {
+ enc.SliceFloat64(s)
+}
+
+// SliceFloat64 marshals the given []float64 s
+func (enc *Encoder) SliceFloat64(s []float64) {
+ enc.Array(EncodeArrayFunc(func(enc *Encoder) {
+ for _, i := range s {
+ enc.Float64(i)
+ }
+ }))
+}
+
+// AddSliceFloat64Key marshals the given []string s
+func (enc *Encoder) AddSliceFloat64Key(k string, s []float64) {
+ enc.SliceFloat64Key(k, s)
+}
+
+// SliceFloat64Key marshals the given []string s
+func (enc *Encoder) SliceFloat64Key(k string, s []float64) {
+ enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
+ for _, i := range s {
+ enc.Float64(i)
+ }
+ }))
+}
+
+// AddSliceBool marshals the given []bool s
+func (enc *Encoder) AddSliceBool(s []bool) {
+ enc.SliceBool(s)
+}
+
+// SliceBool marshals the given []bool s
+func (enc *Encoder) SliceBool(s []bool) {
+ enc.Array(EncodeArrayFunc(func(enc *Encoder) {
+ for _, i := range s {
+ enc.Bool(i)
+ }
+ }))
+}
+
+// AddSliceBoolKey marshals the given []bool s
+func (enc *Encoder) AddSliceBoolKey(k string, s []bool) {
+ enc.SliceBoolKey(k, s)
+}
+
+// SliceBoolKey marshals the given []bool s
+func (enc *Encoder) SliceBoolKey(k string, s []bool) {
+ enc.ArrayKey(k, EncodeArrayFunc(func(enc *Encoder) {
+ for _, i := range s {
+ enc.Bool(i)
+ }
+ }))
+}
diff --git a/encode_slice_test.go b/encode_slice_test.go
@@ -0,0 +1,89 @@
+package gojay
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func (s *slicesTestObject) MarshalJSONObject(enc *Encoder) {
+ enc.AddSliceStringKey("sliceString", s.sliceString)
+ enc.AddSliceIntKey("sliceInt", s.sliceInt)
+ enc.AddSliceFloat64Key("sliceFloat64", s.sliceFloat64)
+ enc.AddSliceBoolKey("sliceBool", s.sliceBool)
+}
+
+func (s *slicesTestObject) IsNil() bool {
+ return s == nil
+}
+
+func TestEncodeSlices(t *testing.T) {
+ testCases := []struct {
+ name string
+ json string
+ obj slicesTestObject
+ }{
+ {
+ name: "basic slice string",
+ json: `{
+ "sliceString": ["foo","bar"],
+ "sliceInt": [],
+ "sliceFloat64": [],
+ "sliceBool": []
+ }`,
+ obj: slicesTestObject{
+ sliceString: []string{"foo", "bar"},
+ },
+ },
+ {
+ name: "basic slice bool",
+ json: `{
+ "sliceString": [],
+ "sliceInt": [],
+ "sliceFloat64": [],
+ "sliceBool": [true,false]
+ }`,
+ obj: slicesTestObject{
+ sliceBool: []bool{true, false},
+ },
+ },
+ {
+ name: "basic slice int",
+ json: `{
+ "sliceString": [],
+ "sliceFloat64": [],
+ "sliceInt": [1,2,3],
+ "sliceBool": []
+ }`,
+ obj: slicesTestObject{
+ sliceInt: []int{1, 2, 3},
+ },
+ },
+ {
+ name: "basic slice float64",
+ json: `{
+ "sliceString": [],
+ "sliceFloat64": [1.3,2.4,3.1],
+ "sliceInt": [],
+ "sliceBool": []
+ }`,
+ obj: slicesTestObject{
+ sliceFloat64: []float64{1.3, 2.4, 3.1},
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(
+ testCase.name,
+ func(t *testing.T) {
+ b := strings.Builder{}
+ enc := BorrowEncoder(&b)
+ err := enc.Encode(&testCase.obj)
+ require.Nil(t, err, "err should be nil")
+ require.JSONEq(t, testCase.json, b.String())
+ },
+ )
+ }
+}