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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 149 |
150 JsonDecoder get decoder { | 150 JsonDecoder get decoder { |
151 if (_reviver == null) return const JsonDecoder(); | 151 if (_reviver == null) return const JsonDecoder(); |
152 return new JsonDecoder(_reviver); | 152 return new JsonDecoder(_reviver); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 /** | 156 /** |
157 * This class converts JSON objects to strings. | 157 * This class converts JSON objects to strings. |
158 */ | 158 */ |
159 class JsonEncoder extends Converter<Object, String> | 159 class JsonEncoder extends Converter<Object, String> { |
160 implements ChunkedConverter<Object, String, Object, String> { | |
161 /** | 160 /** |
162 * The string used for indention. | 161 * The string used for indention. |
163 * | 162 * |
164 * When generating multi-line output, this string is inserted once at the | 163 * When generating multi-line output, this string is inserted once at the |
165 * beginning of each indented line for each level of indentation. | 164 * beginning of each indented line for each level of indentation. |
166 * | 165 * |
167 * If `null`, the output is encoded as a single line. | 166 * If `null`, the output is encoded as a single line. |
168 */ | 167 */ |
169 final String indent; | 168 final String indent; |
170 | 169 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 } | 275 } |
277 } | 276 } |
278 | 277 |
279 /** | 278 /** |
280 * Encoder that encodes a single object as a UTF-8 encoded JSON string. | 279 * Encoder that encodes a single object as a UTF-8 encoded JSON string. |
281 * | 280 * |
282 * This encoder works equivalently to first converting the object to | 281 * This encoder works equivalently to first converting the object to |
283 * a JSON string, and then UTF-8 encoding the string, but without | 282 * a JSON string, and then UTF-8 encoding the string, but without |
284 * creating an intermediate string. | 283 * creating an intermediate string. |
285 */ | 284 */ |
286 class JsonUtf8Encoder extends Converter<Object, List<int>> | 285 class JsonUtf8Encoder extends Converter<Object, List<int>> { |
287 implements ChunkedConverter<Object, List<int>, Object, List<int>> { | |
288 /** Default buffer size used by the JSON-to-UTF-8 encoder. */ | 286 /** Default buffer size used by the JSON-to-UTF-8 encoder. */ |
289 static const int DEFAULT_BUFFER_SIZE = 256; | 287 static const int DEFAULT_BUFFER_SIZE = 256; |
290 /** Indentation used in pretty-print mode, `null` if not pretty. */ | 288 /** Indentation used in pretty-print mode, `null` if not pretty. */ |
291 final List<int> _indent; | 289 final List<int> _indent; |
292 /** Function called with each un-encodable object encountered. */ | 290 /** Function called with each un-encodable object encountered. */ |
293 final _ToEncodable _toEncodable; | 291 final _ToEncodable _toEncodable; |
294 /** UTF-8 buffer size. */ | 292 /** UTF-8 buffer size. */ |
295 final int _bufferSize; | 293 final int _bufferSize; |
296 | 294 |
297 /** | 295 /** |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 if (!_isDone) { | 459 if (!_isDone) { |
462 _isDone = true; | 460 _isDone = true; |
463 _sink.close(); | 461 _sink.close(); |
464 } | 462 } |
465 } | 463 } |
466 } | 464 } |
467 | 465 |
468 /** | 466 /** |
469 * This class parses JSON strings and builds the corresponding objects. | 467 * This class parses JSON strings and builds the corresponding objects. |
470 */ | 468 */ |
471 class JsonDecoder extends Converter<String, Object> | 469 class JsonDecoder extends Converter<String, Object> { |
472 implements ChunkedConverter<String, Object, String, Object> { | |
473 final _Reviver _reviver; | 470 final _Reviver _reviver; |
474 /** | 471 /** |
475 * Constructs a new JsonDecoder. | 472 * Constructs a new JsonDecoder. |
476 * | 473 * |
477 * The [reviver] may be `null`. | 474 * The [reviver] may be `null`. |
478 */ | 475 */ |
479 const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver; | 476 const JsonDecoder([reviver(var key, var value)]) : this._reviver = reviver; |
480 | 477 |
481 /** | 478 /** |
482 * Converts the given JSON-string [input] to its corresponding object. | 479 * Converts the given JSON-string [input] to its corresponding object. |
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 buffer.setRange(index, end, indent); | 1049 buffer.setRange(index, end, indent); |
1053 index = end; | 1050 index = end; |
1054 } else { | 1051 } else { |
1055 for (int i = 0; i < indentLength; i++) { | 1052 for (int i = 0; i < indentLength; i++) { |
1056 writeByte(indent[i]); | 1053 writeByte(indent[i]); |
1057 } | 1054 } |
1058 } | 1055 } |
1059 } | 1056 } |
1060 } | 1057 } |
1061 } | 1058 } |
OLD | NEW |