OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package json | 5 package json |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "image" | 10 "image" |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 out, err := Marshal(n) | 414 out, err := Marshal(n) |
415 if err != nil { | 415 if err != nil { |
416 t.Fatal(err) | 416 t.Fatal(err) |
417 } | 417 } |
418 outStr := string(out) | 418 outStr := string(out) |
419 if outStr != "0" { | 419 if outStr != "0" { |
420 t.Fatalf("Invalid zero val for Number: %q", outStr) | 420 t.Fatalf("Invalid zero val for Number: %q", outStr) |
421 } | 421 } |
422 } | 422 } |
423 | 423 |
| 424 func TestMarshalEmbeds(t *testing.T) { |
| 425 top := &Top{ |
| 426 Level0: 1, |
| 427 Embed0: Embed0{ |
| 428 Level1b: 2, |
| 429 Level1c: 3, |
| 430 }, |
| 431 Embed0a: &Embed0a{ |
| 432 Level1a: 5, |
| 433 Level1b: 6, |
| 434 }, |
| 435 Embed0b: &Embed0b{ |
| 436 Level1a: 8, |
| 437 Level1b: 9, |
| 438 Level1c: 10, |
| 439 Level1d: 11, |
| 440 Level1e: 12, |
| 441 }, |
| 442 Loop: Loop{ |
| 443 Loop1: 13, |
| 444 Loop2: 14, |
| 445 }, |
| 446 Embed0p: Embed0p{ |
| 447 Point: image.Point{X: 15, Y: 16}, |
| 448 }, |
| 449 Embed0q: Embed0q{ |
| 450 Point: Point{Z: 17}, |
| 451 }, |
| 452 } |
| 453 b, err := Marshal(top) |
| 454 if err != nil { |
| 455 t.Fatal(err) |
| 456 } |
| 457 want := "{\"Level0\":1,\"Level1b\":2,\"Level1c\":3,\"Level1a\":5,\"LEVEL
1B\":6,\"e\":{\"Level1a\":8,\"Level1b\":9,\"Level1c\":10,\"Level1d\":11,\"x\":12
},\"Loop1\":13,\"Loop2\":14,\"X\":15,\"Y\":16,\"Z\":17}" |
| 458 if string(b) != want { |
| 459 t.Errorf("Wrong marshal result.\n got: %q\nwant: %q", b, want) |
| 460 } |
| 461 } |
| 462 |
424 func TestUnmarshal(t *testing.T) { | 463 func TestUnmarshal(t *testing.T) { |
425 for i, tt := range unmarshalTests { | 464 for i, tt := range unmarshalTests { |
426 var scan scanner | 465 var scan scanner |
427 in := []byte(tt.in) | 466 in := []byte(tt.in) |
428 if err := checkValid(in, &scan); err != nil { | 467 if err := checkValid(in, &scan); err != nil { |
429 if !reflect.DeepEqual(err, tt.err) { | 468 if !reflect.DeepEqual(err, tt.err) { |
430 t.Errorf("#%d: checkValid: %#v", i, err) | 469 t.Errorf("#%d: checkValid: %#v", i, err) |
431 continue | 470 continue |
432 } | 471 } |
433 } | 472 } |
434 if tt.ptr == nil { | 473 if tt.ptr == nil { |
435 continue | 474 continue |
436 } | 475 } |
437 // v = new(right-type) | 476 // v = new(right-type) |
438 v := reflect.New(reflect.TypeOf(tt.ptr).Elem()) | 477 v := reflect.New(reflect.TypeOf(tt.ptr).Elem()) |
439 » » dec := NewDecoder(bytes.NewBuffer(in)) | 478 » » dec := NewDecoder(bytes.NewReader(in)) |
440 if tt.useNumber { | 479 if tt.useNumber { |
441 dec.UseNumber() | 480 dec.UseNumber() |
442 } | 481 } |
443 if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.
err) { | 482 if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.
err) { |
444 t.Errorf("#%d: %v want %v", i, err, tt.err) | 483 t.Errorf("#%d: %v want %v", i, err, tt.err) |
445 continue | 484 continue |
446 } | 485 } |
447 if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { | 486 if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { |
448 t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.E
lem().Interface(), tt.out) | 487 t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.E
lem().Interface(), tt.out) |
449 data, _ := Marshal(v.Elem().Interface()) | 488 data, _ := Marshal(v.Elem().Interface()) |
450 println(string(data)) | 489 println(string(data)) |
451 data, _ = Marshal(tt.out) | 490 data, _ = Marshal(tt.out) |
452 println(string(data)) | 491 println(string(data)) |
453 continue | 492 continue |
454 } | 493 } |
455 | 494 |
456 // Check round trip. | 495 // Check round trip. |
457 if tt.err == nil { | 496 if tt.err == nil { |
458 enc, err := Marshal(v.Interface()) | 497 enc, err := Marshal(v.Interface()) |
459 if err != nil { | 498 if err != nil { |
460 t.Errorf("#%d: error re-marshaling: %v", i, err) | 499 t.Errorf("#%d: error re-marshaling: %v", i, err) |
461 continue | 500 continue |
462 } | 501 } |
463 vv := reflect.New(reflect.TypeOf(tt.ptr).Elem()) | 502 vv := reflect.New(reflect.TypeOf(tt.ptr).Elem()) |
464 » » » dec = NewDecoder(bytes.NewBuffer(enc)) | 503 » » » dec = NewDecoder(bytes.NewReader(enc)) |
465 if tt.useNumber { | 504 if tt.useNumber { |
466 dec.UseNumber() | 505 dec.UseNumber() |
467 } | 506 } |
468 if err := dec.Decode(vv.Interface()); err != nil { | 507 if err := dec.Decode(vv.Interface()); err != nil { |
469 t.Errorf("#%d: error re-unmarshaling: %v", i, er
r) | 508 t.Errorf("#%d: error re-unmarshaling: %v", i, er
r) |
470 continue | 509 continue |
471 } | 510 } |
472 if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().In
terface()) { | 511 if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().In
terface()) { |
473 t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v"
, i, v.Elem().Interface(), vv.Elem().Interface()) | 512 t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v"
, i, v.Elem().Interface(), vv.Elem().Interface()) |
| 513 t.Errorf(" In: %q", strings.Map(noSpace, str
ing(in))) |
| 514 t.Errorf("Marshal: %q", strings.Map(noSpace, str
ing(enc))) |
474 continue | 515 continue |
475 } | 516 } |
476 } | 517 } |
477 } | 518 } |
478 } | 519 } |
479 | 520 |
480 func TestUnmarshalMarshal(t *testing.T) { | 521 func TestUnmarshalMarshal(t *testing.T) { |
481 initBig() | 522 initBig() |
482 var v interface{} | 523 var v interface{} |
483 if err := Unmarshal(jsonBig, &v); err != nil { | 524 if err := Unmarshal(jsonBig, &v); err != nil { |
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1187 // Issue 3717 | 1228 // Issue 3717 |
1188 func TestSkipArrayObjects(t *testing.T) { | 1229 func TestSkipArrayObjects(t *testing.T) { |
1189 json := `[{}]` | 1230 json := `[{}]` |
1190 var dest [0]interface{} | 1231 var dest [0]interface{} |
1191 | 1232 |
1192 err := Unmarshal([]byte(json), &dest) | 1233 err := Unmarshal([]byte(json), &dest) |
1193 if err != nil { | 1234 if err != nil { |
1194 t.Errorf("got error %q, want nil", err) | 1235 t.Errorf("got error %q, want nil", err) |
1195 } | 1236 } |
1196 } | 1237 } |
OLD | NEW |