| 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; |
| 11 | 11 |
| 12 import 'dart:async'; | 12 import 'dart:async'; |
| 13 import 'dart:convert'; |
| 13 import 'dart:mirrors'; | 14 import 'dart:mirrors'; |
| 14 import 'dart:json' as json; | |
| 15 | 15 |
| 16 String serialize(Object o) { | 16 String serialize(Object o) { |
| 17 var printer = new JsonPrinter(); | 17 var printer = new JsonPrinter(); |
| 18 _serialize(null, o, printer); | 18 _serialize(null, o, printer); |
| 19 return printer.toString(); | 19 return printer.toString(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 /// Serialize the object with pretty printing. | 22 /// Serialize the object with pretty printing. |
| 23 String prettySerialize(Object o) { | 23 String prettySerialize(Object o) { |
| 24 var printer = new JsonPrinter(prettyPrint: true); | 24 var printer = new JsonPrinter(prettyPrint: true); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 _sb.write(' '); | 200 _sb.write(' '); |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 | 203 |
| 204 String toString() { | 204 String toString() { |
| 205 if (prettyPrint) { | 205 if (prettyPrint) { |
| 206 return _sb.toString(); | 206 return _sb.toString(); |
| 207 } else { | 207 } else { |
| 208 // Convenient hack to remove the pretty printing this serializer adds by | 208 // Convenient hack to remove the pretty printing this serializer adds by |
| 209 // default. | 209 // default. |
| 210 return json.stringify(json.parse(_sb.toString())); | 210 return JSON.encode(JSON.decode(_sb.toString())); |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x; | 214 static int _hexDigit(int x) => x < 10 ? 48 + x : 87 + x; |
| 215 | 215 |
| 216 static void _escape(StringBuffer sb, String s) { | 216 static void _escape(StringBuffer sb, String s) { |
| 217 final int length = s.length; | 217 final int length = s.length; |
| 218 bool needsEscape = false; | 218 bool needsEscape = false; |
| 219 final codeUnits = new List<int>(); | 219 final codeUnits = new List<int>(); |
| 220 for (int i = 0; i < length; i++) { | 220 for (int i = 0; i < length; i++) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 251 needsEscape = true; | 251 needsEscape = true; |
| 252 codeUnits.add(JsonPrinter.BACKSLASH); | 252 codeUnits.add(JsonPrinter.BACKSLASH); |
| 253 codeUnits.add(codeUnit); | 253 codeUnits.add(codeUnit); |
| 254 } else { | 254 } else { |
| 255 codeUnits.add(codeUnit); | 255 codeUnits.add(codeUnit); |
| 256 } | 256 } |
| 257 } | 257 } |
| 258 sb.write(needsEscape ? new String.fromCharCodes(codeUnits) : s); | 258 sb.write(needsEscape ? new String.fromCharCodes(codeUnits) : s); |
| 259 } | 259 } |
| 260 } | 260 } |
| OLD | NEW |