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

Commit 9d39a5e

Browse files
committed
Add primitive serializers, generator, example and tests.
1 parent ffdd39a commit 9d39a5e

40 files changed

+1245
-843
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.buildlog
2+
.DS_Store
3+
.idea
4+
.pub/
5+
.settings/
6+
*.iml
7+
**/build
8+
**/packages
9+
**/pubspec.lock
10+
**/.packages

CHANGELOG.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
# Changelog
22

3-
## 0.0.3
4-
5-
- Allow static fields in value class.
6-
- Allow custom setters in builder.
7-
8-
## 0.0.2
9-
10-
- Fix error message for missing builder class.
11-
- Allow non-abstract getters in value class.
12-
133
## 0.0.1
144

155
- Generator, tests and example.

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
# Built Values for Dart
1+
# Built JSON for Dart
22

3-
Built values are value types for Dart.
4-
5-
They have generated `equals`, `hashCode`, `toString` and builder class.
6-
7-
This provides an easy way to write deeply immutable classes for Dart that
8-
behave in a consistent, predicatable way.
3+
JSON serialization for Built Collections, Built Values and Enum Classes.
94

105
See
11-
[this example](https://github.com/google/built_value.dart/tree/master/example)
12-
for a full project with a `build.dart` and some example value types.
6+
[this example](https://github.com/google/built_json.dart/tree/master/example)
7+
for a full project with a `build.dart` and some examples.
8+
9+
This project is not yet ready for use. Watch this space!
1310

1411
## Features and bugs
1512

1613
Please file feature requests and bugs at the [issue tracker][tracker].
1714

18-
[tracker]: https://github.com/google/built_value.dart/issues
15+
[tracker]: https://github.com/google/built_json.dart/issues
File renamed without changes.

built_json/lib/built_json.dart

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright (c) 2015, 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+
library built_json;
6+
7+
import 'src/bool_serializer.dart';
8+
import 'src/built_list_serializer.dart';
9+
import 'src/built_map_serializer.dart';
10+
import 'src/built_set_serializer.dart';
11+
import 'src/double_serializer.dart';
12+
import 'src/int_serializer.dart';
13+
import 'src/string_serializer.dart';
14+
15+
/// Serializes a single class.
16+
///
17+
/// See <https://github.com/google/built_json.dart/tree/master/example>
18+
abstract class BuiltJsonSerializer<T> {
19+
Type get type;
20+
String get typeName;
21+
22+
Object serialize(BuiltJsonSerializers builtJsonSerializers, T object,
23+
{String expectedType});
24+
T deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
25+
{String expectedType});
26+
}
27+
28+
/// Serializes all transitive dependencies of a class.
29+
///
30+
/// See <https://github.com/google/built_json.dart/tree/master/example>
31+
// TODO(davidmorgan): make immutable.
32+
class BuiltJsonSerializers {
33+
Map<String, String> _deobfuscatedNames = <String, String>{};
34+
Map<String, BuiltJsonSerializer> _serializersByName =
35+
<String, BuiltJsonSerializer>{};
36+
37+
BuiltJsonSerializers() {
38+
add(new BoolSerializer());
39+
add(new DoubleSerializer());
40+
add(new IntSerializer());
41+
add(new StringSerializer());
42+
43+
add(new BuiltListSerializer());
44+
add(new BuiltMapSerializer());
45+
add(new BuiltSetSerializer());
46+
}
47+
48+
void addAll(BuiltJsonSerializers builtJsonSerializers) {
49+
for (final serializer in builtJsonSerializers._serializersByName.values) {
50+
add(serializer);
51+
}
52+
}
53+
54+
void add(BuiltJsonSerializer builtJsonSerializer) {
55+
_deobfuscatedNames[_getName(builtJsonSerializer.type)] =
56+
builtJsonSerializer.typeName;
57+
_serializersByName[builtJsonSerializer.typeName] = builtJsonSerializer;
58+
}
59+
60+
Object serialize(Object object, {String expectedType}) {
61+
final rawName = _deobfuscatedNames[_getName(object.runtimeType)];
62+
if (rawName == null) throw new StateError(
63+
"No serializer for '${object.runtimeType}'.");
64+
65+
var genericType = _getGenericName(object.runtimeType);
66+
67+
// TODO(davidmorgan): handle this generically.
68+
if (genericType == 'BuiltList<int>') {
69+
genericType = 'List<int>';
70+
}
71+
if (genericType == 'BuiltSet<int>') {
72+
genericType = 'Set<int>';
73+
}
74+
75+
final genericName =
76+
genericType == null ? rawName : '$rawName<$genericType>';
77+
78+
if (genericName == expectedType) {
79+
return _serializersByName[rawName]
80+
.serialize(this, object, expectedType: genericType);
81+
} else {
82+
return <String, Object>{
83+
genericName: _serializersByName[rawName]
84+
.serialize(this, object, expectedType: genericType)
85+
};
86+
}
87+
}
88+
89+
Object deserialize(Object object, {String expectedType}) {
90+
if (object is Map) {
91+
if (object.keys.length > 1) {
92+
// Must be expectedType.
93+
// TODO(davidmorgan): distinguish in the one field case.
94+
if (expectedType == null) {
95+
throw new StateError('Need an expected type here.');
96+
}
97+
final typeName = _makeRaw(expectedType);
98+
final genericName = _getGeneric(expectedType);
99+
return _serializersByName[typeName]
100+
.deserialize(this, object, expectedType: genericName);
101+
} else {
102+
final typeName = _makeRaw(object.keys.single);
103+
final genericName = _getGeneric(object.keys.single);
104+
return _serializersByName[typeName]
105+
.deserialize(this, object.values.single, expectedType: genericName);
106+
}
107+
} else {
108+
final serializer = _serializersByName[_makeRaw(expectedType)];
109+
if (serializer == null) {
110+
throw new StateError('No serializer for $expectedType');
111+
}
112+
return serializer.deserialize(this, object,
113+
expectedType: _getGeneric(expectedType));
114+
}
115+
}
116+
117+
String _getName(Type type) => _makeRaw(type.toString());
118+
119+
String _makeRaw(String name) {
120+
final genericsStart = name.indexOf('<');
121+
return genericsStart == -1 ? name : name.substring(0, genericsStart);
122+
}
123+
124+
String _getGenericName(Type type) => _getGeneric(type.toString());
125+
126+
String _getGeneric(String name) {
127+
final genericsStart = name.indexOf('<');
128+
return genericsStart == -1
129+
? null
130+
: name.substring(genericsStart + 1, name.length - 1);
131+
}
132+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
6+
7+
class BoolSerializer implements BuiltJsonSerializer<bool> {
8+
final Type type = bool;
9+
final String typeName = 'bool';
10+
11+
Object serialize(BuiltJsonSerializers builtJsonSerializers, bool object,
12+
{String expectedType}) {
13+
return object;
14+
}
15+
16+
bool deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
17+
{String expectedType}) {
18+
return object as bool;
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
7+
8+
class BuiltListSerializer implements BuiltJsonSerializer<BuiltList> {
9+
final Type type = BuiltList;
10+
final String typeName = 'List';
11+
12+
Object serialize(BuiltJsonSerializers builtJsonSerializers, BuiltList object,
13+
{String expectedType}) {
14+
return object
15+
.map((item) =>
16+
builtJsonSerializers.serialize(item, expectedType: expectedType))
17+
.toList();
18+
}
19+
20+
BuiltList deserialize(
21+
BuiltJsonSerializers builtJsonSerializers, Object object,
22+
{String expectedType}) {
23+
return new BuiltList<Object>((object as Iterable).map((item) =>
24+
builtJsonSerializers.deserialize(item, expectedType: expectedType)));
25+
}
26+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
7+
8+
class BuiltMapSerializer implements BuiltJsonSerializer<BuiltMap> {
9+
final Type type = BuiltMap;
10+
final String typeName = 'Map';
11+
12+
Object serialize(BuiltJsonSerializers builtJsonSerializers, BuiltMap object,
13+
{String expectedType}) {
14+
final expectedKeyType =
15+
expectedType.substring(0, expectedType.indexOf(', '));
16+
final expectedValueType =
17+
expectedType.substring(expectedType.indexOf(', ') + 2);
18+
19+
final result = <Object>[];
20+
for (final key in object.keys) {
21+
result.add(
22+
builtJsonSerializers.serialize(key, expectedType: expectedKeyType));
23+
final value = object[key];
24+
result.add(builtJsonSerializers.serialize(value,
25+
expectedType: expectedValueType));
26+
}
27+
return result;
28+
}
29+
30+
BuiltMap deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
31+
{String expectedType}) {
32+
final expectedKeyType =
33+
expectedType.substring(0, expectedType.indexOf(', '));
34+
final expectedValueType =
35+
expectedType.substring(expectedType.indexOf(', ') + 2);
36+
37+
final result = new MapBuilder<Object, Object>();
38+
final list = object as List<Object>;
39+
40+
if (list.length & 1 == 1) {
41+
throw new ArgumentError('odd length');
42+
}
43+
44+
for (int i = 0; i != list.length; i += 2) {
45+
final key = builtJsonSerializers.deserialize(list[i],
46+
expectedType: expectedKeyType);
47+
final value = builtJsonSerializers.deserialize(list[i + 1],
48+
expectedType: expectedValueType);
49+
result[key] = value;
50+
}
51+
52+
return result.build();
53+
}
54+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
7+
8+
class BuiltSetSerializer implements BuiltJsonSerializer<BuiltSet> {
9+
final Type type = BuiltSet;
10+
final String typeName = 'Set';
11+
12+
Object serialize(BuiltJsonSerializers builtJsonSerializers, BuiltSet object,
13+
{String expectedType}) {
14+
return object
15+
.map((item) =>
16+
builtJsonSerializers.serialize(item, expectedType: expectedType))
17+
.toList();
18+
}
19+
20+
BuiltSet deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
21+
{String expectedType}) {
22+
return new BuiltSet<Object>((object as Iterable).map((item) =>
23+
builtJsonSerializers.deserialize(item, expectedType: expectedType)));
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
6+
7+
// TODO(davidmorgan): support special values.
8+
class DoubleSerializer implements BuiltJsonSerializer<double> {
9+
final Type type = double;
10+
final String typeName = 'double';
11+
12+
Object serialize(BuiltJsonSerializers builtJsonSerializers, double object,
13+
{String expectedType}) {
14+
return object.toString();
15+
}
16+
17+
double deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
18+
{String expectedType}) {
19+
return double.parse(object as String);
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
6+
7+
class IntSerializer implements BuiltJsonSerializer<int> {
8+
final Type type = int;
9+
final String typeName = 'int';
10+
11+
Object serialize(BuiltJsonSerializers builtJsonSerializers, int object,
12+
{String expectedType}) {
13+
return object;
14+
}
15+
16+
int deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
17+
{String expectedType}) {
18+
return object as int;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2015, 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_json/built_json.dart';
6+
7+
class StringSerializer implements BuiltJsonSerializer<String> {
8+
final Type type = String;
9+
final String typeName = 'String';
10+
11+
Object serialize(BuiltJsonSerializers builtJsonSerializers, String object,
12+
{String expectedType}) {
13+
return object;
14+
}
15+
16+
String deserialize(BuiltJsonSerializers builtJsonSerializers, Object object,
17+
{String expectedType}) {
18+
return object as String;
19+
}
20+
}

built_json/pubspec.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: built_json
2+
version: 0.0.1
3+
description: >
4+
JSON serialization for Built Collections, Built Values and Enum Classes.
5+
This library is the runtime dependency.
6+
authors:
7+
- David Morgan <davidmorgan@google.com>
8+
homepage: https://github.com/google/built_json.dart
9+
10+
environment:
11+
sdk: '>=1.8.0 <2.0.0'
12+
13+
dependencies:
14+
built_collection: '^1.0.0'
15+
quiver: '>=0.21.0 <0.22.0'
16+
17+
dev_dependencies:
18+
test: any

0 commit comments

Comments
 (0)