fork download
  1. using System.Text.Json;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. var contractV2Json = JsonSerializer.Serialize(new ContractV2 { Letters = EvolvedLetters.A });
  8. try{
  9. //we recieve event with new enum, but as a consumer I did not updated and I have old Contract class for this event
  10. //so when I as a consumer will try to get it from the queue this deserialization will happen
  11. var deserializedContractFromContract2 = JsonSerializer.Deserialize<Contract>(contractV2Json);
  12. Console.WriteLine($"deserialization success {deserializedContractFromContract2.Letters}");
  13. }
  14. catch(Exception ex){
  15. Console.WriteLine($"deserialization failed. Ex: {ex}");
  16. }
  17. }
  18. }
  19.  
  20.  
  21. public class Contract
  22. {
  23. public Letters Letters { get; set; }
  24. }
  25.  
  26. public class ContractV2
  27. {
  28. public EvolvedLetters Letters { get; set; }
  29. }
  30.  
  31. public enum Letters
  32. {
  33. A = 1,
  34. B = 2,
  35. C = 3,
  36. }
  37.  
  38. public enum EvolvedLetters
  39. {
  40. A = 1,
  41. B = 2,
  42. C = 3,
  43. D = 4,
  44. }
Success #stdin #stdout 0.12s 33940KB
stdin
Standard input is empty
stdout
deserialization success A