Skip to content

Commit a7c12f1

Browse files
authored
Merge pull request #104 from francoispqt/feature/built-in-slices
Feature/built in slices
2 parents bf5c9d9 + 10ed323 commit a7c12f1

10 files changed

+785
-6
lines changed

decode_array.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (dec *Decoder) AddArray(v UnmarshalerJSONArray) error {
212212
}
213213

214214
// AddArrayNull decodes the JSON value within an object or an array to a UnmarshalerJSONArray.
215-
func (dec *Decoder) AddArrayNull(v UnmarshalerJSONArray) error {
215+
func (dec *Decoder) AddArrayNull(v interface{}) error {
216216
return dec.ArrayNull(v)
217217
}
218218

decode_array_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ func TestDecodeArrayNullPtr(t *testing.T) {
412412
assert.Nil(t, err)
413413
assert.Nil(t, o.SubArray)
414414
})
415+
t.Run("sub array err, not closing arr", func(t *testing.T) {
416+
var o = &ObjectArrayNull{}
417+
var err = UnmarshalJSONObject([]byte(`{"subarray": [ `), o)
418+
assert.NotNil(t, err)
419+
})
420+
t.Run("sub array err, invalid string", func(t *testing.T) {
421+
var o = &ObjectArrayNull{}
422+
var err = UnmarshalJSONObject([]byte(`{"subarray":[",]}`), o)
423+
assert.NotNil(t, err)
424+
})
425+
t.Run("sub array err, invalid null", func(t *testing.T) {
426+
var o = &ObjectArrayNull{}
427+
var err = UnmarshalJSONObject([]byte(`{"subarray":nll}`), o)
428+
assert.NotNil(t, err)
429+
})
430+
t.Run("sub array err, empty", func(t *testing.T) {
431+
var o = &ObjectArrayNull{}
432+
var err = UnmarshalJSONObject([]byte(`{"subarray":`), o)
433+
assert.NotNil(t, err)
434+
})
415435
}
416436

417437
type testChannelArray chan *TestObj

decode_number_int_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,12 @@ func TestDecoderInt64(t *testing.T) {
734734
json: "3e-3",
735735
expectedResult: 0,
736736
},
737+
{
738+
name: "before-exp-err-too-big",
739+
json: "10.11231242345325435464364643e1",
740+
expectedResult: 0,
741+
err: true,
742+
},
737743
{
738744
name: "error3",
739745
json: "0E40",
@@ -1401,6 +1407,12 @@ func TestDecoderInt32(t *testing.T) {
14011407
json: "-8e+005",
14021408
expectedResult: -800000,
14031409
},
1410+
{
1411+
name: "before-exp-err-too-big",
1412+
json: "10.11231242345325435464364643e1",
1413+
expectedResult: 0,
1414+
err: true,
1415+
},
14041416
{
14051417
name: "exponent-err-",
14061418
json: "0.1e",
@@ -2016,6 +2028,11 @@ func TestDecoderInt16(t *testing.T) {
20162028
json: "1.2E2",
20172029
expectedResult: 120,
20182030
},
2031+
{
2032+
name: "exponent too big",
2033+
json: "1000.202302302422324435342E2",
2034+
err: true,
2035+
},
20192036
{
20202037
name: "basic-exponent-positive-positive-exp1",
20212038
json: "3.5e+001 ",
@@ -2751,6 +2768,7 @@ func TestDecoderInt8(t *testing.T) {
27512768
json: "-3e01",
27522769
expectedResult: -30,
27532770
},
2771+
27542772
{
27552773
name: "error3",
27562774
json: "0E40",
@@ -2764,6 +2782,12 @@ func TestDecoderInt8(t *testing.T) {
27642782
expectedResult: 0,
27652783
err: true,
27662784
},
2785+
{
2786+
name: "before-exp-err-too-big",
2787+
json: "10.11231242345325435464364643e1",
2788+
expectedResult: 0,
2789+
err: true,
2790+
},
27672791
{
27682792
name: "exponent-err-too-big",
27692793
json: "0.1e10000000000000000000",

decode_object_test.go

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ func (o *ObjectNull) UnmarshalJSONObject(dec *Decoder, k string) error {
939939
case "subobject":
940940
return dec.ObjectNull(&o.SubObject)
941941
case "subarray":
942-
return dec.ArrayNull(&o.SubArray)
942+
return dec.AddArrayNull(&o.SubArray)
943943
}
944944
return nil
945945
}
@@ -948,6 +948,25 @@ func (o *ObjectNull) NKeys() int {
948948
return 2
949949
}
950950

951+
type ObjectNullZeroNKeys struct {
952+
SubObject *ObjectNullZeroNKeys
953+
SubArray *testSliceBools
954+
}
955+
956+
func (o *ObjectNullZeroNKeys) UnmarshalJSONObject(dec *Decoder, k string) error {
957+
switch k {
958+
case "subobject":
959+
return dec.AddObjectNull(&o.SubObject)
960+
case "subarray":
961+
return dec.AddArrayNull(&o.SubArray)
962+
}
963+
return nil
964+
}
965+
966+
func (o *ObjectNullZeroNKeys) NKeys() int {
967+
return 0
968+
}
969+
951970
func TestDecodeObjectNull(t *testing.T) {
952971
t.Run("sub obj should not be nil", func(t *testing.T) {
953972
var o = &ObjectNull{}
@@ -985,6 +1004,49 @@ func TestDecodeObjectNull(t *testing.T) {
9851004
assert.Nil(t, o.SubObject)
9861005
},
9871006
)
1007+
t.Run(
1008+
"skip data",
1009+
func(t *testing.T) {
1010+
var o = &ObjectNull{}
1011+
var err = UnmarshalJSONObject([]byte(`{
1012+
"subobject": {
1013+
"subobject": {},
1014+
"subarray": [],
1015+
"subarray": [],
1016+
"skipped": ""
1017+
}
1018+
}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1019+
return dec.ObjectNull(&o.SubObject)
1020+
}))
1021+
assert.Nil(t, err)
1022+
assert.NotNil(t, o.SubObject)
1023+
assert.Nil(t, o.SubArray)
1024+
},
1025+
)
1026+
t.Run(
1027+
"skip data not child",
1028+
func(t *testing.T) {
1029+
var o = &ObjectNull{}
1030+
var dec = NewDecoder(strings.NewReader(`{
1031+
"subobject": {},
1032+
"subarray": [],
1033+
"subarray": [],
1034+
"skipped": ""
1035+
}`))
1036+
var _, err = dec.decodeObjectNull(&o)
1037+
assert.Nil(t, err)
1038+
assert.NotNil(t, o.SubObject)
1039+
},
1040+
)
1041+
t.Run(
1042+
"err empty json",
1043+
func(t *testing.T) {
1044+
var o = &ObjectNull{}
1045+
var dec = NewDecoder(strings.NewReader(``))
1046+
var _, err = dec.decodeObjectNull(&o)
1047+
assert.NotNil(t, err)
1048+
},
1049+
)
9881050
t.Run(
9891051
"should return an error as type is not ptr",
9901052
func(t *testing.T) {
@@ -1060,6 +1122,17 @@ func TestDecodeObjectNull(t *testing.T) {
10601122
assert.IsType(t, InvalidUnmarshalError(""), err)
10611123
},
10621124
)
1125+
t.Run(
1126+
"invalid JSON for object",
1127+
func(t *testing.T) {
1128+
var o = &ObjectNull{}
1129+
var err = UnmarshalJSONObject([]byte(`{"subobject":{"subobject":{"a":a}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1130+
return dec.ObjectNull(&o.SubObject)
1131+
}))
1132+
assert.NotNil(t, err)
1133+
assert.IsType(t, InvalidJSONError(""), err)
1134+
},
1135+
)
10631136
t.Run(
10641137
"invalid JSON for object",
10651138
func(t *testing.T) {
@@ -1071,6 +1144,17 @@ func TestDecodeObjectNull(t *testing.T) {
10711144
assert.IsType(t, InvalidJSONError(""), err)
10721145
},
10731146
)
1147+
t.Run(
1148+
"invalid JSON for object",
1149+
func(t *testing.T) {
1150+
var o = &ObjectNull{}
1151+
var err = UnmarshalJSONObject([]byte(`{"subobject":{"subobject":{"sub}}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1152+
return dec.ObjectNull(&o.SubObject)
1153+
}))
1154+
assert.NotNil(t, err)
1155+
assert.IsType(t, InvalidJSONError(""), err)
1156+
},
1157+
)
10741158
t.Run(
10751159
"invalid JSON for object",
10761160
func(t *testing.T) {
@@ -1122,6 +1206,92 @@ func TestDecodeObjectNull(t *testing.T) {
11221206
assert.IsType(t, InvalidJSONError(""), err)
11231207
},
11241208
)
1209+
t.Run(
1210+
"zero nkeys, no error, two keys",
1211+
func(t *testing.T) {
1212+
var o = &ObjectNullZeroNKeys{}
1213+
var err = UnmarshalJSONObject([]byte(`{
1214+
"subobject": {
1215+
"subobject": {
1216+
"subobject":{}
1217+
},
1218+
"subarray": []
1219+
}
1220+
}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1221+
return dec.ObjectNull(&o.SubObject)
1222+
}))
1223+
assert.Nil(t, err)
1224+
},
1225+
)
1226+
t.Run(
1227+
"zero nkeys, no error, two keys, skip data",
1228+
func(t *testing.T) {
1229+
var o = &ObjectNullZeroNKeys{}
1230+
var err = UnmarshalJSONObject([]byte(`{
1231+
"subobject": {
1232+
"subobject": {
1233+
"subobject":{}
1234+
},
1235+
"subarray": [],
1236+
"skipped": 1
1237+
}
1238+
}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1239+
return dec.ObjectNull(&o.SubObject)
1240+
}))
1241+
assert.Nil(t, err)
1242+
},
1243+
)
1244+
t.Run(
1245+
"zero nkeys, error skip data",
1246+
func(t *testing.T) {
1247+
var o = &ObjectNullZeroNKeys{}
1248+
var err = UnmarshalJSONObject([]byte(`{
1249+
"subobject": {
1250+
"subobject": {
1251+
"subobject":{}
1252+
},
1253+
"subarray": [],
1254+
"skippedInvalid": "q
1255+
}
1256+
}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1257+
return dec.ObjectNull(&o.SubObject)
1258+
}))
1259+
assert.NotNil(t, err)
1260+
assert.IsType(t, InvalidJSONError(""), err)
1261+
},
1262+
)
1263+
t.Run(
1264+
"zero nkeys, error invalid json in keys",
1265+
func(t *testing.T) {
1266+
var o = &ObjectNullZeroNKeys{}
1267+
var err = UnmarshalJSONObject([]byte(`{
1268+
"subobject": {
1269+
"subobj
1270+
}
1271+
}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1272+
return dec.ObjectNull(&o.SubObject)
1273+
}))
1274+
assert.NotNil(t, err)
1275+
assert.IsType(t, InvalidJSONError(""), err)
1276+
},
1277+
)
1278+
t.Run(
1279+
"zero nkeys, error invalid json, sub object",
1280+
func(t *testing.T) {
1281+
var o = &ObjectNullZeroNKeys{}
1282+
var err = UnmarshalJSONObject([]byte(`{
1283+
"subobject": {
1284+
"subobject": {
1285+
"subobj
1286+
}
1287+
}
1288+
}`), DecodeObjectFunc(func(dec *Decoder, k string) error {
1289+
return dec.ObjectNull(&o.SubObject)
1290+
}))
1291+
assert.NotNil(t, err)
1292+
assert.IsType(t, InvalidJSONError(""), err)
1293+
},
1294+
)
11251295
}
11261296

11271297
func TestDecodeObjectComplex(t *testing.T) {

decode_slice.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package gojay
2+
3+
// AddSliceString unmarshals the next JSON array of strings to the given *[]string s
4+
func (dec *Decoder) AddSliceString(s *[]string) error {
5+
return dec.SliceString(s)
6+
}
7+
8+
// SliceString unmarshals the next JSON array of strings to the given *[]string s
9+
func (dec *Decoder) SliceString(s *[]string) error {
10+
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
11+
var str string
12+
if err := dec.String(&str); err != nil {
13+
return err
14+
}
15+
*s = append(*s, str)
16+
return nil
17+
}))
18+
19+
if err != nil {
20+
return err
21+
}
22+
return nil
23+
}
24+
25+
// AddSliceInt unmarshals the next JSON array of integers to the given *[]int s
26+
func (dec *Decoder) AddSliceInt(s *[]int) error {
27+
return dec.SliceInt(s)
28+
}
29+
30+
// SliceInt unmarshals the next JSON array of integers to the given *[]int s
31+
func (dec *Decoder) SliceInt(s *[]int) error {
32+
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
33+
var i int
34+
if err := dec.Int(&i); err != nil {
35+
return err
36+
}
37+
*s = append(*s, i)
38+
return nil
39+
}))
40+
41+
if err != nil {
42+
return err
43+
}
44+
return nil
45+
}
46+
47+
// AddFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
48+
func (dec *Decoder) AddSliceFloat64(s *[]float64) error {
49+
return dec.SliceFloat64(s)
50+
}
51+
52+
// SliceFloat64 unmarshals the next JSON array of floats to the given *[]float64 s
53+
func (dec *Decoder) SliceFloat64(s *[]float64) error {
54+
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
55+
var i float64
56+
if err := dec.Float64(&i); err != nil {
57+
return err
58+
}
59+
*s = append(*s, i)
60+
return nil
61+
}))
62+
63+
if err != nil {
64+
return err
65+
}
66+
return nil
67+
}
68+
69+
// AddBool unmarshals the next JSON array of boolegers to the given *[]bool s
70+
func (dec *Decoder) AddSliceBool(s *[]bool) error {
71+
return dec.SliceBool(s)
72+
}
73+
74+
// SliceBool unmarshals the next JSON array of boolegers to the given *[]bool s
75+
func (dec *Decoder) SliceBool(s *[]bool) error {
76+
err := dec.Array(DecodeArrayFunc(func(dec *Decoder) error {
77+
var b bool
78+
if err := dec.Bool(&b); err != nil {
79+
return err
80+
}
81+
*s = append(*s, b)
82+
return nil
83+
}))
84+
85+
if err != nil {
86+
return err
87+
}
88+
return nil
89+
}

0 commit comments

Comments
 (0)