Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(561)

Side by Side Diff: sdk/lib/convert/json.dart

Issue 1153893004: In JsonEncoder, test if map has only string keys, and use toEncodable if not. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/lib/convert/json_toEncodable_reviver_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.convert; 5 part of dart.convert;
6 6
7 /** 7 /**
8 * Error thrown by JSON serialization if an object cannot be serialized. 8 * Error thrown by JSON serialization if an object cannot be serialized.
9 * 9 *
10 * The [unsupportedObject] field holds that object that failed to be serialized. 10 * The [unsupportedObject] field holds that object that failed to be serialized.
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 writeStringContent(object); 686 writeStringContent(object);
687 writeString('"'); 687 writeString('"');
688 return true; 688 return true;
689 } else if (object is List) { 689 } else if (object is List) {
690 _checkCycle(object); 690 _checkCycle(object);
691 writeList(object); 691 writeList(object);
692 _removeSeen(object); 692 _removeSeen(object);
693 return true; 693 return true;
694 } else if (object is Map) { 694 } else if (object is Map) {
695 _checkCycle(object); 695 _checkCycle(object);
696 writeMap(object); 696 // writeMap can fail if keys are not all strings.
697 var result = writeMap(object);
Søren Gjesse 2015/05/26 14:54:00 result -> success
697 _removeSeen(object); 698 _removeSeen(object);
698 return true; 699 return result;
699 } else { 700 } else {
700 return false; 701 return false;
701 } 702 }
702 } 703 }
703 704
704 /** Serializes a [List]. */ 705 /** Serializes a [List]. */
705 void writeList(List list) { 706 void writeList(List list) {
706 writeString('['); 707 writeString('[');
707 if (list.length > 0) { 708 if (list.length > 0) {
708 writeObject(list[0]); 709 writeObject(list[0]);
709 for (int i = 1; i < list.length; i++) { 710 for (int i = 1; i < list.length; i++) {
710 writeString(','); 711 writeString(',');
711 writeObject(list[i]); 712 writeObject(list[i]);
712 } 713 }
713 } 714 }
714 writeString(']'); 715 writeString(']');
715 } 716 }
716 717
717 /** Serializes a [Map]. */ 718 /** Serializes a [Map]. */
718 void writeMap(Map<String, Object> map) { 719 bool writeMap(Map<String, Object> map) {
720 List keyValueList = new List(map.length * 2);
721 int i = 0;
722 bool allStringKeys = true;
723 map.forEach((key, value) {
724 if (key is! String) {
725 allStringKeys = false;
726 }
727 keyValueList[i++] = key;
728 keyValueList[i++] = value;
729 });
730 if (!allStringKeys) return false;
719 writeString('{'); 731 writeString('{');
720 String separator = '"'; 732 String separator = '"';
721 map.forEach((String key, value) { 733 for (int i = 0; i < keyValueList.length; i += 2) {
722 writeString(separator); 734 writeString(separator);
723 separator = ',"'; 735 separator = ',"';
724 writeStringContent(key); 736 writeStringContent(keyValueList[i]);
725 writeString('":'); 737 writeString('":');
726 writeObject(value); 738 writeObject(keyValueList[i + 1]);
727 }); 739 }
728 writeString('}'); 740 writeString('}');
741 return true;
729 } 742 }
730 } 743 }
731 744
732 /** 745 /**
733 * A modification of [_JsonStringifier] which indents the contents of [List] and 746 * A modification of [_JsonStringifier] which indents the contents of [List] and
734 * [Map] objects using the specified indent value. 747 * [Map] objects using the specified indent value.
735 * 748 *
736 * Subclasses should implement [writeIndentation]. 749 * Subclasses should implement [writeIndentation].
737 */ 750 */
738 abstract class _JsonPrettyPrintMixin implements _JsonStringifier { 751 abstract class _JsonPrettyPrintMixin implements _JsonStringifier {
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 buffer.setRange(index, end, indent); 1042 buffer.setRange(index, end, indent);
1030 index = end; 1043 index = end;
1031 } else { 1044 } else {
1032 for (int i = 0; i < indentLength; i++) { 1045 for (int i = 0; i < indentLength; i++) {
1033 writeByte(indent[i]); 1046 writeByte(indent[i]);
1034 } 1047 }
1035 } 1048 }
1036 } 1049 }
1037 } 1050 }
1038 } 1051 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/convert/json_toEncodable_reviver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698