Migrations.Json.Net
Migrations.Json.Net copied to clipboard
Migration breaks when using PreserveReferencesHandling
I can't seem to figure out how to resolve object references in a migration method if PreserveReferencesHandling was turned on during serialization. In that case the migrating object's JObject data may have a reference to the object (ex. "$ref": "123") instead of the actual JSON data. Is there a good solution for passing along all the object references from the top of the object graph?
Update: I'm pretty sure this is due to a reference loop. I noticed that MigrationConverterSpec.ShouldMigrateTypesWithReferenceLoops() unit test is skipped because "Not a requirement by now".
Any chance you might have some hints to get me started with a PR to add that?
Basically, migrations are not getting called for some serialized objects that happen to be referenced by "$ref" later on. I attempted to fix MigrationConverter.ReadJson to check for ref, and to return the referenced object:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var data = JToken.Load(reader);
//don't try and repeat migration for objects serialized as refs to previous
if (data["$ref"] != null)
{
return serializer.ReferenceResolver.ResolveReference(serializer, (string)data["$ref"]);
}
var migratedData = DataMigrator.TryMigrate(data, objectType, serializer);
...
This fix does work to prevent the migration methods from getting called on encountered "$ref" JTokens. However, the migrations DO NOT get called on the initial deserialization of those objects.
Just to confirm that there isn't anything wonky with my test data or a fundamental misunderstanding of anything, I implemented a JsonConverter<> to perform the same migrations as I was attempting to do with this lib. For the very same dataset, all JTokens for my class are migrated correctly in order. So there is something about your particular implementation that breaks when using references, besides not detecting/resolving them in ReadJson.
Update: I punted and wrote my own migration code. I'm somewhat confident that this library could be updated to suit my purposes but I just don't have time to work on a PR at the moment. I may revisit, or in case someone else digs into this, the following SO links may prove helpful: Trying to implement a JSON version migrator by implementing JsonConverter<T> Generic method of modifying JSON before being returned to client JSON.Net throws StackOverflowException when using [JsonConvert()]
@Awsmolak hey mate!
I rewrote this plugin and made it from 5 to 10 times faster Link to plugin: https://github.com/vangogih/FastMigrations.Json.Net
And also added the feature with PreserveReferencesHandling support.
Link to test case
It has been a while since your comment but I hope you will find the time to check this test case to be sure that I have wrote it correctly ;)
Looking forward to getting feedback from you!