LEFT | RIGHT |
(no file at all) | |
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 // Represents JSON data structure using native Go types: booleans, floats, | 5 // Represents JSON data structure using native Go types: booleans, floats, |
6 // strings, arrays, and maps. | 6 // strings, arrays, and maps. |
7 | 7 |
8 package json | 8 package json |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 423 |
424 // Decoding into nil interface? Switch to non-reflect code. | 424 // Decoding into nil interface? Switch to non-reflect code. |
425 if v.Kind() == reflect.Interface { | 425 if v.Kind() == reflect.Interface { |
426 v.Set(reflect.ValueOf(d.objectInterface())) | 426 v.Set(reflect.ValueOf(d.objectInterface())) |
427 return | 427 return |
428 } | 428 } |
429 | 429 |
430 // Check type of target: struct or map[string]T | 430 // Check type of target: struct or map[string]T |
431 switch v.Kind() { | 431 switch v.Kind() { |
432 case reflect.Map: | 432 case reflect.Map: |
433 » » // map must have string type | 433 » » // map must have string kind |
434 t := v.Type() | 434 t := v.Type() |
435 » » if t.Key() != reflect.TypeOf("") { | 435 » » if t.Key().Kind() != reflect.String { |
436 d.saveError(&UnmarshalTypeError{"object", v.Type()}) | 436 d.saveError(&UnmarshalTypeError{"object", v.Type()}) |
437 break | 437 break |
438 } | 438 } |
439 if v.IsNil() { | 439 if v.IsNil() { |
440 v.Set(reflect.MakeMap(t)) | 440 v.Set(reflect.MakeMap(t)) |
441 } | 441 } |
442 case reflect.Struct: | 442 case reflect.Struct: |
443 default: | 443 default: |
444 d.saveError(&UnmarshalTypeError{"object", v.Type()}) | 444 d.saveError(&UnmarshalTypeError{"object", v.Type()}) |
445 } | 445 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 d.error(errPhase) | 529 d.error(errPhase) |
530 } | 530 } |
531 | 531 |
532 // Read value. | 532 // Read value. |
533 if destring { | 533 if destring { |
534 d.value(reflect.ValueOf(&d.tempstr)) | 534 d.value(reflect.ValueOf(&d.tempstr)) |
535 d.literalStore([]byte(d.tempstr), subv, true) | 535 d.literalStore([]byte(d.tempstr), subv, true) |
536 } else { | 536 } else { |
537 d.value(subv) | 537 d.value(subv) |
538 } | 538 } |
| 539 |
539 // Write value back to map; | 540 // Write value back to map; |
540 // if using struct, subv points into struct already. | 541 // if using struct, subv points into struct already. |
541 if v.Kind() == reflect.Map { | 542 if v.Kind() == reflect.Map { |
542 » » » v.SetMapIndex(reflect.ValueOf(key), subv) | 543 » » » kv := reflect.ValueOf(key).Convert(v.Type().Key()) |
| 544 » » » v.SetMapIndex(kv, subv) |
543 } | 545 } |
544 | 546 |
545 // Next token must be , or }. | 547 // Next token must be , or }. |
546 op = d.scanWhile(scanSkipSpace) | 548 op = d.scanWhile(scanSkipSpace) |
547 if op == scanEndObject { | 549 if op == scanEndObject { |
548 break | 550 break |
549 } | 551 } |
550 if op != scanObjectValue { | 552 if op != scanObjectValue { |
551 d.error(errPhase) | 553 d.error(errPhase) |
552 } | 554 } |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 | 973 |
972 // Coerce to well-formed UTF-8. | 974 // Coerce to well-formed UTF-8. |
973 default: | 975 default: |
974 rr, size := utf8.DecodeRune(s[r:]) | 976 rr, size := utf8.DecodeRune(s[r:]) |
975 r += size | 977 r += size |
976 w += utf8.EncodeRune(b[w:], rr) | 978 w += utf8.EncodeRune(b[w:], rr) |
977 } | 979 } |
978 } | 980 } |
979 return b[0:w], true | 981 return b[0:w], true |
980 } | 982 } |
LEFT | RIGHT |