| 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 * An instance of the default implementation of the [JsonCodec]. | 8 * An instance of the default implementation of the [JsonCodec]. |
| 9 * | 9 * |
| 10 * This instance provides a convenient access to the most common JSON | 10 * This instance provides a convenient access to the most common JSON |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 * Returns a chunked-conversion sink that accepts at most one object. It is | 111 * Returns a chunked-conversion sink that accepts at most one object. It is |
| 112 * an error to invoke `add` more than once on the returned sink. | 112 * an error to invoke `add` more than once on the returned sink. |
| 113 */ | 113 */ |
| 114 ChunkedConversionSink<Object> startChunkedConversion( | 114 ChunkedConversionSink<Object> startChunkedConversion( |
| 115 ChunkedConversionSink<String> sink) { | 115 ChunkedConversionSink<String> sink) { |
| 116 if (sink is! StringConversionSink) { | 116 if (sink is! StringConversionSink) { |
| 117 sink = new StringConversionSink.from(sink); | 117 sink = new StringConversionSink.from(sink); |
| 118 } | 118 } |
| 119 return new _JsonEncoderSink(sink); | 119 return new _JsonEncoderSink(sink); |
| 120 } | 120 } |
| 121 |
| 122 // Override the base-classes bind, to provide a better type. |
| 123 Stream<String> bind(Stream<Object> stream) => super.bind(stream); |
| 121 } | 124 } |
| 122 | 125 |
| 123 /** | 126 /** |
| 124 * Implements the chunked conversion from object to its JSON representation. | 127 * Implements the chunked conversion from object to its JSON representation. |
| 125 * | 128 * |
| 126 * The sink only accepts one value, but will produce output in a chunked way. | 129 * The sink only accepts one value, but will produce output in a chunked way. |
| 127 */ | 130 */ |
| 128 class _JsonEncoderSink extends ChunkedConversionSink<Object> { | 131 class _JsonEncoderSink extends ChunkedConversionSink<Object> { |
| 129 final StringConversionSink _sink; | 132 final StringConversionSink _sink; |
| 130 bool _isDone = false; | 133 bool _isDone = false; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 /** | 186 /** |
| 184 * Starts a conversion from a chunked JSON string to its corresponding | 187 * Starts a conversion from a chunked JSON string to its corresponding |
| 185 * object. | 188 * object. |
| 186 * | 189 * |
| 187 * The output [sink] receives exactly one decoded element through `add`. | 190 * The output [sink] receives exactly one decoded element through `add`. |
| 188 */ | 191 */ |
| 189 StringConversionSink startChunkedConversion( | 192 StringConversionSink startChunkedConversion( |
| 190 ChunkedConversionSink<Object> sink) { | 193 ChunkedConversionSink<Object> sink) { |
| 191 return new _JsonDecoderSink(_reviver, sink); | 194 return new _JsonDecoderSink(_reviver, sink); |
| 192 } | 195 } |
| 196 |
| 197 // Override the base-classes bind, to provide a better type. |
| 198 Stream<Object> bind(Stream<String> stream) => super.bind(stream); |
| 193 } | 199 } |
| 194 | 200 |
| 195 /** | 201 /** |
| 196 * Implements the chunked conversion from a JSON string to its corresponding | 202 * Implements the chunked conversion from a JSON string to its corresponding |
| 197 * object. | 203 * object. |
| 198 * | 204 * |
| 199 * The sink only creates one object, but its input can be chunked. | 205 * The sink only creates one object, but its input can be chunked. |
| 200 */ | 206 */ |
| 201 // TODO(floitsch): don't accumulate everything before starting to decode. | 207 // TODO(floitsch): don't accumulate everything before starting to decode. |
| 202 class _JsonDecoderSink extends _StringSinkConversionSink { | 208 class _JsonDecoderSink extends _StringSinkConversionSink { |
| 203 final _Reviver _reviver; | 209 final _Reviver _reviver; |
| 204 final ChunkedConversionSink<Object> _chunkedSink; | 210 final ChunkedConversionSink<Object> _chunkedSink; |
| 205 | 211 |
| 206 _JsonDecoderSink(this._reviver, this._chunkedSink) | 212 _JsonDecoderSink(this._reviver, this._chunkedSink) |
| 207 : super(new StringBuffer()); | 213 : super(new StringBuffer()); |
| 208 | 214 |
| 209 void close() { | 215 void close() { |
| 210 super.close(); | 216 super.close(); |
| 211 StringBuffer buffer = _stringSink; | 217 StringBuffer buffer = _stringSink; |
| 212 String accumulated = buffer.toString(); | 218 String accumulated = buffer.toString(); |
| 213 buffer.clear(); | 219 buffer.clear(); |
| 214 Object decoded = OLD_JSON_LIB.parse(accumulated, _reviver); | 220 Object decoded = OLD_JSON_LIB.parse(accumulated, _reviver); |
| 215 _chunkedSink.add(decoded); | 221 _chunkedSink.add(decoded); |
| 216 _chunkedSink.close(); | 222 _chunkedSink.close(); |
| 217 } | 223 } |
| 218 } | 224 } |
| OLD | NEW |