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

Unified Diff: lib/json/json.dart

Issue 9465023: Fix escaping in JSON.stringify and add unit test (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix a stray LINE_FEED reference Created 8 years, 10 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/utils/src/JsonTest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/json/json.dart
diff --git a/lib/json/json.dart b/lib/json/json.dart
index 57bfe3a6808877f53a84598aef08e1b81c60d4b5..00a24359920464547dee28fccea59dce3791021d 100644
--- a/lib/json/json.dart
+++ b/lib/json/json.dart
@@ -114,7 +114,7 @@ class JsonTokenizer {
static final int TAB = 9; // '\t'.charCodeAt(0)
static final int NEW_LINE = 10; // '\n'.charCodeAt(0)
static final int FORM_FEED = 12; // '\f'.charCodeAt(0)
- static final int LINE_FEED = 13; // '\r'.charCodeAt(0)
+ static final int CARRIAGE_RETURN = 13; // '\r'.charCodeAt(0)
static final int SPACE = 32; // ' '.charCodeAt(0)
static final int QUOTE = 34; // '"'.charCodeAt(0)
static final int PLUS = 43; // '+'.charCodeAt(0)
@@ -134,8 +134,11 @@ class JsonTokenizer {
static final int A_SMALL = 97; // 'a'.charCodeAt(0)
static final int B_SMALL = 98; // 'b'.charCodeAt(0)
static final int E_SMALL = 101; // 'e'.charCodeAt(0)
+ static final int F_SMALL = 102; // 'f'.charCodeAt(0)
static final int N_SMALL = 110; // 'n'.charCodeAt(0)
static final int R_SMALL = 114; // 'r'.charCodeAt(0)
+ static final int T_SMALL = 116; // 't'.charCodeAt(0)
+ static final int U_SMALL = 117; // 'u'.charCodeAt(0)
static final int Z_SMALL = 122; // 'z'.charCodeAt(0)
static final int LBRACE = 123; // '{'.charCodeAt(0)
static final int RBRACE = 125; // '}'.charCodeAt(0)
@@ -186,7 +189,7 @@ class JsonTokenizer {
c = NEW_LINE;
break;
case 'r':
- c = LINE_FEED;
+ c = CARRIAGE_RETURN;
break;
case 'f':
c = FORM_FEED;
@@ -335,7 +338,7 @@ class JsonTokenizer {
// TODO other kind of whitespace.
static bool isWhitespace(int c) {
- return c == SPACE || c == TAB || c == NEW_LINE || c == LINE_FEED;
+ return c == SPACE || c == TAB || c == NEW_LINE || c == CARRIAGE_RETURN;
}
static bool isDigit(int c) {
return (ZERO <= c) && (c <= NINE);
@@ -493,32 +496,50 @@ class JsonStringifier {
}
}
- // TODO: add others.
- static bool _needsEscape(int charCode) {
- return JsonTokenizer.QUOTE == charCode || JsonTokenizer.BACKSLASH == charCode
- || JsonTokenizer.NEW_LINE == charCode || JsonTokenizer.LINE_FEED == charCode;
- }
+ // ('0' + x) or ('a' + x - 10)
+ static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x;
static void _escape(StringBuffer sb, String s) {
- // TODO: support \u code points.
- // TODO: use writeCodePoint when implemented.
- // TODO: use for each if implemented.
final int length = s.length;
bool needsEscape = false;
final charCodes = new List<int>();
for (int i = 0; i < length; i++) {
int charCode = s.charCodeAt(i);
- if (_needsEscape(charCode)) {
- charCodes.add(JsonTokenizer.BACKSLASH);
+ if (charCode < 32) {
needsEscape = true;
-
- if (JsonTokenizer.NEW_LINE == charCode) {
- charCode = JsonTokenizer.N_SMALL;
- } else if (JsonTokenizer.LINE_FEED == charCode) {
- charCode = JsonTokenizer.R_SMALL;
+ charCodes.add(JsonTokenizer.BACKSLASH);
+ switch (charCode) {
+ case JsonTokenizer.BACKSPACE:
+ charCodes.add(JsonTokenizer.B_SMALL);
+ break;
+ case JsonTokenizer.TAB:
+ charCodes.add(JsonTokenizer.T_SMALL);
+ break;
+ case JsonTokenizer.NEW_LINE:
+ charCodes.add(JsonTokenizer.N_SMALL);
+ break;
+ case JsonTokenizer.FORM_FEED:
+ charCodes.add(JsonTokenizer.F_SMALL);
+ break;
+ case JsonTokenizer.CARRIAGE_RETURN:
+ charCodes.add(JsonTokenizer.R_SMALL);
+ break;
+ default:
+ charCodes.add(JsonTokenizer.U_SMALL);
+ charCodes.add(_hexDigit((charCode >> 12) & 0xf));
+ charCodes.add(_hexDigit((charCode >> 8) & 0xf));
+ charCodes.add(_hexDigit((charCode >> 4) & 0xf));
+ charCodes.add(_hexDigit(charCode & 0xf));
+ break;
}
+ } else if (charCode == JsonTokenizer.QUOTE ||
+ charCode == JsonTokenizer.BACKSLASH) {
+ needsEscape = true;
+ charCodes.add(JsonTokenizer.BACKSLASH);
+ charCodes.add(charCode);
+ } else {
+ charCodes.add(charCode);
}
- charCodes.add(charCode);
}
sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s);
}
« no previous file with comments | « no previous file | tests/utils/src/JsonTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698