Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 | 6 |
| 7 import 'package:async/async.dart'; | 7 import 'package:async/async.dart'; |
| 8 | 8 |
| 9 import '../stream_channel.dart'; | 9 import '../stream_channel.dart'; |
| 10 import 'stream_channel_transformer.dart'; | 10 import 'stream_channel_transformer.dart'; |
| 11 | 11 |
| 12 /// The canonical instance of [JsonDocumentTransformer]. | 12 /// The canonical instance of [JsonDocumentTransformer]. |
| 13 final jsonDocument = new JsonDocumentTransformer(); | 13 final jsonDocument = new JsonDocumentTransformer(); |
| 14 | 14 |
| 15 /// A [StreamChannelTransformer] that transforms JSON documents—strings that | 15 /// A [StreamChannelTransformer] that transforms JSON documents—strings that |
| 16 /// contain individual objects encoded as JSON—into decoded Dart objects. | 16 /// contain individual objects encoded as JSON—into decoded Dart objects. |
| 17 /// | 17 /// |
| 18 /// This decodes JSON that's emitted by the transformed channel's stream, and | 18 /// This decodes JSON that's emitted by the transformed channel's stream, and |
| 19 /// encodes objects so that JSON is passed to the transformed channel's sink. | 19 /// encodes objects so that JSON is passed to the transformed channel's sink. |
| 20 /// | |
| 21 /// If the transformed channel emits invalid JSON, this will emit a | |
| 22 /// [FormatException]. If an unencodable object is added to the sink, it will | |
|
Bob Nystrom
2016/02/01 22:48:28
Remove "will".
| |
| 23 /// synchronously throws a [JsonUnsupportedObjectError]. | |
| 20 class JsonDocumentTransformer | 24 class JsonDocumentTransformer |
| 21 implements StreamChannelTransformer<String, Object> { | 25 implements StreamChannelTransformer<String, Object> { |
| 22 /// The underlying codec that implements the encoding and decoding logic. | 26 /// The underlying codec that implements the encoding and decoding logic. |
| 23 final JsonCodec _codec; | 27 final JsonCodec _codec; |
| 24 | 28 |
| 25 /// Creates a new transformer. | 29 /// Creates a new transformer. |
| 26 /// | 30 /// |
| 27 /// The [reviver] and [toEncodable] arguments work the same way as the | 31 /// The [reviver] and [toEncodable] arguments work the same way as the |
| 28 /// corresponding arguments to [new JsonCodec]. | 32 /// corresponding arguments to [new JsonCodec]. |
| 29 JsonDocumentTransformer({reviver(key, value), toEncodable(object)}) | 33 JsonDocumentTransformer({reviver(key, value), toEncodable(object)}) |
| 30 : _codec = new JsonCodec(reviver: reviver, toEncodable: toEncodable); | 34 : _codec = new JsonCodec(reviver: reviver, toEncodable: toEncodable); |
| 31 | 35 |
| 32 JsonDocumentTransformer._(this._codec); | 36 JsonDocumentTransformer._(this._codec); |
| 33 | 37 |
| 34 StreamChannel bind(StreamChannel<String> channel) { | 38 StreamChannel bind(StreamChannel<String> channel) { |
| 35 var stream = channel.stream.map(_codec.decode); | 39 var stream = channel.stream.map(_codec.decode); |
| 36 var sink = new StreamSinkTransformer.fromHandlers(handleData: (data, sink) { | 40 var sink = new StreamSinkTransformer.fromHandlers(handleData: (data, sink) { |
| 37 sink.add(_codec.encode(data)); | 41 sink.add(_codec.encode(data)); |
| 38 }).bind(channel.sink); | 42 }).bind(channel.sink); |
| 39 return new StreamChannel(stream, sink); | 43 return new StreamChannel(stream, sink); |
| 40 } | 44 } |
| 41 } | 45 } |
| OLD | NEW |