Skip to content

Commit ff3138b

Browse files
committed
Added project for data serialization with JSON type migration
1 parent ae3218b commit ff3138b

File tree

7 files changed

+447
-0
lines changed

7 files changed

+447
-0
lines changed

MADE.NET.sln

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MADE.Data.Validation.Fluent
4747
EndProject
4848
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MADE.Data.Validation.FluentValidation.Tests", "tests\MADE.Data.Validation.FluentValidation.Tests\MADE.Data.Validation.FluentValidation.Tests.csproj", "{D8C1B3CC-B5BA-4946-944E-D898AB4DFF6E}"
4949
EndProject
50+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MADE.Data.Serialization", "src\MADE.Data.Serialization\MADE.Data.Serialization.csproj", "{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}"
51+
EndProject
52+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MADE.Data.Serialization.Tests", "tests\MADE.Data.Serialization.Tests\MADE.Data.Serialization.Tests.csproj", "{7D789D04-A010-4F11-91AD-B1B94A23BAE0}"
53+
EndProject
5054
Global
5155
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5256
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -1199,6 +1203,118 @@ Global
11991203
{D8C1B3CC-B5BA-4946-944E-D898AB4DFF6E}.Release|x64.Build.0 = Release|Any CPU
12001204
{D8C1B3CC-B5BA-4946-944E-D898AB4DFF6E}.Release|x86.ActiveCfg = Release|Any CPU
12011205
{D8C1B3CC-B5BA-4946-944E-D898AB4DFF6E}.Release|x86.Build.0 = Release|Any CPU
1206+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1207+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1208+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1209+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1210+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|ARM64.ActiveCfg = Debug|Any CPU
1211+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|ARM64.Build.0 = Debug|Any CPU
1212+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1213+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1214+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1215+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1216+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1217+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1218+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1219+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1220+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1221+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1222+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1223+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|ARM.Build.0 = Debug|Any CPU
1224+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|ARM64.ActiveCfg = Debug|Any CPU
1225+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|ARM64.Build.0 = Debug|Any CPU
1226+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1227+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|iPhone.Build.0 = Debug|Any CPU
1228+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1229+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1230+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|x64.ActiveCfg = Debug|Any CPU
1231+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|x64.Build.0 = Debug|Any CPU
1232+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|x86.ActiveCfg = Debug|Any CPU
1233+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.AppStore|x86.Build.0 = Debug|Any CPU
1234+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1235+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|Any CPU.Build.0 = Debug|Any CPU
1236+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|ARM.ActiveCfg = Debug|Any CPU
1237+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|ARM.Build.0 = Debug|Any CPU
1238+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|ARM64.ActiveCfg = Debug|Any CPU
1239+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|ARM64.Build.0 = Debug|Any CPU
1240+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1241+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|iPhone.Build.0 = Debug|Any CPU
1242+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1243+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1244+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|x64.ActiveCfg = Debug|Any CPU
1245+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|x64.Build.0 = Debug|Any CPU
1246+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|x86.ActiveCfg = Debug|Any CPU
1247+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Debug|x86.Build.0 = Debug|Any CPU
1248+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|Any CPU.ActiveCfg = Release|Any CPU
1249+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|Any CPU.Build.0 = Release|Any CPU
1250+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|ARM.ActiveCfg = Release|Any CPU
1251+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|ARM.Build.0 = Release|Any CPU
1252+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|ARM64.ActiveCfg = Release|Any CPU
1253+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|ARM64.Build.0 = Release|Any CPU
1254+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|iPhone.ActiveCfg = Release|Any CPU
1255+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|iPhone.Build.0 = Release|Any CPU
1256+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1257+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1258+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|x64.ActiveCfg = Release|Any CPU
1259+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|x64.Build.0 = Release|Any CPU
1260+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|x86.ActiveCfg = Release|Any CPU
1261+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90}.Release|x86.Build.0 = Release|Any CPU
1262+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1263+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1264+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1265+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1266+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|ARM64.ActiveCfg = Debug|Any CPU
1267+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|ARM64.Build.0 = Debug|Any CPU
1268+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1269+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1270+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1271+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1272+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1273+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1274+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1275+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1276+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1277+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1278+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1279+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|ARM.Build.0 = Debug|Any CPU
1280+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|ARM64.ActiveCfg = Debug|Any CPU
1281+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|ARM64.Build.0 = Debug|Any CPU
1282+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1283+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|iPhone.Build.0 = Debug|Any CPU
1284+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1285+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1286+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|x64.ActiveCfg = Debug|Any CPU
1287+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|x64.Build.0 = Debug|Any CPU
1288+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|x86.ActiveCfg = Debug|Any CPU
1289+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.AppStore|x86.Build.0 = Debug|Any CPU
1290+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1291+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
1292+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|ARM.ActiveCfg = Debug|Any CPU
1293+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|ARM.Build.0 = Debug|Any CPU
1294+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|ARM64.ActiveCfg = Debug|Any CPU
1295+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|ARM64.Build.0 = Debug|Any CPU
1296+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1297+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|iPhone.Build.0 = Debug|Any CPU
1298+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1299+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1300+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|x64.ActiveCfg = Debug|Any CPU
1301+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|x64.Build.0 = Debug|Any CPU
1302+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|x86.ActiveCfg = Debug|Any CPU
1303+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Debug|x86.Build.0 = Debug|Any CPU
1304+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
1305+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|Any CPU.Build.0 = Release|Any CPU
1306+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|ARM.ActiveCfg = Release|Any CPU
1307+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|ARM.Build.0 = Release|Any CPU
1308+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|ARM64.ActiveCfg = Release|Any CPU
1309+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|ARM64.Build.0 = Release|Any CPU
1310+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|iPhone.ActiveCfg = Release|Any CPU
1311+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|iPhone.Build.0 = Release|Any CPU
1312+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1313+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1314+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|x64.ActiveCfg = Release|Any CPU
1315+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|x64.Build.0 = Release|Any CPU
1316+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|x86.ActiveCfg = Release|Any CPU
1317+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0}.Release|x86.Build.0 = Release|Any CPU
12021318
EndGlobalSection
12031319
GlobalSection(SolutionProperties) = preSolution
12041320
HideSolutionNode = FALSE
@@ -1224,6 +1340,8 @@ Global
12241340
{C8DF10B0-D157-47CF-BD10-9EE1D06BEB9A} = {01380FB8-F8A7-4416-AABA-5407574B7723}
12251341
{BBAB544A-BFB2-4755-8F09-8E150D3638F3} = {01380FB8-F8A7-4416-AABA-5407574B7723}
12261342
{D8C1B3CC-B5BA-4946-944E-D898AB4DFF6E} = {69149D0F-BB09-411B-88F0-A1E845058D70}
1343+
{0ACCC377-5FA5-47D9-B6EF-7936F1038B90} = {01380FB8-F8A7-4416-AABA-5407574B7723}
1344+
{7D789D04-A010-4F11-91AD-B1B94A23BAE0} = {69149D0F-BB09-411B-88F0-A1E845058D70}
12271345
EndGlobalSection
12281346
GlobalSection(ExtensibilityGlobals) = postSolution
12291347
SolutionGuid = {3921AD86-E6C0-4436-8880-2D9EDFAD6151}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
namespace MADE.Data.Serialization.Json.Binders
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using MADE.Data.Serialization.Json;
9+
using MADE.Data.Serialization.Json.Exceptions;
10+
using Newtonsoft.Json.Serialization;
11+
12+
/// <summary>
13+
/// Defines a serialization binder for JSON.NET for migrating serialized <see cref="Type"/> declarations within a serialized JSON object.
14+
/// </summary>
15+
/// <remarks>
16+
/// This is for migrating serialized types where <code>TypeNameHandling.All</code> has been set in the JSON serializer settings.
17+
/// </remarks>
18+
public class JsonTypeMigrationSerializationBinder : DefaultSerializationBinder
19+
{
20+
private readonly SemaphoreSlim migrationSemaphore;
21+
22+
private readonly List<JsonTypeMigration> migrations = new();
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="JsonTypeMigrationSerializationBinder"/> class.
26+
/// </summary>
27+
/// <remarks>
28+
/// To add migrations, call the <see cref="AddTypeMigrationAsync"/> method.
29+
/// </remarks>
30+
public JsonTypeMigrationSerializationBinder()
31+
: this(null)
32+
{
33+
}
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="JsonTypeMigrationSerializationBinder"/> class with pre-configured type migrations.
37+
/// </summary>
38+
/// <param name="migrations">The type migrations to initialize with.</param>
39+
public JsonTypeMigrationSerializationBinder(params JsonTypeMigration[] migrations)
40+
{
41+
this.migrationSemaphore = new SemaphoreSlim(1, 1);
42+
43+
if (migrations != null && migrations.Any())
44+
{
45+
this.migrations.AddRange(migrations);
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Adds a JSON type migration to the binder.
51+
/// </summary>
52+
/// <param name="migration">The type migration to add.</param>
53+
/// <returns>An asynchronous operation.</returns>
54+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="migration"/> is null.</exception>
55+
/// <exception cref="JsonTypeMigrationException">Thrown if a <paramref name="migration"/> already exists for the from type.</exception>
56+
public async Task AddTypeMigrationAsync(JsonTypeMigration migration)
57+
{
58+
if (migration == null)
59+
{
60+
throw new ArgumentNullException(nameof(migration));
61+
}
62+
63+
await this.migrationSemaphore.WaitAsync();
64+
65+
try
66+
{
67+
JsonTypeMigration existingMigration = this.migrations.FirstOrDefault(
68+
m =>
69+
m.FromAssemblyName == migration.FromAssemblyName &&
70+
m.FromTypeName == migration.FromTypeName);
71+
72+
if (existingMigration != null)
73+
{
74+
throw new JsonTypeMigrationException(
75+
$"A type migration is already registered for type {existingMigration.FromTypeName} in assembly {existingMigration.FromAssemblyName} to {existingMigration.ToType.FullName}");
76+
}
77+
78+
this.migrations.Add(migration);
79+
}
80+
finally
81+
{
82+
this.migrationSemaphore.Release();
83+
}
84+
}
85+
86+
/// <summary>
87+
/// When overridden in a derived class, controls the binding of a serialized object to a type.
88+
/// </summary>
89+
/// <param name="assemblyName">Specifies the <see cref="T:System.Reflection.Assembly" /> name of the serialized object.</param>
90+
/// <param name="typeName">Specifies the <see cref="T:System.Type" /> name of the serialized object.</param>
91+
/// <returns>
92+
/// The type of the object the formatter creates a new instance of.
93+
/// </returns>
94+
public override Type BindToType(string assemblyName, string typeName)
95+
{
96+
Task task = this.migrationSemaphore.WaitAsync();
97+
Task.WaitAll(task);
98+
99+
JsonTypeMigration migration = null;
100+
try
101+
{
102+
migration = this.migrations.FirstOrDefault(
103+
m =>
104+
m.FromAssemblyName == assemblyName && m.FromTypeName == typeName);
105+
}
106+
finally
107+
{
108+
this.migrationSemaphore.Release();
109+
}
110+
111+
return migration != null ? migration.ToType : base.BindToType(assemblyName, typeName);
112+
}
113+
}
114+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace MADE.Data.Serialization.Json.Exceptions
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Defines an exception for errors occurred when interacting with JSON type migrations.
7+
/// </summary>
8+
public class JsonTypeMigrationException : Exception
9+
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="JsonTypeMigrationException"/> class.
12+
/// </summary>
13+
public JsonTypeMigrationException()
14+
{
15+
}
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="JsonTypeMigrationException"/> class with a message that describes the error.
19+
/// </summary>
20+
/// <param name="message">The message that describes the error.</param>
21+
public JsonTypeMigrationException(string message)
22+
: base(message)
23+
{
24+
}
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="JsonTypeMigrationException"/> class with a message that describes the error and inner exception.
28+
/// </summary>
29+
/// <param name="message">The message that describes the error.</param>
30+
/// <param name="innerException">The exception that caused this exception to be thrown.</param>
31+
public JsonTypeMigrationException(string message, Exception innerException)
32+
: base(message, innerException)
33+
{
34+
}
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace MADE.Data.Serialization.Json
2+
{
3+
using System;
4+
using MADE.Data.Serialization.Json.Binders;
5+
6+
/// <summary>
7+
/// Defines the detail for migrating from one type to another using the <see cref="JsonTypeMigrationSerializationBinder"/>.
8+
/// </summary>
9+
public class JsonTypeMigration
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="JsonTypeMigration"/> class with the expected from and to migration types.
13+
/// </summary>
14+
/// <param name="fromType">The type being migrated from.</param>
15+
/// <param name="toType">The type being migrated to.</param>
16+
public JsonTypeMigration(Type fromType, Type toType)
17+
{
18+
this.FromAssemblyName = fromType.Assembly.GetName().Name;
19+
this.FromTypeName = fromType.FullName;
20+
this.ToType = toType;
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="JsonTypeMigration"/> class with the expected from and to migration types.
25+
/// </summary>
26+
/// <param name="fromAssemblyName">The name of the assembly being migrated from.</param>
27+
/// <param name="fromTypeName">The name of the type being migrated from.</param>
28+
/// <param name="toType">The type being migrated to.</param>
29+
public JsonTypeMigration(string fromAssemblyName, string fromTypeName, Type toType)
30+
{
31+
this.FromAssemblyName = fromAssemblyName;
32+
this.FromTypeName = fromTypeName;
33+
this.ToType = toType;
34+
}
35+
36+
/// <summary>
37+
/// Gets the name of the assembly being migrated from.
38+
/// </summary>
39+
public string FromAssemblyName { get; }
40+
41+
/// <summary>
42+
/// Gets the name of the type being migrated from.
43+
/// </summary>
44+
public string FromTypeName { get; }
45+
46+
/// <summary>
47+
/// Gets the type being migrated to.
48+
/// </summary>
49+
public Type ToType { get; }
50+
}
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="MSBuild.Sdk.Extras">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<Product>MADE.NET Data Serialization</Product>
7+
<Description>
8+
This package includes:
9+
- JsonTypeMigrationSerializationBinder for migrating type names within a serialized JSON object.
10+
</Description>
11+
<PackageTags>MADE Data Serialization JSON</PackageTags>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)