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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/lib/convert/json_toEncodable_reviver_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/convert/json.dart
diff --git a/sdk/lib/convert/json.dart b/sdk/lib/convert/json.dart
index fa0886d7f3b535a654ac7b02fc43bee83c73137f..7d5f929fdda5c49a8986b761637d88cd852c11a9 100644
--- a/sdk/lib/convert/json.dart
+++ b/sdk/lib/convert/json.dart
@@ -693,9 +693,10 @@ abstract class _JsonStringifier {
return true;
} else if (object is Map) {
_checkCycle(object);
- writeMap(object);
+ // writeMap can fail if keys are not all strings.
+ var result = writeMap(object);
Søren Gjesse 2015/05/26 14:54:00 result -> success
_removeSeen(object);
- return true;
+ return result;
} else {
return false;
}
@@ -715,17 +716,29 @@ abstract class _JsonStringifier {
}
/** Serializes a [Map]. */
- void writeMap(Map<String, Object> map) {
+ bool writeMap(Map<String, Object> map) {
+ List keyValueList = new List(map.length * 2);
+ int i = 0;
+ bool allStringKeys = true;
+ map.forEach((key, value) {
+ if (key is! String) {
+ allStringKeys = false;
+ }
+ keyValueList[i++] = key;
+ keyValueList[i++] = value;
+ });
+ if (!allStringKeys) return false;
writeString('{');
String separator = '"';
- map.forEach((String key, value) {
+ for (int i = 0; i < keyValueList.length; i += 2) {
writeString(separator);
separator = ',"';
- writeStringContent(key);
+ writeStringContent(keyValueList[i]);
writeString('":');
- writeObject(value);
- });
+ writeObject(keyValueList[i + 1]);
+ }
writeString('}');
+ return true;
}
}
« 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