Skip to content

Commit

Permalink
_type_info_key is back as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
sestegra committed May 21, 2016
1 parent 93fb739 commit 3f287de
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions example/serializer_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ModelC extends Serialize {
ModelC([this.name, this.password, this.age]);
}

main() async {
await initSerializer();
main() {
initSerializer(type_info_key: '@dart_type');

ModelA a = new ModelA("toto", 15);
ModelB b = new ModelB("Paris", "France");
Expand All @@ -44,6 +44,6 @@ main() async {
print(Serializer.toJson(a));
print(Serializer.toMap(a));

ModelA A = Serializer.fromJson(Serializer.toJson(a), ModelA);
ModelA A = Serializer.fromJson(Serializer.toJson(a));
print(Serializer.toJson(A));
}
3 changes: 2 additions & 1 deletion lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ abstract class Serializer {
/**
* Init the Serializer by mapping every class annotated with @serializable
*/
initSerializer() {
initSerializer({String type_info_key}) {
_type_info_key = type_info_key;
for (ClassMirror classMirror in serializable.annotatedClasses) {
if (classMirror != null
&& classMirror.simpleName != null
Expand Down
8 changes: 8 additions & 0 deletions lib/src/convert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ Object _fromMap(Map map, [Type type = Map, List<Type> mapOf]) {
return null;
}

if (_type_info_key != null && map.containsKey(_type_info_key)) {
type = _decodeType(map.remove(_type_info_key));
}

// Only Map
if (type == Map) {
Type embedType = mapOf != null ? mapOf[1] : null;
Expand Down Expand Up @@ -179,6 +183,10 @@ Map _toMap(Object obj) {
ClassMirror cm = mir.type;
Map data = new Map();

if (_type_info_key != null) {
data[_type_info_key] = obj.runtimeType.toString();
}

while (cm != null
&& cm.superclass != null
&& Serializer.classes.containsKey(cm.simpleName)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/serializer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "package:reflectable/reflectable.dart";
part "api.dart";
part "convert.dart";

String _type_info_key;

class Serializable extends Reflectable {
const Serializable()
: super.fromList(const [
Expand Down

0 comments on commit 3f287de

Please sign in to comment.