OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * Simple library to serialize acyclic Dart types to JSON. | 6 * Simple library to serialize acyclic Dart types to JSON. |
7 * This library is not intended for broad consumption and should be replaced | 7 * This library is not intended for broad consumption and should be replaced |
8 * with a more generic Dart serialization library when one is available. | 8 * with a more generic Dart serialization library when one is available. |
9 */ | 9 */ |
10 library json_serializer; | 10 library json_serializer; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 // default. | 202 // default. |
203 return json.stringify(json.parse(_sb.toString())); | 203 return json.stringify(json.parse(_sb.toString())); |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x; | 207 static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x; |
208 | 208 |
209 static void _escape(StringBuffer sb, String s) { | 209 static void _escape(StringBuffer sb, String s) { |
210 final int length = s.length; | 210 final int length = s.length; |
211 bool needsEscape = false; | 211 bool needsEscape = false; |
212 final charCodes = new List<int>(); | 212 final codeUnits = new List<int>(); |
213 for (int i = 0; i < length; i++) { | 213 for (int i = 0; i < length; i++) { |
214 int charCode = s.charCodeAt(i); | 214 int codeUnit = s.codeUnitAt(i); |
215 if (charCode < 32) { | 215 if (codeUnit < 32) { |
216 needsEscape = true; | 216 needsEscape = true; |
217 charCodes.add(JsonPrinter.BACKSLASH); | 217 codeUnits.add(JsonPrinter.BACKSLASH); |
218 switch (charCode) { | 218 switch (codeUnit) { |
219 case JsonPrinter.BACKSPACE: | 219 case JsonPrinter.BACKSPACE: |
220 charCodes.add(JsonPrinter.CHAR_B); | 220 codeUnits.add(JsonPrinter.CHAR_B); |
221 break; | 221 break; |
222 case JsonPrinter.TAB: | 222 case JsonPrinter.TAB: |
223 charCodes.add(JsonPrinter.CHAR_T); | 223 codeUnits.add(JsonPrinter.CHAR_T); |
224 break; | 224 break; |
225 case JsonPrinter.NEW_LINE: | 225 case JsonPrinter.NEW_LINE: |
226 charCodes.add(JsonPrinter.CHAR_N); | 226 codeUnits.add(JsonPrinter.CHAR_N); |
227 break; | 227 break; |
228 case JsonPrinter.FORM_FEED: | 228 case JsonPrinter.FORM_FEED: |
229 charCodes.add(JsonPrinter.CHAR_F); | 229 codeUnits.add(JsonPrinter.CHAR_F); |
230 break; | 230 break; |
231 case JsonPrinter.CARRIAGE_RETURN: | 231 case JsonPrinter.CARRIAGE_RETURN: |
232 charCodes.add(JsonPrinter.CHAR_R); | 232 codeUnits.add(JsonPrinter.CHAR_R); |
233 break; | 233 break; |
234 default: | 234 default: |
235 charCodes.add(JsonPrinter.CHAR_U); | 235 codeUnits.add(JsonPrinter.CHAR_U); |
236 charCodes.add(_hexDigit((charCode >> 12) & 0xf)); | 236 codeUnits.add(_hexDigit((codeUnit >> 12) & 0xf)); |
237 charCodes.add(_hexDigit((charCode >> 8) & 0xf)); | 237 codeUnits.add(_hexDigit((codeUnit >> 8) & 0xf)); |
238 charCodes.add(_hexDigit((charCode >> 4) & 0xf)); | 238 codeUnits.add(_hexDigit((codeUnit >> 4) & 0xf)); |
239 charCodes.add(_hexDigit(charCode & 0xf)); | 239 codeUnits.add(_hexDigit(codeUnit & 0xf)); |
240 break; | 240 break; |
241 } | 241 } |
242 } else if (charCode == JsonPrinter.QUOTE || | 242 } else if (codeUnit == JsonPrinter.QUOTE || |
243 charCode == JsonPrinter.BACKSLASH) { | 243 codeUnit == JsonPrinter.BACKSLASH) { |
244 needsEscape = true; | 244 needsEscape = true; |
245 charCodes.add(JsonPrinter.BACKSLASH); | 245 codeUnits.add(JsonPrinter.BACKSLASH); |
246 charCodes.add(charCode); | 246 codeUnits.add(codeUnit); |
247 } else { | 247 } else { |
248 charCodes.add(charCode); | 248 codeUnits.add(codeUnit); |
249 } | 249 } |
250 } | 250 } |
251 sb.add(needsEscape ? new String.fromCharCodes(charCodes) : s); | 251 sb.add(needsEscape ? new String.fromCharCodes(codeUnits) : s); |
252 } | 252 } |
253 } | 253 } |
OLD | NEW |