1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package json
import (
"bytes"
gojson "encoding/json"
"io/ioutil"
"reflect"
"strings"
"testing"
)
func TestSyntaxErrorOffset(t *testing.T) {
malformedJSON :=
[]byte(`{
"test1":true,
"test2":true
"test3":true
}`)
err1 := UnmarshalCaseSensitivePreserveInts(malformedJSON, &map[string]interface{}{})
if err1 == nil {
t.Fatal("expected err, got none")
}
ok1, offset1 := SyntaxErrorOffset(err1)
if !ok1 {
t.Fatal("expected ok, got false")
}
err2 := gojson.Unmarshal(malformedJSON, &map[string]interface{}{})
if err2 == nil {
t.Fatal("expected err, got none")
}
ok2, offset2 := SyntaxErrorOffset(err2)
if !ok2 {
t.Fatal("expected ok, got false")
}
if offset1 != offset2 {
t.Fatalf("offset mismatch from stdlib and custom: %d != %d", offset1, offset2)
}
}
func TestUnmarshal(t *testing.T) {
type Obj struct {
A int `json:"a"`
B int `json:"b"`
C map[string]string `json:"c"`
D int
E int
}
testcases := []struct {
name string
in string
to func() interface{}
expect interface{}
expectErr string
expectStrictErrs []string
}{
{
name: "simple",
in: `{"a":1}`,
to: func() interface{} { return map[string]interface{}{} },
expect: map[string]interface{}{"a": int64(1)},
},
{
name: "case-sensitive",
in: `{"a":1,"A":2,"B":3}`,
to: func() interface{} { return &Obj{} },
expect: &Obj{A: 1}, // case-mismatches don't decode
expectStrictErrs: []string{`unknown field "A"`, `unknown field "B"`}, // multiple strict errors are returned
},
{
name: "duplicate untyped",
in: `{"a":1,"a":2,"b":1,"b":2}`,
to: func() interface{} { return map[string]interface{}{} },
expect: map[string]interface{}{"a": int64(2), "b": int64(2)}, // last duplicates win
expectStrictErrs: []string{`duplicate field "a"`, `duplicate field "b"`}, // multiple strict errors are returned
},
{
name: "duplicate typed",
in: `{"a":1,"a":2,"b":1,"b":2}`,
to: func() interface{} { return &Obj{} },
expect: &Obj{A: 2, B: 2}, // last duplicates win
expectStrictErrs: []string{`duplicate field "a"`, `duplicate field "b"`}, // multiple strict errors are returned
},
{
name: "duplicate map field",
in: `{"c":{"a":"1","a":"2","b":"1","b":"2"}}`,
to: func() interface{} { return &Obj{} },
expect: &Obj{C: map[string]string{"a": "2", "b": "2"}}, // last duplicates win
expectStrictErrs: []string{`duplicate field "c.a"`, `duplicate field "c.b"`}, // multiple strict errors are returned
},
{
name: "unknown fields",
in: `{"a":1,"unknown":true,"unknown2":false,"b":2}`,
to: func() interface{} { return &Obj{} },
expect: &Obj{A: 1, B: 2}, // data is populated
expectStrictErrs: []string{`unknown field "unknown"`, `unknown field "unknown2"`}, // multiple strict errors are returned
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
unmarshalTo := tc.to()
err := UnmarshalCaseSensitivePreserveInts([]byte(tc.in), &unmarshalTo)
strictUnmarshalTo := tc.to()
strictErrors, strictErr := UnmarshalStrict([]byte(tc.in), &strictUnmarshalTo)
decodeTo := tc.to()
decodeErr := NewDecoderCaseSensitivePreserveInts(bytes.NewBuffer([]byte(tc.in))).Decode(&decodeTo)
// ensure expected errors are returned
if (len(tc.expectErr) > 0) != (err != nil) {
t.Fatalf("expected err=%v, got %v", len(tc.expectErr) > 0, err)
}
if len(tc.expectErr) > 0 && !strings.Contains(err.Error(), tc.expectErr) {
t.Fatalf("expected error containing '%s', got %v", tc.expectErr, err)
}
// ensure expected strict errors are returned
if len(tc.expectStrictErrs) != len(strictErrors) {
t.Fatalf("expected %d strict errors, got %v", len(tc.expectStrictErrs), strictErrors)
}
for i := range tc.expectStrictErrs {
strictFieldErr, ok := strictErrors[i].(FieldError)
if !ok {
t.Fatalf("strict error does not implement FieldError: %v", strictErrors[i])
}
if !strings.Contains(strictFieldErr.Error(), tc.expectStrictErrs[i]) {
t.Fatalf("expected strict errors:\n %s\ngot:\n %v", strings.Join(tc.expectStrictErrs, "\n "), strictErrors)
}
}
// ensure expected decode errors are returned
if (len(tc.expectErr) > 0) != (decodeErr != nil) {
t.Fatalf("expected err=%v, got %v", len(tc.expectErr) > 0, decodeErr)
}
if len(tc.expectErr) > 0 && !strings.Contains(decodeErr.Error(), tc.expectErr) {
t.Fatalf("expected error containing '%s', got %v", tc.expectErr, decodeErr)
}
// ensure we got the expected object back
if !reflect.DeepEqual(tc.expect, unmarshalTo) {
t.Fatalf("expected\n%#v\ngot\n%#v", tc.expect, unmarshalTo)
}
if !reflect.DeepEqual(tc.expect, decodeTo) {
t.Fatalf("expected\n%#v\ngot\n%#v", tc.expect, decodeTo)
}
// ensure Unmarshal and UnmarshalStrict return identical errors and objects
if !reflect.DeepEqual(err, strictErr) {
t.Fatalf("unmarshal/strictunmarshal returned different errors:\n%v\n%v", err, strictErr)
}
if !reflect.DeepEqual(unmarshalTo, strictUnmarshalTo) {
t.Fatalf("unmarshal/strictunmarshal returned different objects:\n%#v\n%#v", unmarshalTo, strictUnmarshalTo)
}
// ensure Unmarshal and Decode return identical errors and objects
if !reflect.DeepEqual(err, decodeErr) {
t.Fatalf("unmarshal/decode returned different errors:\n%v\n%v", err, decodeErr)
}
if !reflect.DeepEqual(unmarshalTo, decodeTo) {
t.Fatalf("unmarshal/decode returned different objects:\n%#v\n%#v", unmarshalTo, decodeTo)
}
})
}
}
func BenchmarkUnmarshal(b *testing.B) {
testcases := []struct {
name string
unmarshal func(b *testing.B, data []byte, v interface{})
}{
{
name: "stdlib",
unmarshal: func(b *testing.B, data []byte, v interface{}) {
if err := gojson.Unmarshal(data, v); err != nil {
b.Fatal(err)
}
},
},
{
name: "unmarshal",
unmarshal: func(b *testing.B, data []byte, v interface{}) {
if err := UnmarshalCaseSensitivePreserveInts(data, v); err != nil {
b.Fatal(err)
}
},
},
{
name: "strict",
unmarshal: func(b *testing.B, data []byte, v interface{}) {
if strict, err := UnmarshalStrict(data, v); err != nil {
b.Fatal(err)
} else if len(strict) > 0 {
b.Fatal(strict)
}
},
},
{
name: "strict-custom",
unmarshal: func(b *testing.B, data []byte, v interface{}) {
if strict, err := UnmarshalStrict(data, v, DisallowDuplicateFields, DisallowUnknownFields); err != nil {
b.Fatal(err)
} else if len(strict) > 0 {
b.Fatal(strict)
}
},
},
}
data, err := ioutil.ReadFile("testdata/bench.json")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for _, tc := range testcases {
b.Run("typed_"+tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
tc.unmarshal(b, data, &A{})
}
})
}
for _, tc := range testcases {
b.Run("untyped_"+tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
tc.unmarshal(b, data, &map[string]interface{}{})
}
})
}
}
type A struct {
Int int `json:"int"`
Bool bool `json:"bool"`
String string `json:"string"`
StringMap map[string]string `json:"map"`
ObjectArray []A `json:"array"`
Small Small `json:"small"`
Big Big `json:"big"`
Custom Custom `json:"custom"`
}
type Small struct {
F01 string `json:"f01"`
F02 string `json:"f02"`
F03 string `json:"f03"`
F04 string `json:"f04"`
F05 string `json:"f05"`
F06 string `json:"f06"`
F07 string `json:"f07"`
F08 string `json:"f08"`
F09 string `json:"f09"`
F10 string `json:"f10"`
F11 string `json:"f11"`
F12 string `json:"f12"`
F13 string `json:"f13"`
F14 string `json:"f14"`
F15 string `json:"f15"`
F16 string `json:"f16"`
F17 string `json:"f17"`
F18 string `json:"f18"`
F19 string `json:"f19"`
F20 string `json:"f20"`
F21 string `json:"f21"`
F22 string `json:"f22"`
F23 string `json:"f23"`
F24 string `json:"f24"`
F25 string `json:"f25"`
F26 string `json:"f26"`
F27 string `json:"f27"`
F28 string `json:"f28"`
F29 string `json:"f29"`
F30 string `json:"f30"`
F31 string `json:"f31"`
F32 string `json:"f32"`
F33 string `json:"f33"`
F34 string `json:"f34"`
F35 string `json:"f35"`
F36 string `json:"f36"`
F37 string `json:"f37"`
F38 string `json:"f38"`
F39 string `json:"f39"`
F40 string `json:"f40"`
F41 string `json:"f41"`
F42 string `json:"f42"`
F43 string `json:"f43"`
F44 string `json:"f44"`
F45 string `json:"f45"`
F46 string `json:"f46"`
F47 string `json:"f47"`
F48 string `json:"f48"`
F49 string `json:"f49"`
F50 string `json:"f50"`
F51 string `json:"f51"`
F52 string `json:"f52"`
F53 string `json:"f53"`
F54 string `json:"f54"`
F55 string `json:"f55"`
F56 string `json:"f56"`
F57 string `json:"f57"`
F58 string `json:"f58"`
F59 string `json:"f59"`
F60 string `json:"f60"`
F61 string `json:"f61"`
F62 string `json:"f62"`
F63 string `json:"f63"`
F64 string `json:"f64"`
}
type Big struct {
F01 string `json:"f01"`
F02 string `json:"f02"`
F03 string `json:"f03"`
F04 string `json:"f04"`
F05 string `json:"f05"`
F06 string `json:"f06"`
F07 string `json:"f07"`
F08 string `json:"f08"`
F09 string `json:"f09"`
F10 string `json:"f10"`
F11 string `json:"f11"`
F12 string `json:"f12"`
F13 string `json:"f13"`
F14 string `json:"f14"`
F15 string `json:"f15"`
F16 string `json:"f16"`
F17 string `json:"f17"`
F18 string `json:"f18"`
F19 string `json:"f19"`
F20 string `json:"f20"`
F21 string `json:"f21"`
F22 string `json:"f22"`
F23 string `json:"f23"`
F24 string `json:"f24"`
F25 string `json:"f25"`
F26 string `json:"f26"`
F27 string `json:"f27"`
F28 string `json:"f28"`
F29 string `json:"f29"`
F30 string `json:"f30"`
F31 string `json:"f31"`
F32 string `json:"f32"`
F33 string `json:"f33"`
F34 string `json:"f34"`
F35 string `json:"f35"`
F36 string `json:"f36"`
F37 string `json:"f37"`
F38 string `json:"f38"`
F39 string `json:"f39"`
F40 string `json:"f40"`
F41 string `json:"f41"`
F42 string `json:"f42"`
F43 string `json:"f43"`
F44 string `json:"f44"`
F45 string `json:"f45"`
F46 string `json:"f46"`
F47 string `json:"f47"`
F48 string `json:"f48"`
F49 string `json:"f49"`
F50 string `json:"f50"`
F51 string `json:"f51"`
F52 string `json:"f52"`
F53 string `json:"f53"`
F54 string `json:"f54"`
F55 string `json:"f55"`
F56 string `json:"f56"`
F57 string `json:"f57"`
F58 string `json:"f58"`
F59 string `json:"f59"`
F60 string `json:"f60"`
F61 string `json:"f61"`
F62 string `json:"f62"`
F63 string `json:"f63"`
F64 string `json:"f64"`
F65 string `json:"f65"`
}
type Custom struct{}
func (c *Custom) UnmarshalJSON(data []byte) error {
return nil
}
|