Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(732)

Unified Diff: sdk/lib/convert/json.dart

Issue 19883003: Add chunked conversion to converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Improve some typse. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/convert/json.dart
diff --git a/sdk/lib/convert/json.dart b/sdk/lib/convert/json.dart
index df036a194a26173bb6415596234f11b86d43e741..d2f2e52308f74c5b1bc9a23637fbcdf6b2011d7e 100644
--- a/sdk/lib/convert/json.dart
+++ b/sdk/lib/convert/json.dart
@@ -66,7 +66,7 @@ class _ReviverJsonCodec extends JsonCodec {
}
/**
- * A [JsonEncoder] converts JSON objects to strings.
+ * This class converts JSON objects to strings.
*/
class JsonEncoder extends Converter<Object, String> {
JsonEncoder();
@@ -99,13 +99,45 @@ class JsonEncoder extends Converter<Object, String> {
* serialized, the new values may or may not be reflected in the result.
*/
String convert(Object o) => OLD_JSON_LIB.stringify(o);
+
+ ChunkedConversionSink<Object, dynamic> startChunkedConversion(
+ ChunkedConversionSink sink) {
+ return new _JsonEncoderSink(sink.adaptTo(outputInterface));
+ }
+
+ ChunkedConversionInterface get outputInterface =>
+ StringConversionSink.INTERFACE;
}
-typedef _Reviver(var key, var value);
+/**
+ * Implements the chunked conversion from object to its JSON representation.
+ *
+ * The sink only accepts one value (and therefore only allows its input in
+ * [addNonChunked], but will produce output in a chunked way.
+ */
+class _JsonEncoderSink extends ChunkedConversionSink<Object, Object> {
+ StringConversionSink _sink;
+
+ _JsonEncoderSink(this._sink);
+ /**
+ * Encodes the given object [o].
+ *
+ * Although the input is non-chunked the output will receive the
+ * encoded string in chunks.
+ */
+ void addNonChunked(Object o) {
+ ClosableStringSink stringSink = _sink.asStringSink();
+ OLD_JSON_LIB.printOn(o, stringSink);
+ stringSink.close();
+ }
+}
+
+
+typedef _Reviver(var key, var value);
/**
- * A [JsonDecoder] parses JSON strings and builds the corresponding objects.
+ * This class parses JSON strings and builds the corresponding objects.
*/
class JsonDecoder extends Converter<String, Object> {
final _Reviver _reviver;
@@ -117,7 +149,7 @@ class JsonDecoder extends Converter<String, Object> {
JsonDecoder(reviver(var key, var value)) : this._reviver = reviver;
/**
- * Converts the given Json-string [input] to its corresponding object.
+ * Converts the given JSON-string [input] to its corresponding object.
*
* Parsed JSON values are of the types [num], [String], [bool], [Null],
* [List]s of parsed JSON values or [Map]s from [String] to parsed
@@ -132,4 +164,40 @@ class JsonDecoder extends Converter<String, Object> {
* Throws [FormatException] if the input is not valid JSON text.
*/
Object convert(String input) => OLD_JSON_LIB.parse(input, _reviver);
+
+ /**
+ * Starts a conversion from a chunked JSON string to its corresponding
+ * object.
+ *
+ * The output [sink] receives exactly one decoded element through
+ * `addNonChunked`.
+ */
+ StringConversionSink startChunkedConversion(ChunkedConversionSink sink) {
+ return new _JsonDecoderSink(_reviver, sink.adaptTo(outputInterface));
+ }
+
+ ChunkedConversionInterface get inputInterface =>
+ StringConversionSink.INTERFACE;
+}
+
+/**
+ * Implements the chunked conversion from a JSON string to its corresponding
+ * object.
+ *
+ * The sink only creates one object, but its input can be chunked.
+ */
+// TODO(floitsch): don't accumulate everything before starting to decode.
+class _JsonDecoderSink extends _StringSinkConversionSink {
+ final _Reviver _reviver;
+ final ChunkedConversionSink<Object, Object> _chunkedSink;
+
+ _JsonDecoderSink(this._reviver, this._chunkedSink)
+ : super(new StringBuffer());
+
+ void close() {
+ super.close();
+ StringBuffer buffer = _stringSink;
Søren Gjesse 2013/07/24 09:26:41 Any reason for this temporary? Maybe relying on t
floitsch 2013/07/24 18:31:15 The temporary was to get the StringBuffer type. I
+ Object decoded = OLD_JSON_LIB.parse(buffer.toString(), _reviver);
+ _chunkedSink.addNonChunked(decoded);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698