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

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: Address comment. 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..d3624ab10e7491242d7542e607ef436684b20088 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 success = writeMap(object);
_removeSeen(object);
- return true;
+ return success;
} else {
return false;
}
@@ -715,17 +716,33 @@ abstract class _JsonStringifier {
}
/** Serializes a [Map]. */
- void writeMap(Map<String, Object> map) {
+ bool writeMap(Map<String, Object> map) {
+ if (map.isEmpty) {
+ writeString("{}");
+ return true;
+ }
+ 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;
}
}
@@ -763,29 +780,39 @@ abstract class _JsonPrettyPrintMixin implements _JsonStringifier {
}
}
- void writeMap(Map map) {
+ bool writeMap(Map map) {
if (map.isEmpty) {
- writeString('{}');
- } else {
- writeString('{\n');
- _indentLevel++;
- bool first = true;
- map.forEach((String key, Object value) {
- if (!first) {
- writeString(",\n");
- }
- writeIndentation(_indentLevel);
- writeString('"');
- writeStringContent(key);
- writeString('": ');
- writeObject(value);
- first = false;
- });
- writeString('\n');
- _indentLevel--;
+ writeString("{}");
+ return true;
+ }
+ 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('{\n');
+ _indentLevel++;
+ String separator = "";
+ for (int i = 0; i < keyValueList.length; i += 2) {
+ writeString(separator);
+ separator = ",\n";
writeIndentation(_indentLevel);
- writeString('}');
+ writeString('"');
+ writeStringContent(keyValueList[i]);
+ writeString('": ');
+ writeObject(keyValueList[i + 1]);
}
+ writeString('\n');
+ _indentLevel--;
+ writeIndentation(_indentLevel);
+ 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