| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.convert; | 5 part of dart.convert; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Error thrown by JSON serialization if an object cannot be serialized. | 8 * Error thrown by JSON serialization if an object cannot be serialized. |
| 9 * | 9 * |
| 10 * The [unsupportedObject] field holds that object that failed to be serialized. | 10 * The [unsupportedObject] field holds that object that failed to be serialized. |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 154 |
| 155 JsonDecoder get decoder { | 155 JsonDecoder get decoder { |
| 156 if (_reviver == null) return const JsonDecoder(); | 156 if (_reviver == null) return const JsonDecoder(); |
| 157 return new JsonDecoder(_reviver); | 157 return new JsonDecoder(_reviver); |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 | 160 |
| 161 /** | 161 /** |
| 162 * This class converts JSON objects to strings. | 162 * This class converts JSON objects to strings. |
| 163 */ | 163 */ |
| 164 class JsonEncoder extends Converter<Object, String> { | 164 class JsonEncoder extends ChunkedConverter<Object, String, Object, String> { |
| 165 /** | 165 /** |
| 166 * The string used for indention. | 166 * The string used for indention. |
| 167 * | 167 * |
| 168 * When generating multi-line output, this string is inserted once at the | 168 * When generating multi-line output, this string is inserted once at the |
| 169 * beginning of each indented line for each level of indentation. | 169 * beginning of each indented line for each level of indentation. |
| 170 * | 170 * |
| 171 * If `null`, the output is encoded as a single line. | 171 * If `null`, the output is encoded as a single line. |
| 172 */ | 172 */ |
| 173 final String indent; | 173 final String indent; |
| 174 | 174 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 278 } |
| 279 } | 279 } |
| 280 | 280 |
| 281 /** | 281 /** |
| 282 * Encoder that encodes a single object as a UTF-8 encoded JSON string. | 282 * Encoder that encodes a single object as a UTF-8 encoded JSON string. |
| 283 * | 283 * |
| 284 * This encoder works equivalently to first converting the object to | 284 * This encoder works equivalently to first converting the object to |
| 285 * a JSON string, and then UTF-8 encoding the string, but without | 285 * a JSON string, and then UTF-8 encoding the string, but without |
| 286 * creating an intermediate string. | 286 * creating an intermediate string. |
| 287 */ | 287 */ |
| 288 class JsonUtf8Encoder extends Converter<Object, List<int>> { | 288 class JsonUtf8Encoder extends |
| 289 ChunkedConverter<Object, List<int>, Object, List<int>> { |
| 289 /** Default buffer size used by the JSON-to-UTF-8 encoder. */ | 290 /** Default buffer size used by the JSON-to-UTF-8 encoder. */ |
| 290 static const int DEFAULT_BUFFER_SIZE = 256; | 291 static const int DEFAULT_BUFFER_SIZE = 256; |
| 291 /** Indentation used in pretty-print mode, `null` if not pretty. */ | 292 /** Indentation used in pretty-print mode, `null` if not pretty. */ |
| 292 final List<int> _indent; | 293 final List<int> _indent; |
| 293 /** Function called with each un-encodable object encountered. */ | 294 /** Function called with each un-encodable object encountered. */ |
| 294 final Function _toEncodable; | 295 final Function _toEncodable; |
| 295 /** UTF-8 buffer size. */ | 296 /** UTF-8 buffer size. */ |
| 296 final int _bufferSize; | 297 final int _bufferSize; |
| 297 | 298 |
| 298 /** | 299 /** |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 if (!_isDone) { | 468 if (!_isDone) { |
| 468 _isDone = true; | 469 _isDone = true; |
| 469 _sink.close(); | 470 _sink.close(); |
| 470 } | 471 } |
| 471 } | 472 } |
| 472 } | 473 } |
| 473 | 474 |
| 474 /** | 475 /** |
| 475 * This class parses JSON strings and builds the corresponding objects. | 476 * This class parses JSON strings and builds the corresponding objects. |
| 476 */ | 477 */ |
| 477 class JsonDecoder extends Converter<String, Object> { | 478 class JsonDecoder extends ChunkedConverter<String, Object, String, Object> { |
| 478 final _Reviver _reviver; | 479 final _Reviver _reviver; |
| 479 /** | 480 /** |
| 480 * Constructs a new JsonDecoder. | 481 * Constructs a new JsonDecoder. |
| 481 * | 482 * |
| 482 * The [reviver] may be `null`. | 483 * The [reviver] may be `null`. |
| 483 */ | 484 */ |
| 484 const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver; | 485 const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver; |
| 485 | 486 |
| 486 /** | 487 /** |
| 487 * Converts the given JSON-string [input] to its corresponding object. | 488 * Converts the given JSON-string [input] to its corresponding object. |
| (...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1061 buffer.setRange(index, end, indent); | 1062 buffer.setRange(index, end, indent); |
| 1062 index = end; | 1063 index = end; |
| 1063 } else { | 1064 } else { |
| 1064 for (int i = 0; i < indentLength; i++) { | 1065 for (int i = 0; i < indentLength; i++) { |
| 1065 writeByte(indent[i]); | 1066 writeByte(indent[i]); |
| 1066 } | 1067 } |
| 1067 } | 1068 } |
| 1068 } | 1069 } |
| 1069 } | 1070 } |
| 1070 } | 1071 } |
| OLD | NEW |