| 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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 206 |
| 207 /** | 207 /** |
| 208 * Starts a chunked conversion. | 208 * Starts a chunked conversion. |
| 209 * | 209 * |
| 210 * The converter works more efficiently if the given [sink] is a | 210 * The converter works more efficiently if the given [sink] is a |
| 211 * [StringConversionSink]. | 211 * [StringConversionSink]. |
| 212 * | 212 * |
| 213 * Returns a chunked-conversion sink that accepts at most one object. It is | 213 * Returns a chunked-conversion sink that accepts at most one object. It is |
| 214 * an error to invoke `add` more than once on the returned sink. | 214 * an error to invoke `add` more than once on the returned sink. |
| 215 */ | 215 */ |
| 216 ChunkedConversionSink<Object> startChunkedConversion( | 216 ChunkedConversionSink<Object> startChunkedConversion(Sink<String> sink) { |
| 217 ChunkedConversionSink<String> sink) { | |
| 218 if (sink is! StringConversionSink) { | 217 if (sink is! StringConversionSink) { |
| 219 sink = new StringConversionSink.from(sink); | 218 sink = new StringConversionSink.from(sink); |
| 220 } | 219 } |
| 221 return new _JsonEncoderSink(sink, _toEncodableFunction); | 220 return new _JsonEncoderSink(sink, _toEncodableFunction); |
| 222 } | 221 } |
| 223 | 222 |
| 224 // Override the base-classes bind, to provide a better type. | 223 // Override the base-classes bind, to provide a better type. |
| 225 Stream<String> bind(Stream<Object> stream) => super.bind(stream); | 224 Stream<String> bind(Stream<Object> stream) => super.bind(stream); |
| 226 } | 225 } |
| 227 | 226 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 * Throws [FormatException] if the input is not valid JSON text. | 284 * Throws [FormatException] if the input is not valid JSON text. |
| 286 */ | 285 */ |
| 287 dynamic convert(String input) => _parseJson(input, _reviver); | 286 dynamic convert(String input) => _parseJson(input, _reviver); |
| 288 | 287 |
| 289 /** | 288 /** |
| 290 * Starts a conversion from a chunked JSON string to its corresponding | 289 * Starts a conversion from a chunked JSON string to its corresponding |
| 291 * object. | 290 * object. |
| 292 * | 291 * |
| 293 * The output [sink] receives exactly one decoded element through `add`. | 292 * The output [sink] receives exactly one decoded element through `add`. |
| 294 */ | 293 */ |
| 295 StringConversionSink startChunkedConversion( | 294 StringConversionSink startChunkedConversion(Sink<Object> sink) { |
| 296 ChunkedConversionSink<Object> sink) { | |
| 297 return new _JsonDecoderSink(_reviver, sink); | 295 return new _JsonDecoderSink(_reviver, sink); |
| 298 } | 296 } |
| 299 | 297 |
| 300 // Override the base-classes bind, to provide a better type. | 298 // Override the base-classes bind, to provide a better type. |
| 301 Stream<Object> bind(Stream<String> stream) => super.bind(stream); | 299 Stream<Object> bind(Stream<String> stream) => super.bind(stream); |
| 302 } | 300 } |
| 303 | 301 |
| 304 /** | 302 /** |
| 305 * Implements the chunked conversion from a JSON string to its corresponding | 303 * Implements the chunked conversion from a JSON string to its corresponding |
| 306 * object. | 304 * object. |
| 307 * | 305 * |
| 308 * The sink only creates one object, but its input can be chunked. | 306 * The sink only creates one object, but its input can be chunked. |
| 309 */ | 307 */ |
| 310 // TODO(floitsch): don't accumulate everything before starting to decode. | 308 // TODO(floitsch): don't accumulate everything before starting to decode. |
| 311 class _JsonDecoderSink extends _StringSinkConversionSink { | 309 class _JsonDecoderSink extends _StringSinkConversionSink { |
| 312 final _Reviver _reviver; | 310 final _Reviver _reviver; |
| 313 final ChunkedConversionSink<Object> _chunkedSink; | 311 final Sink<Object> _sink; |
| 314 | 312 |
| 315 _JsonDecoderSink(this._reviver, this._chunkedSink) | 313 _JsonDecoderSink(this._reviver, this._sink) |
| 316 : super(new StringBuffer()); | 314 : super(new StringBuffer()); |
| 317 | 315 |
| 318 void close() { | 316 void close() { |
| 319 super.close(); | 317 super.close(); |
| 320 StringBuffer buffer = _stringSink; | 318 StringBuffer buffer = _stringSink; |
| 321 String accumulated = buffer.toString(); | 319 String accumulated = buffer.toString(); |
| 322 buffer.clear(); | 320 buffer.clear(); |
| 323 Object decoded = _parseJson(accumulated, _reviver); | 321 Object decoded = _parseJson(accumulated, _reviver); |
| 324 _chunkedSink.add(decoded); | 322 _sink.add(decoded); |
| 325 _chunkedSink.close(); | 323 _sink.close(); |
| 326 } | 324 } |
| 327 } | 325 } |
| 328 | 326 |
| 329 // Internal optimized JSON parsing implementation. | 327 // Internal optimized JSON parsing implementation. |
| 330 external _parseJson(String source, reviver(key, value)); | 328 external _parseJson(String source, reviver(key, value)); |
| 331 | 329 |
| 332 | 330 |
| 333 // Implementation of encoder/stringifier. | 331 // Implementation of encoder/stringifier. |
| 334 | 332 |
| 335 Object _defaultToEncodable(object) => object.toJson(); | 333 Object _defaultToEncodable(object) => object.toJson(); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 first = false; | 499 first = false; |
| 502 }); | 500 }); |
| 503 _sink.write('}'); | 501 _sink.write('}'); |
| 504 _seen.remove(object); | 502 _seen.remove(object); |
| 505 return true; | 503 return true; |
| 506 } else { | 504 } else { |
| 507 return false; | 505 return false; |
| 508 } | 506 } |
| 509 } | 507 } |
| 510 } | 508 } |
| OLD | NEW |