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 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 * Throws [FormatException] if the input is not valid JSON text. | 319 * Throws [FormatException] if the input is not valid JSON text. |
320 */ | 320 */ |
321 dynamic convert(String input) => _parseJson(input, _reviver); | 321 dynamic convert(String input) => _parseJson(input, _reviver); |
322 | 322 |
323 /** | 323 /** |
324 * Starts a conversion from a chunked JSON string to its corresponding | 324 * Starts a conversion from a chunked JSON string to its corresponding |
325 * object. | 325 * object. |
326 * | 326 * |
327 * The output [sink] receives exactly one decoded element through `add`. | 327 * The output [sink] receives exactly one decoded element through `add`. |
328 */ | 328 */ |
329 StringConversionSink startChunkedConversion(Sink<Object> sink) { | 329 external StringConversionSink startChunkedConversion(Sink<Object> sink); |
330 return new _JsonDecoderSink(_reviver, sink); | |
331 } | |
332 | 330 |
333 // Override the base-classes bind, to provide a better type. | 331 // Override the base-classes bind, to provide a better type. |
334 Stream<Object> bind(Stream<String> stream) => super.bind(stream); | 332 Stream<Object> bind(Stream<String> stream) => super.bind(stream); |
335 } | 333 } |
336 | 334 |
337 /** | |
338 * Implements the chunked conversion from a JSON string to its corresponding | |
339 * object. | |
340 * | |
341 * The sink only creates one object, but its input can be chunked. | |
342 */ | |
343 // TODO(floitsch): don't accumulate everything before starting to decode. | |
344 class _JsonDecoderSink extends _StringSinkConversionSink { | |
345 final _Reviver _reviver; | |
346 final Sink<Object> _sink; | |
347 | |
348 _JsonDecoderSink(this._reviver, this._sink) | |
349 : super(new StringBuffer()); | |
350 | |
351 void close() { | |
352 super.close(); | |
353 StringBuffer buffer = _stringSink; | |
354 String accumulated = buffer.toString(); | |
355 buffer.clear(); | |
356 Object decoded = _parseJson(accumulated, _reviver); | |
357 _sink.add(decoded); | |
358 _sink.close(); | |
359 } | |
360 } | |
361 | |
362 // Internal optimized JSON parsing implementation. | 335 // Internal optimized JSON parsing implementation. |
363 external _parseJson(String source, reviver(key, value)); | 336 external _parseJson(String source, reviver(key, value)); |
364 | 337 |
365 | 338 |
366 // Implementation of encoder/stringifier. | 339 // Implementation of encoder/stringifier. |
367 | 340 |
368 Object _defaultToEncodable(object) => object.toJson(); | 341 Object _defaultToEncodable(object) => object.toJson(); |
369 | 342 |
370 class _JsonStringifier { | 343 class _JsonStringifier { |
371 // Character code constants. | 344 // Character code constants. |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 _sink.writeln(); | 598 _sink.writeln(); |
626 _indentLevel--; | 599 _indentLevel--; |
627 _write('}'); | 600 _write('}'); |
628 } | 601 } |
629 _seen.remove(object); | 602 _seen.remove(object); |
630 return true; | 603 return true; |
631 } | 604 } |
632 return super.stringifyJsonValue(object); | 605 return super.stringifyJsonValue(object); |
633 } | 606 } |
634 } | 607 } |
OLD | NEW |