Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. using System;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4.  
  5. namespace DuplicatedJsonProperties
  6. {
  7. class DuplicatedJsonProperties
  8. {
  9. private const string data =
  10. @"{
  11. ""language"": ""esperanto"",
  12. ""title"": ""Primeiro Manual de Esperanto"",
  13. ""title"": ""Fundamento de Esperanto"",
  14. ""author"" : ""Ludwik Lejzer Zamenhof""
  15. }";
  16.  
  17. static void Main(string[] args)
  18. {
  19. Console.WriteLine("---Input---");
  20. Console.WriteLine(data);
  21.  
  22. PrintJson("Throw exception on duplicates", DuplicatePropertyNameHandling.Error);
  23. PrintJson("Ignore duplicates", DuplicatePropertyNameHandling.Ignore);
  24. PrintJson("Replace duplicates with the last property", DuplicatePropertyNameHandling.Replace);
  25.  
  26. Console.ReadKey();
  27. }
  28.  
  29. private static void PrintJson(string header, DuplicatePropertyNameHandling duplicateFlag)
  30. {
  31. try
  32. {
  33. Console.WriteLine();
  34. Console.WriteLine("---" + header + "---");
  35.  
  36. JsonLoadSettings jsonLoadSettings = (duplicateFlag == DuplicatePropertyNameHandling.Replace)
  37. ? null
  38. : new JsonLoadSettings { DuplicatePropertyNameHandling = duplicateFlag };
  39.  
  40. JToken jToken = JToken.Parse(data, jsonLoadSettings);
  41.  
  42. Console.WriteLine(jToken.ToString(Formatting.Indented));
  43. Console.WriteLine();
  44. }
  45. catch (JsonReaderException jsonReaderException)
  46. {
  47. Console.WriteLine("Exception thrown: " + jsonReaderException.Message);
  48. }
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement