Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 80df32f

Browse files
committed
Merge pull request #26 from davidmorgan/try-unknown
Allow non-serializable static types provided runtime types are serializable.
2 parents 6069627 + 2b84c8b commit 80df32f

16 files changed

+345
-29
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.0.7
4+
5+
- Allow non-serializable static types provided runtime types are serializable.
6+
37
## 0.0.6
48

59
- Fix serialization of null @nullable fields: omit them entirely.

built_json/lib/src/built_json_serializers.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class BuiltJsonSerializers implements Serializers {
4848
} else {
4949
final serializer = _getSerializerByType(specifiedType.root);
5050
if (serializer == null) {
51-
throw new StateError("No serializer for '${specifiedType.root}'.");
51+
// Might be an interface; try resolving using the runtime type.
52+
return serialize(object);
5253
}
5354
if (serializer is StructuredSerializer) {
5455
return serializer
@@ -85,7 +86,8 @@ class BuiltJsonSerializers implements Serializers {
8586
} else {
8687
final serializer = _getSerializerByType(specifiedType.root);
8788
if (serializer == null) {
88-
throw new StateError("No serializer for '${specifiedType.root}'.");
89+
// Might be an interface; try resolving using the runtime type.
90+
return deserialize(object);
8991
}
9092

9193
if (serializer is StructuredSerializer) {

built_json/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: built_json
2-
version: 0.0.6
2+
version: 0.0.7
33
description: >
44
JSON serialization for Built Collections, Built Values and Enum Classes.
55
This library is the runtime dependency.

built_json_generator/lib/src/source_field.dart

-8
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ abstract class SourceField implements Built<SourceField, SourceFieldBuilder> {
6363
}
6464

6565
static String _generateFullType(String type) {
66-
// TODO(davidmorgan): Object or any abstract class that is not a Built
67-
// Value should return 'unspecified'; serialization will have to use the
68-
// runtime type as the static type is not specific enough. Add detection
69-
// for the abstract class case.
70-
if (type == 'Object') {
71-
return 'FullType.unspecified';
72-
}
73-
7466
// TODO(davidmorgan): support more than one level of nesting.
7567
final bareType = _getBareType(type);
7668
final generics = _getGenerics(type);

built_json_generator/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: built_json_generator
2-
version: 0.0.6
2+
version: 0.0.7
33
description: >
44
JSON serialization for Built Collections, Built Values and Enum Classes.
55
This library is the dev dependency.
@@ -13,7 +13,7 @@ environment:
1313
dependencies:
1414
analyzer: '>=0.27.1 <0.28.0'
1515
built_collection: '^1.0.1'
16-
built_json: '^0.0.6'
16+
built_json: '^0.0.7'
1717
source_gen: '>=0.4.3 <0.5.0'
1818
quiver: '>=0.21.0 <0.22.0'
1919

example/lib/compound_value.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ library compound_value;
66

77
import 'package:built_json/built_json.dart';
88
import 'package:built_value/built_value.dart';
9-
import 'package:example/value.dart';
9+
import 'package:example/has_int.dart';
1010
import 'package:example/test_enum.dart';
11+
import 'package:example/value.dart';
1112

1213
part 'compound_value.g.dart';
1314

@@ -22,6 +23,7 @@ abstract class CompoundValue
2223
static final Serializer<CompoundValue> serializer = _$compoundValueSerializer;
2324

2425
Value get aValue;
26+
HasInt get aHasInt;
2527
TestEnum get aTestEnum;
2628

2729
CompoundValue._();
@@ -32,6 +34,7 @@ abstract class CompoundValue
3234
abstract class CompoundValueBuilder
3335
implements Builder<CompoundValue, CompoundValueBuilder> {
3436
ValueBuilder aValue = new ValueBuilder();
37+
HasInt aHasInt;
3538
TestEnum aTestEnum;
3639

3740
CompoundValueBuilder._();

example/lib/compound_value.g.dart

+20-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/has_int.dart

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details.
2+
3+
// All rights reserved. Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
library has_int;
7+
8+
import 'package:built_json/built_json.dart';
9+
import 'package:built_value/built_value.dart';
10+
import 'package:enum_class/enum_class.dart';
11+
12+
part 'has_int.g.dart';
13+
14+
/// Example "serializable" interface.
15+
///
16+
/// In fact it is the implementations of the interface that must be
17+
/// serializable. See examples below.
18+
abstract class HasInt {
19+
int get anInt;
20+
}
21+
22+
/// Example [HasInt] implementation that is not serializable.
23+
///
24+
/// To be serializable it must use built_value or enum_class.
25+
abstract class WrongHasInt implements HasInt {
26+
int anInt = 4;
27+
}
28+
29+
/// Example [HasInt] that is serializable because it uses built_value.
30+
abstract class ValueWithInt
31+
implements Built<ValueWithInt, ValueWithIntBuilder>, HasInt {
32+
/// Serializer field makes the built_value serializable.
33+
static final Serializer<ValueWithInt> serializer = _$valueWithIntSerializer;
34+
static final int youCanHaveStaticFields = 3;
35+
36+
@override
37+
int get anInt;
38+
39+
String get note;
40+
41+
ValueWithInt._();
42+
43+
factory ValueWithInt([updates(ValueWithIntBuilder b)]) = _$ValueWithInt;
44+
}
45+
46+
/// Builder class for [ValueWithInt].
47+
abstract class ValueWithIntBuilder
48+
implements Builder<ValueWithInt, ValueWithIntBuilder> {
49+
int anInt;
50+
String note;
51+
52+
ValueWithIntBuilder._();
53+
54+
factory ValueWithIntBuilder() = _$ValueWithIntBuilder;
55+
}
56+
57+
/// Example [HasInt] that is serializable because it uses enum_class.
58+
class EnumWithInt extends EnumClass implements HasInt {
59+
/// Serializer field makes the enum_class serializable.
60+
static final Serializer<EnumWithInt> serializer = _$enumWithIntSerializer;
61+
62+
static const EnumWithInt one = _$one;
63+
static const EnumWithInt two = _$two;
64+
static const EnumWithInt three = _$three;
65+
66+
const EnumWithInt._(String name) : super(name);
67+
68+
static BuiltSet<EnumWithInt> get values => _$values;
69+
70+
static EnumWithInt valueOf(String name) => _$valueOf(name);
71+
72+
@override
73+
int get anInt {
74+
switch (this) {
75+
case one:
76+
return 1;
77+
case two:
78+
return 2;
79+
case three:
80+
return 3;
81+
default:
82+
throw new StateError(this.toString());
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)