GithubHelp home page GithubHelp logo

Comments (2)

Awsmolak avatar Awsmolak commented on May 25, 2024

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()]

from migrations.json.net.

vangogih avatar vangogih commented on May 25, 2024

@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!

from migrations.json.net.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.