diff --git a/lib/src/converters/json_converter.dart b/lib/src/converters/json_converter.dart index cd49b53..b348ffb 100644 --- a/lib/src/converters/json_converter.dart +++ b/lib/src/converters/json_converter.dart @@ -78,8 +78,14 @@ class JsonConverter implements Converter return JSON.encode( _fromBaseObjectData( baseObjectData ) ); } - void setHashcodeInformation(Map result, String hashcode, bool hasProperties){ + 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 ) @@ -88,8 +94,7 @@ class JsonConverter implements Converter { var result = { }; - result["\$type"] = MirrorSystem.getName( reflectClass( baseObjectData.objectType ).qualifiedName ); - setHashcodeInformation(result, baseObjectData.previousHashCode, baseObjectData.properties.length > 0); + setMetaData(result, baseObjectData.previousHashCode, baseObjectData); baseObjectData.properties.forEach( ( name, value ) { result[name] = _fromBaseObjectData( value ); @@ -108,13 +113,19 @@ class JsonConverter implements Converter class NewtonSoftJsonConverter extends JsonConverter { @override - void setHashcodeInformation(Map result, String hashcode, bool hasProperties){ - if(hasProperties) + 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) { diff --git a/pubspec.yaml b/pubspec.yaml index 8242ee6..4d0d151 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: nomirrorsmap -version: 0.0.10 +version: 0.0.11 dependencies: barback: any dev_dependencies: diff --git a/test/test_json/newtonsoft_test.json b/test/test_json/newtonsoft_test.json index 52518dc..068b32e 100644 --- a/test/test_json/newtonsoft_test.json +++ b/test/test_json/newtonsoft_test.json @@ -1 +1 @@ -[{"$type":"nomirrorsmap.tests.NewtonSoftTest","$id":"1","age":14,"gender":"m"},{"$type":"nomirrorsmap.tests.NewtonSoftTest","$ref":"1"}] \ No newline at end of file +[{"$id":"1","$type":"nomirrorsmap.tests.NewtonSoftTest","age":14,"gender":"m"},{"$ref":"1"}] \ No newline at end of file