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