JsonMigrator 1.0.1

dotnet add package JsonMigrator --version 1.0.1
NuGet\Install-Package JsonMigrator -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: JsonMigrator, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 1,447 2/16/2019