Skip to content

Commit

Permalink
Merge pull request #428 from davidmorgan/serialize-sets
Browse files Browse the repository at this point in the history
Support serializing BuiltSet with StandardJsonPlugin.
  • Loading branch information
davidmorgan authored May 29, 2018
2 parents 93c2acb + 20606b0 commit 74e7e0d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 16 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Changelog

# 5.4.6
# 5.5.0

- Fix code generation when inherited generic fields are made non-generic.
- Support serializing `BuiltSet` with `StandardJsonPlugin`. It's serialized to
a JSON list.
- Fix code generation when inherited generic fields are made non-generic.

# 5.4.5

Expand Down
3 changes: 2 additions & 1 deletion built_value/lib/standard_json_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'dart:convert' show json;
/// [SerializersBuilder.addPlugin] to install this plugin.
class StandardJsonPlugin implements SerializerPlugin {
static final BuiltSet<Type> _unsupportedTypes =
new BuiltSet<Type>([BuiltSet, BuiltListMultimap, BuiltSetMultimap]);
new BuiltSet<Type>([BuiltListMultimap, BuiltSetMultimap]);

/// The field used to specify the value type if needed. Defaults to `$`.
final String discriminator;
Expand All @@ -35,6 +35,7 @@ class StandardJsonPlugin implements SerializerPlugin {
Object afterSerialize(Object object, FullType specifiedType) {
if (object is List &&
specifiedType.root != BuiltList &&
specifiedType.root != BuiltSet &&
specifiedType.root != JsonObject) {
if (specifiedType.isUnspecified) {
return _toMapWithDiscriminator(object);
Expand Down
29 changes: 21 additions & 8 deletions built_value/test/standard_json_plugin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ void main() {
const FullType(BuiltList, const [const FullType(int)])
]),
() => new ListBuilder<BuiltList<int>>())
..addBuilderFactory(
const FullType(BuiltSet, const [const FullType(int)]),
() => new SetBuilder<int>())
..addBuilderFactory(
const FullType(
BuiltMap, const [const FullType(int), const FullType(String)]),
Expand Down Expand Up @@ -50,14 +53,6 @@ void main() {
throwsA(new isInstanceOf<ArgumentError>()));
});

test('throws on serialize of sets', () {
final data = new BuiltSet<int>([1, 2, 3]);
final specifiedType =
const FullType(BuiltSet, const [const FullType(int)]);
expect(() => serializers.serialize(data, specifiedType: specifiedType),
throwsA(new isInstanceOf<ArgumentError>()));
});

test('throws on serialize of set multimaps', () {
final data = new BuiltSetMultimap<int, String>({
1: ['one'],
Expand Down Expand Up @@ -106,6 +101,24 @@ void main() {
});
});

group('can take a set and', () {
final data = new BuiltSet<int>([1, 2, 3]);
final specifiedType =
const FullType(BuiltSet, const [const FullType(int)]);
final serialized = [1, 2, 3];

test('serialize it', () {
expect(serializers.serialize(data, specifiedType: specifiedType),
serialized);
});

test('deserialize it', () {
expect(
serializers.deserialize(serialized, specifiedType: specifiedType),
data);
});
});

group('can take a nested list and', () {
final data = new BuiltList<BuiltList<int>>([
new BuiltList<int>([1, 2, 3]),
Expand Down
3 changes: 3 additions & 0 deletions end_to_end_test/lib/serializers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions end_to_end_test/lib/standard_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class StandardJsonValue
String get text;
BuiltMap<String, JsonObject> get keyValues;
BuiltList<Animal> get zoo;
BuiltSet<Animal> get uniqueZoo;

@nullable
BuiltList<String> get strings;
Expand Down
40 changes: 36 additions & 4 deletions end_to_end_test/lib/standard_json.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion end_to_end_test/test/standard_json_serializer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ void main() {
..keyValues['five'] = new JsonObject({'one': 1, 'two': 2})
..zoo.add(new Cat((b) => b
..tail = true
..legs = 4)));
..legs = 4))
..uniqueZoo.add(new Cat((b) => b
..tail = false
..legs = 3)));
final specifiedType = new FullType(StandardJsonValue);
final serializersWithPlugin =
(serializers.toBuilder()..addPlugin(new StandardJsonPlugin())).build();
Expand All @@ -39,6 +42,9 @@ void main() {
'zoo': [
{r'$': 'Cat', 'tail': true, 'legs': 4}
],
'uniqueZoo': [
{r'$': 'Cat', 'tail': false, 'legs': 3}
],
};

test('can be serialized', () {
Expand Down Expand Up @@ -124,6 +130,7 @@ void main() {
'zoo': [
{r'$': 'Cat', 'tail': true, 'legs': 4}
],
'uniqueZoo': [],
};

test('can be serialized', () {
Expand Down

0 comments on commit 74e7e0d

Please sign in to comment.