Skip to content

Commit

Permalink
Implement serialiser for mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
sestegra committed Jun 7, 2016
1 parent 9bb41ea commit f1a4ec3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 14 deletions.
10 changes: 5 additions & 5 deletions lib/src/convert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final String MapTypeString = {}.runtimeType.toString();
final String ListTypeString = [].runtimeType.toString();

bool isSerializableClassMirror(Map<String, ClassMirror> serializables, ClassMirror cm) {
return serializables.containsKey(cm.simpleName);
return serializables.containsKey(cm.mixin.simpleName);
}

bool isSerializableVariable(DeclarationMirror vm) {
Expand Down Expand Up @@ -60,13 +60,13 @@ String printAndDumpSerializables() {
initSingletonClasses();
singletonClasses.values.forEach((classMirror) {
var cm = classMirror;
output += _printToString(cm.simpleName);
print(cm.simpleName);
output += _printToString(cm.mixin.simpleName);
print(cm.mixin.simpleName);
while (cm != null
&& cm.superclass != null
&& isSerializableClassMirror(singletonClasses, cm)) {
output += _printToString(" " + cm.simpleName);
print(" " + cm.simpleName);
output += _printToString(" " + cm.mixin.simpleName);
print(" " + cm.mixin.simpleName);
cm.declarations.forEach((symbol, decl) {
if (!decl.isPrivate) {
String name = symbol;
Expand Down
78 changes: 69 additions & 9 deletions test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,26 @@ class Complex extends ProxyA {
Map<String, WithIgnore> ignoreSet;
Map<String, List> listInnerMap;
}

@serializable
class Mixin extends ProxyA with M1, M2 {
String a;
String b;
}

@serializable
class M1 {
String m1;
}

@serializable
class M2 {
String m2;
}

Serializer serializer;

main() {

setUpAll(() {
serializer = new Serializer.Json();
});
Expand Down Expand Up @@ -249,8 +265,10 @@ main() {
test("Serialized name", () {
ModelRenamed _model = new ModelRenamed("Hello")
..tests = ["A", "B", "C"];
expect('{"new":"Hello","tests":["A","B","C"]}', serializer.encode(_model));
expect({"new":"Hello", "tests":["A", "B", "C"]}, serializer.toMap(_model));
expect(
'{"new":"Hello","tests":["A","B","C"]}', serializer.encode(_model));
expect(
{"new":"Hello", "tests":["A", "B", "C"]}, serializer.toMap(_model));
});

test("Test Double", () {
Expand Down Expand Up @@ -306,7 +324,10 @@ main() {

test("list - fromList", () {
List list = serializer.fromList(
[{"@dart_type":"ModelA", "foo":"toto"}, {"@dart_type":"ModelA", "foo":"bar"}],
[
{"@dart_type":"ModelA", "foo":"toto"},
{"@dart_type":"ModelA", "foo":"bar"}
],
ModelA);

expect(2, list.length);
Expand Down Expand Up @@ -417,15 +438,24 @@ main() {
..bools = [ true, false, true]
..ints = [ 1, 2, 3]
..doubles = [ 1.1, 2.2, 3.3]
..dates = [ new DateTime(2016, 12, 24), new DateTime(2016, 12, 25), new DateTime(2016, 12, 26)]
..ignores = [ new WithIgnore("1337A", "42A", "ThisIsASecretA"), new WithIgnore("1337B", "42B", "ThisIsASecretB")
..dates = [
new DateTime(2016, 12, 24),
new DateTime(2016, 12, 25),
new DateTime(2016, 12, 26)
]
..ignores = [
new WithIgnore("1337A", "42A", "ThisIsASecretA"),
new WithIgnore("1337B", "42B", "ThisIsASecretB")
]
..numSet = { "numA": 1, "numB": 12.2}
..stringSet = { "strA": "1", "strB": "3"}
..boolSet = { "ok": true, "nok": false}
..intSet = { "intA": 1, "intB": 12}
..doubleSet = { "dblA": 1.1, "dblB": 12.1}
..dateSet = { "fiesta": new DateTime(2016, 12, 24), "christmas": new DateTime(2016, 12, 25)}
..dateSet = {
"fiesta": new DateTime(2016, 12, 24),
"christmas": new DateTime(2016, 12, 25)
}
..ignoreSet = {
"A": new WithIgnore("1337A", "42A", "ThisIsASecretA"),
"B": new WithIgnore("1337B", "42B", "ThisIsASecretB")
Expand All @@ -446,7 +476,11 @@ main() {
expect(complex.bools, [ true, false, true]);
expect(complex.ints, [ 1, 2, 3]);
expect(complex.doubles, [ 1.1, 2.2, 3.3]);
expect(complex.dates, [ new DateTime(2016, 12, 24), new DateTime(2016, 12, 25), new DateTime(2016, 12, 26)]);
expect(complex.dates, [
new DateTime(2016, 12, 24),
new DateTime(2016, 12, 25),
new DateTime(2016, 12, 26)
]);
expect(complex.ignores[0].a, "1337A");
expect(complex.ignores[0].b, "42A");
expect(complex.ignores[0].secret, null);
Expand All @@ -460,7 +494,10 @@ main() {
expect(complex.boolSet, { "ok": true, "nok": false});
expect(complex.intSet, { "intA": 1, "intB": 12});
expect(complex.doubleSet, { "dblA": 1.0, "dblB": 12.0});
expect(complex.dateSet, { "fiesta": new DateTime(2016, 12, 24), "christmas": new DateTime(2016, 12, 25)});
expect(complex.dateSet, {
"fiesta": new DateTime(2016, 12, 24),
"christmas": new DateTime(2016, 12, 25)
});
expect(complex.ignoreSet["A"].a, "1337A");
expect(complex.ignoreSet["A"].b, "42A");
expect(complex.ignoreSet["A"].secret, null);
Expand All @@ -469,4 +506,27 @@ main() {
expect(complex.ignoreSet["B"].secret, null);
});
});

group("Mixin", () {
test("Serialize", () {
var mixin = new Mixin()
..a = "A"
..b = "B"
..m1 = "M1"
..m2 = "M2";
var json = serializer.encode(mixin);
expect(json, '{"a":"A","b":"B","m2":"M2","m1":"M1"}');
});

test("Deserialize", () {
Mixin mixin = serializer.decode(
'{"a":"A","b":"B","m2":"M2","m1":"M1"}',
Mixin);

expect(mixin.a, "A");
expect(mixin.b, "B");
expect(mixin.m1, "M1");
expect(mixin.m2, "M2");
});
});
}
38 changes: 38 additions & 0 deletions test/typed_json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ class TypedComplex extends TypedProxyA {
Map<String, List> listInnerMap;
}

@serializable
class Mixin extends TypedProxyA with M1, M2 {
String a;
String b;
}

@serializable
class M1 {
String m1;
}

@serializable
class M2 {
String m2;
}

main() {
var serializer = new Serializer.TypedJson();

Expand Down Expand Up @@ -425,4 +441,26 @@ main() {
expect(complex.ignoreSet["B"].secret, null);
});
});

group("Mixin", () {
test("Serialize", () {
var mixin = new Mixin()
..a = "A"
..b = "B"
..m1 = "M1"
..m2 = "M2";
var json = serializer.encode(mixin);
expect(json, '{"@type":"Mixin","a":"A","b":"B","m2":"M2","m1":"M1"}');
});

test("Deserialize", () {
Mixin mixin = serializer.decode(
'{"@type":"Mixin","a":"A","b":"B","m2":"M2","m1":"M1"}');

expect(mixin.a, "A");
expect(mixin.b, "B");
expect(mixin.m1, "M1");
expect(mixin.m2, "M2");
});
});
}

0 comments on commit f1a4ec3

Please sign in to comment.