| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package json |
| 18 |
|
| 19 |
import ( |
| 20 |
gojson "encoding/json" |
| 21 |
"fmt" |
| 22 |
"math" |
| 23 |
"reflect" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
"testing" |
| 27 |
) |
| 28 |
|
| 29 |
func TestUnmarshalWithOptions(t *testing.T) { |
| 30 |
type Typed struct { |
| 31 |
A int `json:"a"` |
| 32 |
} |
| 33 |
|
| 34 |
testcases := []struct { |
| 35 |
name string |
| 36 |
in string |
| 37 |
to interface{} |
| 38 |
options []UnmarshalOpt |
| 39 |
expect interface{} |
| 40 |
expectErr bool |
| 41 |
}{ |
| 42 |
{ |
| 43 |
name: "default untyped", |
| 44 |
in: `{"a":1}`, |
| 45 |
to: map[string]interface{}{}, |
| 46 |
expect: map[string]interface{}{"a": float64(1)}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "default typed", |
| 50 |
in: `{"a":1, "unknown":"foo"}`, |
| 51 |
to: &Typed{}, |
| 52 |
expect: &Typed{A: 1}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "usenumbers untyped", |
| 56 |
in: `{"a":1}`, |
| 57 |
to: map[string]interface{}{}, |
| 58 |
options: []UnmarshalOpt{UseNumber}, |
| 59 |
expect: map[string]interface{}{"a": gojson.Number("1")}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "usenumbers typed", |
| 63 |
in: `{"a":1}`, |
| 64 |
to: &Typed{}, |
| 65 |
expect: &Typed{A: 1}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "disallowunknown untyped", |
| 69 |
in: `{"a":1,"unknown":"foo"}`, |
| 70 |
to: map[string]interface{}{}, |
| 71 |
options: []UnmarshalOpt{DisallowUnknownFields}, |
| 72 |
expect: map[string]interface{}{"a": float64(1), "unknown": "foo"}, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "disallowunknown typed", |
| 76 |
in: `{"a":1,"unknown":"foo"}`, |
| 77 |
to: &Typed{}, |
| 78 |
options: []UnmarshalOpt{DisallowUnknownFields}, |
| 79 |
expectErr: true, |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
for _, tc := range testcases { |
| 84 |
t.Run(tc.name, func(t *testing.T) { |
| 85 |
err := Unmarshal([]byte(tc.in), &tc.to, tc.options...) |
| 86 |
if tc.expectErr != (err != nil) { |
| 87 |
t.Fatalf("expected err=%v, got %v", tc.expectErr, err) |
| 88 |
} |
| 89 |
if tc.expectErr { |
| 90 |
return |
| 91 |
} |
| 92 |
if !reflect.DeepEqual(tc.expect, tc.to) { |
| 93 |
t.Fatalf("expected\n%#v\ngot\n%#v", tc.expect, tc.to) |
| 94 |
} |
| 95 |
}) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestStrictErrors(t *testing.T) { |
| 100 |
type SubType struct { |
| 101 |
} |
| 102 |
type Typed struct { |
| 103 |
A int `json:"a"` |
| 104 |
B map[string]SubType `json:"b"` |
| 105 |
C []SubType `json:"c"` |
| 106 |
} |
| 107 |
|
| 108 |
testcases := []struct { |
| 109 |
name string |
| 110 |
in string |
| 111 |
expectStrictErr bool |
| 112 |
expectErr string |
| 113 |
}{ |
| 114 |
{ |
| 115 |
name: "malformed 1", |
| 116 |
in: `{`, |
| 117 |
expectStrictErr: false, |
| 118 |
expectErr: `unexpected end of JSON input`, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "malformed 2", |
| 122 |
in: `{}}`, |
| 123 |
expectStrictErr: false, |
| 124 |
expectErr: `invalid character '}' after top-level value`, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "malformed 3", |
| 128 |
in: `{,}`, |
| 129 |
expectStrictErr: false, |
| 130 |
expectErr: `invalid character ',' looking for beginning of object key string`, |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "type error", |
| 134 |
in: `{"a":true}`, |
| 135 |
expectStrictErr: false, |
| 136 |
expectErr: `json: cannot unmarshal bool into Go struct field Typed.a of type int`, |
| 137 |
}, |
| 138 |
{ |
| 139 |
name: "unknown", |
| 140 |
in: `{"a":1,"unknown":true,"unknown":false}`, |
| 141 |
expectStrictErr: true, |
| 142 |
expectErr: `json: unknown field "unknown"`, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "unknowns", |
| 146 |
in: `{"a":1,"unknown":true,"unknown2":true,"unknown":true,"unknown2":true}`, |
| 147 |
expectStrictErr: true, |
| 148 |
expectErr: `json: unknown field "unknown", unknown field "unknown2"`, |
| 149 |
}, |
| 150 |
{ |
| 151 |
name: "nested unknowns", |
| 152 |
in: `{"a":1,"unknown":true,"unknown2":true,"unknown":true,"unknown2":true,"b":{"a":{"unknown":true}},"c":[{"unknown":true},{"unknown":true}]}`, |
| 153 |
expectStrictErr: true, |
| 154 |
expectErr: `json: unknown field "unknown", unknown field "unknown2", unknown field "b.a.unknown", unknown field "c[0].unknown", unknown field "c[1].unknown"`, |
| 155 |
}, |
| 156 |
{ |
| 157 |
name: "unknowns and type error", |
| 158 |
in: `{"unknown":true,"a":true}`, |
| 159 |
expectStrictErr: false, |
| 160 |
expectErr: `json: cannot unmarshal bool into Go struct field Typed.a of type int`, |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "unknowns and malformed error", |
| 164 |
in: `{"unknown":true}}`, |
| 165 |
expectStrictErr: false, |
| 166 |
expectErr: `invalid character '}' after top-level value`, |
| 167 |
}, |
| 168 |
} |
| 169 |
|
| 170 |
for _, tc := range testcases { |
| 171 |
t.Run(tc.name, func(t *testing.T) { |
| 172 |
err := Unmarshal([]byte(tc.in), &Typed{}, DisallowUnknownFields) |
| 173 |
if err == nil { |
| 174 |
t.Fatal("expected error, got none") |
| 175 |
} |
| 176 |
_, isStrictErr := err.(*UnmarshalStrictError) |
| 177 |
if tc.expectStrictErr != isStrictErr { |
| 178 |
t.Fatalf("expected strictErr=%v, got %v: %v", tc.expectStrictErr, isStrictErr, err) |
| 179 |
} |
| 180 |
if err.Error() != tc.expectErr { |
| 181 |
t.Fatalf("expected error\n%s\n%s", tc.expectErr, err) |
| 182 |
} |
| 183 |
t.Log(err) |
| 184 |
}) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
func TestCaseSensitive(t *testing.T) { |
| 189 |
type Embedded1 struct { |
| 190 |
C int `json:"c"` |
| 191 |
D int |
| 192 |
} |
| 193 |
type Embedded2 struct { |
| 194 |
E int `json:"e"` |
| 195 |
F int |
| 196 |
} |
| 197 |
|
| 198 |
type Obj struct { |
| 199 |
A int `json:"a"` |
| 200 |
B int |
| 201 |
Embedded1 `json:",inline"` |
| 202 |
Embedded2 |
| 203 |
} |
| 204 |
|
| 205 |
testcases := []struct { |
| 206 |
name string |
| 207 |
in string |
| 208 |
to interface{} |
| 209 |
expect interface{} |
| 210 |
}{ |
| 211 |
{ |
| 212 |
name: "tagged", |
| 213 |
in: `{"A":"1","A":2,"a":3,"A":4,"A":"5"}`, |
| 214 |
to: &Obj{}, |
| 215 |
expect: &Obj{A: 3}, |
| 216 |
}, |
| 217 |
{ |
| 218 |
name: "untagged", |
| 219 |
in: `{"b":"1","b":2,"B":3,"b":4,"b":"5"}`, |
| 220 |
to: &Obj{}, |
| 221 |
expect: &Obj{B: 3}, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "inline embedded tagged subfield", |
| 225 |
in: `{"C":"1","C":2,"c":3,"C":4,"C":"5"}`, |
| 226 |
to: &Obj{}, |
| 227 |
expect: &Obj{Embedded1: Embedded1{C: 3}}, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "inline embedded untagged subfield", |
| 231 |
in: `{"d":"1","d":2,"D":3,"d":4,"d":"5"}`, |
| 232 |
to: &Obj{}, |
| 233 |
expect: &Obj{Embedded1: Embedded1{D: 3}}, |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "inline embedded field name", |
| 237 |
in: `{"Embedded1":{"c":3}}`, |
| 238 |
to: &Obj{}, |
| 239 |
expect: &Obj{}, |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "inline embedded empty name", |
| 243 |
in: `{"":{"c":3}}`, |
| 244 |
to: &Obj{}, |
| 245 |
expect: &Obj{}, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "untagged embedded tagged subfield", |
| 249 |
in: `{"E":"1","E":2,"e":3,"E":4,"E":"5"}`, |
| 250 |
to: &Obj{}, |
| 251 |
expect: &Obj{Embedded2: Embedded2{E: 3}}, |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "untagged embedded untagged subfield", |
| 255 |
in: `{"f":"1","f":2,"F":3,"f":4,"f":"5"}`, |
| 256 |
to: &Obj{}, |
| 257 |
expect: &Obj{Embedded2: Embedded2{F: 3}}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "untagged embedded field name", |
| 261 |
in: `{"Embedded2":{"e":3}}`, |
| 262 |
to: &Obj{}, |
| 263 |
expect: &Obj{}, |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "untagged embedded empty name", |
| 267 |
in: `{"":{"e":3}}`, |
| 268 |
to: &Obj{}, |
| 269 |
expect: &Obj{}, |
| 270 |
}, |
| 271 |
} |
| 272 |
|
| 273 |
for _, tc := range testcases { |
| 274 |
t.Run(tc.name, func(t *testing.T) { |
| 275 |
if err := Unmarshal([]byte(tc.in), &tc.to, CaseSensitive); err != nil { |
| 276 |
t.Fatal(err) |
| 277 |
} |
| 278 |
if !reflect.DeepEqual(tc.expect, tc.to) { |
| 279 |
t.Fatalf("expected\n%#v\ngot\n%#v", tc.expect, tc.to) |
| 280 |
} |
| 281 |
}) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
func TestPreserveInts(t *testing.T) { |
| 286 |
testCases := []struct { |
| 287 |
In string |
| 288 |
Data interface{} |
| 289 |
Out string |
| 290 |
Err bool |
| 291 |
}{ |
| 292 |
|
| 293 |
{ |
| 294 |
In: `0`, |
| 295 |
Data: int64(0), |
| 296 |
Out: `0`, |
| 297 |
}, |
| 298 |
{ |
| 299 |
In: `-0`, |
| 300 |
Data: int64(-0), |
| 301 |
Out: `0`, |
| 302 |
}, |
| 303 |
{ |
| 304 |
In: `1`, |
| 305 |
Data: int64(1), |
| 306 |
Out: `1`, |
| 307 |
}, |
| 308 |
{ |
| 309 |
In: `2147483647`, |
| 310 |
Data: int64(math.MaxInt32), |
| 311 |
Out: `2147483647`, |
| 312 |
}, |
| 313 |
{ |
| 314 |
In: `-2147483648`, |
| 315 |
Data: int64(math.MinInt32), |
| 316 |
Out: `-2147483648`, |
| 317 |
}, |
| 318 |
{ |
| 319 |
In: `9223372036854775807`, |
| 320 |
Data: int64(math.MaxInt64), |
| 321 |
Out: `9223372036854775807`, |
| 322 |
}, |
| 323 |
{ |
| 324 |
In: `-9223372036854775808`, |
| 325 |
Data: int64(math.MinInt64), |
| 326 |
Out: `-9223372036854775808`, |
| 327 |
}, |
| 328 |
|
| 329 |
|
| 330 |
{ |
| 331 |
In: `9223372036854775808`, |
| 332 |
Data: float64(9223372036854775808), |
| 333 |
Out: `9223372036854776000`, |
| 334 |
}, |
| 335 |
{ |
| 336 |
In: `-9223372036854775809`, |
| 337 |
Data: float64(math.MinInt64), |
| 338 |
Out: `-9223372036854776000`, |
| 339 |
}, |
| 340 |
|
| 341 |
|
| 342 |
{ |
| 343 |
In: `0.0`, |
| 344 |
Data: float64(0), |
| 345 |
Out: `0`, |
| 346 |
}, |
| 347 |
{ |
| 348 |
In: `-0.0`, |
| 349 |
Data: float64(-0.0), |
| 350 |
Out: `-0`, |
| 351 |
}, |
| 352 |
{ |
| 353 |
In: `0.5`, |
| 354 |
Data: float64(0.5), |
| 355 |
Out: `0.5`, |
| 356 |
}, |
| 357 |
{ |
| 358 |
In: `1e3`, |
| 359 |
Data: float64(1e3), |
| 360 |
Out: `1000`, |
| 361 |
}, |
| 362 |
{ |
| 363 |
In: `1.5`, |
| 364 |
Data: float64(1.5), |
| 365 |
Out: `1.5`, |
| 366 |
}, |
| 367 |
{ |
| 368 |
In: `-0.3`, |
| 369 |
Data: float64(-.3), |
| 370 |
Out: `-0.3`, |
| 371 |
}, |
| 372 |
{ |
| 373 |
|
| 374 |
In: `3.40282346638528859811704183484516925440e+38`, |
| 375 |
Data: float64(math.MaxFloat32), |
| 376 |
Out: strconv.FormatFloat(math.MaxFloat32, 'g', -1, 64), |
| 377 |
}, |
| 378 |
{ |
| 379 |
|
| 380 |
In: `1.175494351e-38`, |
| 381 |
Data: float64(1.175494351e-38), |
| 382 |
Out: `1.175494351e-38`, |
| 383 |
}, |
| 384 |
{ |
| 385 |
|
| 386 |
In: `1.401298464324817070923729583289916131280e-45`, |
| 387 |
Data: float64(math.SmallestNonzeroFloat32), |
| 388 |
Out: strconv.FormatFloat(math.SmallestNonzeroFloat32, 'g', -1, 64), |
| 389 |
}, |
| 390 |
{ |
| 391 |
|
| 392 |
In: `1.797693134862315708145274237317043567981e+308`, |
| 393 |
Data: float64(math.MaxFloat64), |
| 394 |
Out: strconv.FormatFloat(math.MaxFloat64, 'g', -1, 64), |
| 395 |
}, |
| 396 |
{ |
| 397 |
|
| 398 |
In: `2.2250738585072014e-308`, |
| 399 |
Data: float64(2.2250738585072014e-308), |
| 400 |
Out: `2.2250738585072014e-308`, |
| 401 |
}, |
| 402 |
|
| 403 |
{ |
| 404 |
|
| 405 |
In: `4.940656458412465441765687928682213723651e-324`, |
| 406 |
Data: float64(math.SmallestNonzeroFloat64), |
| 407 |
Out: strconv.FormatFloat(math.SmallestNonzeroFloat64, 'g', -1, 64), |
| 408 |
}, |
| 409 |
|
| 410 |
{ |
| 411 |
|
| 412 |
In: `1.7976931348623159e+308`, |
| 413 |
Err: true, |
| 414 |
}, |
| 415 |
|
| 416 |
|
| 417 |
{ |
| 418 |
In: `[` + strings.Join([]string{ |
| 419 |
`null`, |
| 420 |
`true`, |
| 421 |
`false`, |
| 422 |
`0`, |
| 423 |
`9223372036854775807`, |
| 424 |
`0.0`, |
| 425 |
`0.5`, |
| 426 |
`1.0`, |
| 427 |
`1.797693134862315708145274237317043567981e+308`, |
| 428 |
`"0"`, |
| 429 |
`"A"`, |
| 430 |
`"Iñtërnâtiônàlizætiøn"`, |
| 431 |
`[null,true,1,1.0,1.5]`, |
| 432 |
`{"boolkey":true,"floatkey":1.0,"intkey":1,"nullkey":null}`, |
| 433 |
}, ",") + `]`, |
| 434 |
Data: []interface{}{ |
| 435 |
nil, |
| 436 |
true, |
| 437 |
false, |
| 438 |
int64(0), |
| 439 |
int64(math.MaxInt64), |
| 440 |
float64(0.0), |
| 441 |
float64(0.5), |
| 442 |
float64(1.0), |
| 443 |
float64(math.MaxFloat64), |
| 444 |
string("0"), |
| 445 |
string("A"), |
| 446 |
string("Iñtërnâtiônàlizætiøn"), |
| 447 |
[]interface{}{nil, true, int64(1), float64(1.0), float64(1.5)}, |
| 448 |
map[string]interface{}{"nullkey": nil, "boolkey": true, "intkey": int64(1), "floatkey": float64(1.0)}, |
| 449 |
}, |
| 450 |
Out: `[` + strings.Join([]string{ |
| 451 |
`null`, |
| 452 |
`true`, |
| 453 |
`false`, |
| 454 |
`0`, |
| 455 |
`9223372036854775807`, |
| 456 |
`0`, |
| 457 |
`0.5`, |
| 458 |
`1`, |
| 459 |
strconv.FormatFloat(math.MaxFloat64, 'g', -1, 64), |
| 460 |
`"0"`, |
| 461 |
`"A"`, |
| 462 |
`"Iñtërnâtiônàlizætiøn"`, |
| 463 |
`[null,true,1,1,1.5]`, |
| 464 |
`{"boolkey":true,"floatkey":1,"intkey":1,"nullkey":null}`, |
| 465 |
}, ",") + `]`, |
| 466 |
}, |
| 467 |
|
| 468 |
|
| 469 |
{ |
| 470 |
In: `{"boolkey":true,"floatkey":1.0,"intkey":1,"nullkey":null}`, |
| 471 |
Data: map[string]interface{}{"nullkey": nil, "boolkey": true, "intkey": int64(1), "floatkey": float64(1.0)}, |
| 472 |
Out: `{"boolkey":true,"floatkey":1,"intkey":1,"nullkey":null}`, |
| 473 |
}, |
| 474 |
} |
| 475 |
|
| 476 |
for i, tc := range testCases { |
| 477 |
t.Run(fmt.Sprintf("%d_map", i), func(t *testing.T) { |
| 478 |
|
| 479 |
inputJSON := fmt.Sprintf(`{"data":%s}`, tc.In) |
| 480 |
expectedJSON := fmt.Sprintf(`{"data":%s}`, tc.Out) |
| 481 |
m := map[string]interface{}{} |
| 482 |
err := Unmarshal([]byte(inputJSON), &m, PreserveInts) |
| 483 |
if tc.Err && err != nil { |
| 484 |
|
| 485 |
return |
| 486 |
} |
| 487 |
if err != nil { |
| 488 |
t.Fatalf("%s: error decoding: %v", tc.In, err) |
| 489 |
} |
| 490 |
if tc.Err { |
| 491 |
t.Fatalf("%s: expected error, got none", tc.In) |
| 492 |
} |
| 493 |
data, ok := m["data"] |
| 494 |
if !ok { |
| 495 |
t.Fatalf("%s: decoded object missing data key: %#v", tc.In, m) |
| 496 |
} |
| 497 |
if !reflect.DeepEqual(tc.Data, data) { |
| 498 |
t.Fatalf("%s: expected\n\t%#v (%v), got\n\t%#v (%v)", tc.In, tc.Data, reflect.TypeOf(tc.Data), data, reflect.TypeOf(data)) |
| 499 |
} |
| 500 |
|
| 501 |
outputJSON, err := Marshal(m) |
| 502 |
if err != nil { |
| 503 |
t.Fatalf("%s: error encoding: %v", tc.In, err) |
| 504 |
} |
| 505 |
|
| 506 |
if expectedJSON != string(outputJSON) { |
| 507 |
t.Fatalf("%s: expected\n\t%s, got\n\t%s", tc.In, expectedJSON, string(outputJSON)) |
| 508 |
} |
| 509 |
}) |
| 510 |
|
| 511 |
t.Run(fmt.Sprintf("%d_slice", i), func(t *testing.T) { |
| 512 |
|
| 513 |
inputJSON := fmt.Sprintf(`[0,%s]`, tc.In) |
| 514 |
expectedJSON := fmt.Sprintf(`[0,%s]`, tc.Out) |
| 515 |
m := []interface{}{} |
| 516 |
err := Unmarshal([]byte(inputJSON), &m, PreserveInts) |
| 517 |
if tc.Err && err != nil { |
| 518 |
|
| 519 |
return |
| 520 |
} |
| 521 |
if err != nil { |
| 522 |
t.Fatalf("%s: error decoding: %v", tc.In, err) |
| 523 |
} |
| 524 |
if tc.Err { |
| 525 |
t.Fatalf("%s: expected error, got none", tc.In) |
| 526 |
} |
| 527 |
if len(m) != 2 { |
| 528 |
t.Fatalf("%s: decoded object wasn't the right length: %#v", tc.In, m) |
| 529 |
} |
| 530 |
data := m[1] |
| 531 |
if !reflect.DeepEqual(tc.Data, data) { |
| 532 |
t.Fatalf("%s: expected\n\t%#v (%v), got\n\t%#v (%v)", tc.In, tc.Data, reflect.TypeOf(tc.Data), data, reflect.TypeOf(data)) |
| 533 |
} |
| 534 |
|
| 535 |
outputJSON, err := Marshal(m) |
| 536 |
if err != nil { |
| 537 |
t.Fatalf("%s: error encoding: %v", tc.In, err) |
| 538 |
} |
| 539 |
|
| 540 |
if expectedJSON != string(outputJSON) { |
| 541 |
t.Fatalf("%s: expected\n\t%s, got\n\t%s", tc.In, expectedJSON, string(outputJSON)) |
| 542 |
} |
| 543 |
}) |
| 544 |
|
| 545 |
t.Run(fmt.Sprintf("%d_raw", i), func(t *testing.T) { |
| 546 |
|
| 547 |
inputJSON := fmt.Sprintf(`%s`, tc.In) |
| 548 |
expectedJSON := fmt.Sprintf(`%s`, tc.Out) |
| 549 |
var m interface{} |
| 550 |
err := Unmarshal([]byte(inputJSON), &m, PreserveInts) |
| 551 |
if tc.Err && err != nil { |
| 552 |
|
| 553 |
return |
| 554 |
} |
| 555 |
if err != nil { |
| 556 |
t.Fatalf("%s: error decoding: %v", tc.In, err) |
| 557 |
} |
| 558 |
if tc.Err { |
| 559 |
t.Fatalf("%s: expected error, got none", tc.In) |
| 560 |
} |
| 561 |
data := m |
| 562 |
if !reflect.DeepEqual(tc.Data, data) { |
| 563 |
t.Fatalf("%s: expected\n\t%#v (%v), got\n\t%#v (%v)", tc.In, tc.Data, reflect.TypeOf(tc.Data), data, reflect.TypeOf(data)) |
| 564 |
} |
| 565 |
|
| 566 |
outputJSON, err := Marshal(m) |
| 567 |
if err != nil { |
| 568 |
t.Fatalf("%s: error encoding: %v", tc.In, err) |
| 569 |
} |
| 570 |
|
| 571 |
if expectedJSON != string(outputJSON) { |
| 572 |
t.Fatalf("%s: expected\n\t%s, got\n\t%s", tc.In, expectedJSON, string(outputJSON)) |
| 573 |
} |
| 574 |
}) |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
v := map[string]interface{}{} |
| 579 |
if err := Unmarshal([]byte(`{"a":1}`), &v, PreserveInts, UseNumber); err != nil { |
| 580 |
t.Fatal(err) |
| 581 |
} |
| 582 |
if e, a := map[string]interface{}{"a": gojson.Number("1")}, v; !reflect.DeepEqual(e, a) { |
| 583 |
t.Fatalf("expected\n\t%#v, got\n\t%#v", e, a) |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
func TestDisallowDuplicateFields(t *testing.T) { |
| 588 |
type SubType struct { |
| 589 |
F int `json:"f"` |
| 590 |
G int `json:"g"` |
| 591 |
} |
| 592 |
type MixedObj struct { |
| 593 |
A int `json:"a"` |
| 594 |
B int `json:"b"` |
| 595 |
C int |
| 596 |
D map[string]string `json:"d"` |
| 597 |
E []SubType `json:"e"` |
| 598 |
} |
| 599 |
type SmallObj struct { |
| 600 |
F01 int |
| 601 |
F02 int |
| 602 |
F03 int |
| 603 |
F04 int |
| 604 |
F05 int |
| 605 |
F06 int |
| 606 |
F07 int |
| 607 |
F08 int |
| 608 |
F09 int |
| 609 |
F10 int |
| 610 |
F11 int |
| 611 |
F12 int |
| 612 |
F13 int |
| 613 |
F14 int |
| 614 |
F15 int |
| 615 |
F16 int |
| 616 |
F17 int |
| 617 |
F18 int |
| 618 |
F19 int |
| 619 |
F20 int |
| 620 |
F21 int |
| 621 |
F22 int |
| 622 |
F23 int |
| 623 |
F24 int |
| 624 |
F25 int |
| 625 |
F26 int |
| 626 |
F27 int |
| 627 |
F28 int |
| 628 |
F29 int |
| 629 |
F30 int |
| 630 |
F31 int |
| 631 |
F32 int |
| 632 |
F33 int |
| 633 |
F34 int |
| 634 |
F35 int |
| 635 |
F36 int |
| 636 |
F37 int |
| 637 |
F38 int |
| 638 |
F39 int |
| 639 |
F40 int |
| 640 |
F41 int |
| 641 |
F42 int |
| 642 |
F43 int |
| 643 |
F44 int |
| 644 |
F45 int |
| 645 |
F46 int |
| 646 |
F47 int |
| 647 |
F48 int |
| 648 |
F49 int |
| 649 |
F50 int |
| 650 |
F51 int |
| 651 |
F52 int |
| 652 |
F53 int |
| 653 |
F54 int |
| 654 |
F55 int |
| 655 |
F56 int |
| 656 |
F57 int |
| 657 |
F58 int |
| 658 |
F59 int |
| 659 |
F60 int |
| 660 |
F61 int |
| 661 |
F62 int |
| 662 |
F63 int |
| 663 |
F64 int |
| 664 |
} |
| 665 |
|
| 666 |
type BigObj struct { |
| 667 |
F01 int |
| 668 |
F02 int |
| 669 |
F03 int |
| 670 |
F04 int |
| 671 |
F05 int |
| 672 |
F06 int |
| 673 |
F07 int |
| 674 |
F08 int |
| 675 |
F09 int |
| 676 |
F10 int |
| 677 |
F11 int |
| 678 |
F12 int |
| 679 |
F13 int |
| 680 |
F14 int |
| 681 |
F15 int |
| 682 |
F16 int |
| 683 |
F17 int |
| 684 |
F18 int |
| 685 |
F19 int |
| 686 |
F20 int |
| 687 |
F21 int |
| 688 |
F22 int |
| 689 |
F23 int |
| 690 |
F24 int |
| 691 |
F25 int |
| 692 |
F26 int |
| 693 |
F27 int |
| 694 |
F28 int |
| 695 |
F29 int |
| 696 |
F30 int |
| 697 |
F31 int |
| 698 |
F32 int |
| 699 |
F33 int |
| 700 |
F34 int |
| 701 |
F35 int |
| 702 |
F36 int |
| 703 |
F37 int |
| 704 |
F38 int |
| 705 |
F39 int |
| 706 |
F40 int |
| 707 |
F41 int |
| 708 |
F42 int |
| 709 |
F43 int |
| 710 |
F44 int |
| 711 |
F45 int |
| 712 |
F46 int |
| 713 |
F47 int |
| 714 |
F48 int |
| 715 |
F49 int |
| 716 |
F50 int |
| 717 |
F51 int |
| 718 |
F52 int |
| 719 |
F53 int |
| 720 |
F54 int |
| 721 |
F55 int |
| 722 |
F56 int |
| 723 |
F57 int |
| 724 |
F58 int |
| 725 |
F59 int |
| 726 |
F60 int |
| 727 |
F61 int |
| 728 |
F62 int |
| 729 |
F63 int |
| 730 |
F64 int |
| 731 |
F65 int |
| 732 |
} |
| 733 |
|
| 734 |
testcases := []struct { |
| 735 |
name string |
| 736 |
in string |
| 737 |
to interface{} |
| 738 |
expectErr string |
| 739 |
}{ |
| 740 |
{ |
| 741 |
name: "duplicate typed", |
| 742 |
in: `{"a":1,"a":2,"a":3,"b":4,"b":5,"b":6,"C":7,"C":8,"C":9,"e":[{"f":1,"f":1}]}`, |
| 743 |
to: &MixedObj{}, |
| 744 |
expectErr: `json: duplicate field "a", duplicate field "b", duplicate field "C", duplicate field "e[0].f"`, |
| 745 |
}, |
| 746 |
{ |
| 747 |
name: "duplicate typed map field", |
| 748 |
in: `{"d":{"a":"","b":"","c":"","a":"","b":"","c":""}}`, |
| 749 |
to: &MixedObj{}, |
| 750 |
expectErr: `json: duplicate field "d.a", duplicate field "d.b", duplicate field "d.c"`, |
| 751 |
}, |
| 752 |
{ |
| 753 |
name: "duplicate untyped map", |
| 754 |
in: `{"a":"","b":"","a":"","b":"","c":{"c":"","c":""},"d":{"e":{"f":1,"f":1}},"e":[{"g":1,"g":1}]}`, |
| 755 |
to: &map[string]interface{}{}, |
| 756 |
expectErr: `json: duplicate field "a", duplicate field "b", duplicate field "c.c", duplicate field "d.e.f", duplicate field "e[0].g"`, |
| 757 |
}, |
| 758 |
{ |
| 759 |
name: "small obj", |
| 760 |
in: `{"f01":1,"f01":2,"f32":1,"f32":2,"f64":1,"f64":2}`, |
| 761 |
to: &SmallObj{}, |
| 762 |
expectErr: `json: duplicate field "F01", duplicate field "F32", duplicate field "F64"`, |
| 763 |
}, |
| 764 |
{ |
| 765 |
name: "big obj", |
| 766 |
in: `{"f01":1,"f01":2,"f32":1,"f32":2,"f64":1,"f64":2,"f65":1,"f65":2}`, |
| 767 |
to: &BigObj{}, |
| 768 |
expectErr: `json: duplicate field "F01", duplicate field "F32", duplicate field "F64", duplicate field "F65"`, |
| 769 |
}, |
| 770 |
} |
| 771 |
|
| 772 |
for _, tc := range testcases { |
| 773 |
t.Run(tc.name, func(t *testing.T) { |
| 774 |
err := Unmarshal([]byte(tc.in), &tc.to, DisallowDuplicateFields) |
| 775 |
if (len(tc.expectErr) > 0) != (err != nil) { |
| 776 |
t.Fatalf("expected err=%v, got %v", len(tc.expectErr) > 0, err) |
| 777 |
} |
| 778 |
if err == nil { |
| 779 |
return |
| 780 |
} |
| 781 |
if err.Error() != tc.expectErr { |
| 782 |
t.Fatalf("expected err\n%s\ngot\n%s", tc.expectErr, err.Error()) |
| 783 |
} |
| 784 |
}) |
| 785 |
} |
| 786 |
} |