| Index: sdk/lib/convert/json.dart
|
| diff --git a/sdk/lib/convert/json.dart b/sdk/lib/convert/json.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6b4d9da30539070d4abe44ac28b09ee493d11ba9
|
| --- /dev/null
|
| +++ b/sdk/lib/convert/json.dart
|
| @@ -0,0 +1,59 @@
|
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +part of dart.convert;
|
| +
|
| +class JsonEncoder extends Converter<Object, String> {
|
| + JsonEncoder();
|
| +
|
| + String convert(Object o) => JSON_LIB.stringify(o);
|
| +
|
| + ChunkedConversionSink<Object> startChunkedConversion(
|
| + ChunkedConversionSink sink) {
|
| + return new _JsonEncoderSink(outputInterface.adapt(sink));
|
| + }
|
| +
|
| + ChunkedConversionInterface get outputInterface =>
|
| + StringConversionSink.INTERFACE;
|
| +}
|
| +
|
| +class JsonDecoder extends Converter<String, Object> {
|
| + final _reviver;
|
| + JsonDecoder(this._reviver);
|
| +
|
| + Object convert(String input) => JSON_LIB.parse(input, _reviver);
|
| +
|
| + StringConversionSink startChunkedConversion(ChunkedConversionSink sink) {
|
| + return new _JsonDecoderSink(_reviver, outputInterface.adapt(sink));
|
| + }
|
| +
|
| + ChunkedConversionInterface get inputInterface =>
|
| + StringConversionSink.INTERFACE;
|
| +}
|
| +
|
| +class _JsonDecoderSink extends StringConversionSinkBase {
|
| + final _reviver;
|
| + ChunkedConversionSink _sink;
|
| + List<String> _carry = <String>[];
|
| +
|
| + _JsonDecoderSink(this._reviver, this._sink);
|
| +
|
| + void add(String str) => _carry.add(str);
|
| + void close() => _sink.addNonChunked(_convert(_carry.join()));
|
| +
|
| + Object _convert(String input) {
|
| + return JSON_LIB.parse(input, _reviver);
|
| + }
|
| +}
|
| +
|
| +class _JsonEncoderSink extends ChunkedConversionSink<Object> {
|
| + StringConversionSink _sink;
|
| +
|
| + _JsonEncoderSink(this._sink);
|
| +
|
| + void addNonChunked(Object o) {
|
| + JSON_LIB.printOn(o, _sink);
|
| + _sink.close();
|
| + }
|
| +}
|
|
|