Skip to content

Commit

Permalink
Merge pull request #1 from jrote1/master
Browse files Browse the repository at this point in the history
pull from origin
  • Loading branch information
adeel41 committed Mar 23, 2015
2 parents 6645998 + c8c23d6 commit 21784db
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
43 changes: 40 additions & 3 deletions lib/src/converters/json_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class JsonConverter implements Converter
return _jsonToBaseObjectData( json );
}

String getPreviousHashcode(Map json) => json[_hashcodeName];

BaseObjectData _jsonToBaseObjectData( dynamic json )
{
if ( json is Map )
Expand All @@ -27,7 +29,7 @@ class JsonConverter implements Converter
properties[key] = _jsonToBaseObjectData( value );
} );
return new ClassObjectData( )
..previousHashCode = (json as Map)[_hashcodeName]
..previousHashCode = getPreviousHashcode(json)
..objectType = json.containsKey("\$type") ? _getClassMirrorByName( json["\$type"] ).reflectedType : null
..properties = properties;
} else if ( json is List )
Expand Down Expand Up @@ -76,14 +78,23 @@ class JsonConverter implements Converter
return JSON.encode( _fromBaseObjectData( baseObjectData ) );
}

void setMetaData(Map result, String hashcode, ClassObjectData classObjectData){
result[_hashcodeName] = hashcode;
setTypeFromObjectType(result, classObjectData);
}

void setTypeFromObjectType(Map json, ClassObjectData classObjectData)
{
json["\$type"] = MirrorSystem.getName( reflectClass( classObjectData.objectType ).qualifiedName );
}

dynamic _fromBaseObjectData( BaseObjectData baseObjectData )
{
if ( baseObjectData is ClassObjectData )
{
var result = {
};
result["\$type"] = MirrorSystem.getName( reflectClass( baseObjectData.objectType ).qualifiedName );
result[_hashcodeName] = baseObjectData.previousHashCode;
setMetaData(result, baseObjectData.previousHashCode, baseObjectData);
baseObjectData.properties.forEach( ( name, value )
{
result[name] = _fromBaseObjectData( value );
Expand All @@ -97,4 +108,30 @@ class JsonConverter implements Converter
}
return (baseObjectData as NativeObjectData).value;
}
}

class NewtonSoftJsonConverter extends JsonConverter
{
@override
void setMetaData(Map result, String hashcode, ClassObjectData classObjectData){
if(classObjectData.properties.length > 0 )
{
result["\$id"] = hashcode;
setTypeFromObjectType(result, classObjectData);
}

else
result["\$ref"] = hashcode;
}



@override
String getPreviousHashcode(Map json)
{
if (json.containsKey("\$ref"))
return json["\$ref"];

return json["\$id"];
}
}
6 changes: 1 addition & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: nomirrorsmap
version: 0.0.8
version: 0.0.11
dependencies:
barback: any
dev_dependencies:
unittest: any
#transformers:
# - nomirrorsmap:
# libraries: ["lib/entites/entites.dart"]
# packages: ["shared"]
1 change: 1 addition & 0 deletions test/test_json/newtonsoft_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"$id":"1","$type":"nomirrorsmap.tests.NewtonSoftTest","age":14,"gender":"m"},{"$ref":"1"}]
44 changes: 44 additions & 0 deletions test/tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import 'dart:collection';

part 'transformer_tests.dart';

String getFileContent(String fileName){
return new io.File.fromUri( new Uri.file( fileName ) ).readAsStringSync( );
}

main( )
{
Expand Down Expand Up @@ -226,6 +229,47 @@ main( )
});
});

group("NewtonSoft json test", ()
{
test( "For fromBaseObjectData, When called with two objects with same reference, Then returned json should have \$id in first object and \$ref in second object", ( )
{
var list = new ListObjectData();
var klass1 = new ClassObjectData();
var klass2 = new ClassObjectData();

klass1.objectType = klass2.objectType = NewtonSoftTest;
klass1.previousHashCode = klass2.previousHashCode = "1";
klass1.properties = {};
klass1.properties["age"] = new NativeObjectData()..value = 14;
klass1.properties["gender"] = new NativeObjectData()..value = "m";

klass2.properties = {};

list.values = [klass1, klass2];

var converter = new NewtonSoftJsonConverter();
String json = converter.fromBaseObjectData(list);

expect(json.contains(getFileContent("test_json\\newtonsoft_test.json")), true);

} );

test( "For toBaseObjectData, When called with two objects with same reference, Then returned objects should restore references", ( )
{
var converter = new NewtonSoftJsonConverter();
ListObjectData json = converter.toBaseObjectData(getFileContent("test_json\\newtonsoft_test.json"));

expect((json.values[0] as ClassObjectData).previousHashCode, "1");
expect((json.values[0] as ClassObjectData).previousHashCode, (json.values[1] as ClassObjectData).previousHashCode);

} );
});

}

class NewtonSoftTest{
int age;
String gender;
}

class Person
Expand Down

0 comments on commit 21784db

Please sign in to comment.