JsonMigrator 1.0.1

.NET Standard 2.0
Install-Package JsonMigrator -Version 1.0.1
dotnet add package JsonMigrator --version 1.0.1
<PackageReference Include="JsonMigrator" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonMigrator --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JsonMigrator, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install JsonMigrator as a Cake Addin
#addin nuget:?package=JsonMigrator&version=1.0.1

// Install JsonMigrator as a Cake Tool
#tool nuget:?package=JsonMigrator&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Small C# Json.NET library to implement migration methods for classes serialized and stored as JSON

Usage

Firstly, it is required to add a string property called JsonVersion to any class using migration methods. This property is used by JsonMigrator to determine which migration methods to execute. Secondly, JsonMigrator will automatically update the JsonVersion property, but it is not responsible for setting the correct version when creating an object using migration.

Say you have this class:

public class DummyClass  
{  
    public string JsonVersion { get; set; }      
    public string StringValue { get; set; }      
    public int IntegerValue { get; set; }      
}

Then you update your program and your class looks like this:

public class DummyClass  
{  
    public string JsonVersion { get; set; }      
    public string StringValue2 { get; set; }      
    public int IntegerValue2 { get; set; }      
}

After another update, your class looks like this:

public class DummyClass  
{  
    public string JsonVersion { get; set; }      
    public string StringValue3 { get; set; }      
    public int IntegerValue3 { get; set; }      
}

For users running the first or second version, their JSON will not deserialize correctly after updating to the latest version. To fix this, add migration methods:

public class DummyClass
{
    public string JsonVersion { get; set; }

    public string StringValue3 { get; set; }

    public int IntegerValue3 { get; set; }

    [Migration("1", "2")]
    private static JToken Migration_1_2(JToken jToken)
    {
        //Version 1 had properties called StringValue and IntegerValue
        jToken["StringValue"].Rename("StringValue2");
        jToken["IntegerValue"].Rename("IntegerValue2");

        return jToken;
    }

    [Migration("2", "3")]
    private static JToken Migration_2_3(JToken jToken)
    {
        //Version 2 had properties called StringValue2 and IntegerValue2
        jToken["StringValue2"].Rename("StringValue3");
        jToken["IntegerValue2"].Rename("IntegerValue3");

        return jToken;
    }
}

Note that the <code>Rename</code> method is an extension method offered by JsonMigrator and not part of the standard Json.Net library. All migration methods must have the same signature: A migration attribute, private static, JToken return type and 1 JToken as parameter.

The migration can then be called using the following code:

var jtoken = JToken.Parse("{\"JsonVersion\":\"1\",\"StringValue\":\"String1\",\"IntegerValue\":1}");
jtoken = JsonMigrator.Migrate<DummyClass>(jtoken);
var dummy = jtoken.ToObject<DummyClass>();

The dummy object will now have migrated to version 3.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 752 2/16/2019