Skip to content

Commit e80fc5d

Browse files
authored
Merge pull request #297 from davidmorgan/support-async-deserialize
Add BuiltListAsyncDeserializer.
2 parents b230f78 + 96817fa commit e80fc5d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
- Add support for polymorphism to `StandardJsonPlugin`. It will now specify
2323
type names as needed via a `discriminator` field, which by defualt is
2424
called `$`. This can be changed in the `StandardJsonPlugin` constructor.
25+
- Add `BuiltListAsyncDeserializer`. It provides a way to deserialize large
26+
responses without blocking, provided the top level serialized type is
27+
`BuiltList`.
2528

2629
## 4.4.1
2730

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2017, Google Inc. Please see the AUTHORS file for details.
2+
// All rights reserved. Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:built_value/serializer.dart';
8+
9+
/// Deserializer for `BuiltList` that runs asynchronously.
10+
///
11+
/// If you need to deserialize large payloads without blocking, arrange that
12+
/// the top level serialized object is a `BuiltList`. Then use this class to
13+
/// deserialize to a [Stream] of objects.
14+
class BuiltListAsyncDeserializer {
15+
Stream<Object> deserialize(Serializers serializers, Iterable serialized,
16+
{FullType specifiedType: FullType.unspecified}) async* {
17+
final elementType = specifiedType.parameters.isEmpty
18+
? FullType.unspecified
19+
: specifiedType.parameters[0];
20+
21+
for (final item in serialized) {
22+
yield serializers.deserialize(item, specifiedType: elementType);
23+
}
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2017, Google Inc. Please see the AUTHORS file for details.
2+
// All rights reserved. Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
import 'package:built_collection/built_collection.dart';
6+
import 'package:built_value/async_serializer.dart';
7+
import 'package:built_value/serializer.dart';
8+
import 'package:test/test.dart';
9+
10+
void main() {
11+
group('BuiltList', () {
12+
final data = new BuiltList<int>([1, 2, 3]);
13+
final specifiedType =
14+
const FullType(BuiltList, const [const FullType(int)]);
15+
final serializers = (new Serializers().toBuilder()
16+
..addBuilderFactory(specifiedType, () => new ListBuilder<int>()))
17+
.build();
18+
final serialized = [1, 2, 3];
19+
20+
test('can be deserialized asynchronously', () async {
21+
final deserialized = await new BuiltListAsyncDeserializer()
22+
.deserialize(serializers, serialized, specifiedType: specifiedType)
23+
.toList();
24+
25+
expect(deserialized, data);
26+
});
27+
});
28+
}

0 commit comments

Comments
 (0)