diff --git a/lib/src/converters/json_converter.dart b/lib/src/converters/json_converter.dart index ce0862a..cd49b53 100644 --- a/lib/src/converters/json_converter.dart +++ b/lib/src/converters/json_converter.dart @@ -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 ) @@ -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 ) @@ -76,6 +78,10 @@ class JsonConverter implements Converter return JSON.encode( _fromBaseObjectData( baseObjectData ) ); } + void setHashcodeInformation(Map result, String hashcode, bool hasProperties){ + result[_hashcodeName] = hashcode; + } + dynamic _fromBaseObjectData( BaseObjectData baseObjectData ) { if ( baseObjectData is ClassObjectData ) @@ -83,7 +89,7 @@ class JsonConverter implements Converter var result = { }; result["\$type"] = MirrorSystem.getName( reflectClass( baseObjectData.objectType ).qualifiedName ); - result[_hashcodeName] = baseObjectData.previousHashCode; + setHashcodeInformation(result, baseObjectData.previousHashCode, baseObjectData.properties.length > 0); baseObjectData.properties.forEach( ( name, value ) { result[name] = _fromBaseObjectData( value ); @@ -97,4 +103,24 @@ class JsonConverter implements Converter } return (baseObjectData as NativeObjectData).value; } +} + +class NewtonSoftJsonConverter extends JsonConverter +{ + @override + void setHashcodeInformation(Map result, String hashcode, bool hasProperties){ + if(hasProperties) + result["\$id"] = hashcode; + else + result["\$ref"] = hashcode; + } + + @override + String getPreviousHashcode(Map json) + { + if (json.containsKey("\$ref")) + return json["\$ref"]; + + return json["\$id"]; + } } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 18a0783..8242ee6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: nomirrorsmap -version: 0.0.9 +version: 0.0.10 dependencies: barback: any dev_dependencies: diff --git a/test/test_json/newtonsoft_test.json b/test/test_json/newtonsoft_test.json new file mode 100644 index 0000000..52518dc --- /dev/null +++ b/test/test_json/newtonsoft_test.json @@ -0,0 +1 @@ +[{"$type":"nomirrorsmap.tests.NewtonSoftTest","$id":"1","age":14,"gender":"m"},{"$type":"nomirrorsmap.tests.NewtonSoftTest","$ref":"1"}] \ No newline at end of file diff --git a/test/tests.dart b/test/tests.dart index da80bef..b0c7205 100644 --- a/test/tests.dart +++ b/test/tests.dart @@ -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( ) { @@ -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