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

Side by Side Diff: lib/src/json_document_transformer.dart

Issue 1658833002: Be more explicit about JSON error handling. (Closed) Base URL: git@github.com:dart-lang/stream_channel.git@master
Patch Set: Code review changes Created 4 years, 10 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 unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 emits a
22 /// [FormatException]. If an unencodable object is added to the sink, it
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 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698