using System.Text.Json;
public class Test
{
public static void Main()
{
var contractV2Json = JsonSerializer.Serialize(new ContractV2 { Letters = EvolvedLetters.A });
try{
//we recieve event with new enum, but as a consumer I did not updated and I have old Contract class for this event
//so when I as a consumer will try to get it from the queue this deserialization will happen
var deserializedContractFromContract2 = JsonSerializer.Deserialize<Contract>(contractV2Json);
Console.WriteLine($"deserialization success {deserializedContractFromContract2.Letters}");
}
catch(Exception ex){
Console.WriteLine($"deserialization failed. Ex: {ex}");
}
}
}
public class Contract
{
public Letters Letters { get; set; }
}
public class ContractV2
{
public EvolvedLetters Letters { get; set; }
}
public enum Letters
{
A = 1,
B = 2,
C = 3,
}
public enum EvolvedLetters
{
A = 1,
B = 2,
C = 3,
D = 4,
}
dXNpbmcgU3lzdGVtLlRleHQuSnNvbjsKIApwdWJsaWMgY2xhc3MgVGVzdAp7CglwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCgl7CgkJdmFyIGNvbnRyYWN0VjJKc29uID0gSnNvblNlcmlhbGl6ZXIuU2VyaWFsaXplKG5ldyBDb250cmFjdFYyIHsgTGV0dGVycyA9IEV2b2x2ZWRMZXR0ZXJzLkEgfSk7CgkJdHJ5ewoJCQkvL3dlIHJlY2lldmUgZXZlbnQgd2l0aCBuZXcgZW51bSwgYnV0IGFzIGEgY29uc3VtZXIgSSBkaWQgbm90IHVwZGF0ZWQgYW5kIEkgaGF2ZSBvbGQgQ29udHJhY3QgY2xhc3MgZm9yIHRoaXMgZXZlbnQKCQkJLy9zbyB3aGVuIEkgYXMgYSBjb25zdW1lciB3aWxsIHRyeSB0byBnZXQgaXQgZnJvbSB0aGUgcXVldWUgdGhpcyBkZXNlcmlhbGl6YXRpb24gd2lsbCBoYXBwZW4KCQkJdmFyIGRlc2VyaWFsaXplZENvbnRyYWN0RnJvbUNvbnRyYWN0MiA9IEpzb25TZXJpYWxpemVyLkRlc2VyaWFsaXplPENvbnRyYWN0Pihjb250cmFjdFYySnNvbik7CgkJCUNvbnNvbGUuV3J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