JsonDocumentExt.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Json;
  5. namespace Quadarax.Foundation.Core.Json.Extensions
  6. {
  7. public static class JsonDocumentExt
  8. {
  9. public static IDictionary<string, DiffOccurrence> Diff(this JsonDocument src, JsonDocument trg)
  10. {
  11. if (src == null) throw new ArgumentNullException(nameof(src));
  12. if (trg == null) throw new ArgumentNullException(nameof(trg));
  13. var result = new Dictionary<string, DiffOccurrence>();
  14. var srcEnumerator = src.RootElement.EnumerateObject();
  15. var trgEnumerator = trg.RootElement.EnumerateObject();
  16. while (srcEnumerator.MoveNext())
  17. {
  18. var srcProperty = srcEnumerator.Current;
  19. var trgProperty = Find(srcProperty.Name, trgEnumerator.GetEnumerator());
  20. if (trgProperty == null)
  21. result.Add(srcProperty.Name, new DiffOccurrence(srcProperty.Name, DiffTypeEnum.TrgMissing, srcProperty.Value.GetRawText(), null));
  22. else if (!string.Equals(srcProperty.Value.GetRawText(), trgProperty.Value.Value.GetRawText(),StringComparison.InvariantCultureIgnoreCase))
  23. result.Add(srcProperty.Name, new DiffOccurrence(srcProperty.Name, DiffTypeEnum.Different, srcProperty.Value.GetRawText(), trgProperty.Value.Value.GetRawText()));
  24. }
  25. while (trgEnumerator.MoveNext())
  26. {
  27. var trgProperty = trgEnumerator.Current;
  28. var srcProperty = Find(trgProperty.Name, srcEnumerator.GetEnumerator());
  29. if (srcProperty == null)
  30. {
  31. result.Add(trgProperty.Name, new DiffOccurrence(trgProperty.Name, DiffTypeEnum.SrcMissing, null, trgProperty.Value.ToString()));
  32. }
  33. }
  34. return result;
  35. }
  36. private static JsonProperty? Find(string name, IEnumerator<JsonProperty> enumerator)
  37. {
  38. while (enumerator.MoveNext())
  39. {
  40. var property = enumerator.Current;
  41. if (property.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase))
  42. return property;
  43. }
  44. return null;
  45. }
  46. #region *** Nested classes ***
  47. public class DiffOccurrence
  48. {
  49. public string Name { get; private set; }
  50. public DiffTypeEnum DiffType { get; private set; }
  51. public string? SrcValue { get; private set; }
  52. public string? TrgValue { get; private set; }
  53. public DiffOccurrence(string name, DiffTypeEnum diffType, string? srcValue, string? trgValue)
  54. {
  55. if (string.IsNullOrEmpty(name))
  56. throw new ArgumentNullException(nameof(name));
  57. Name = name;
  58. DiffType = diffType;
  59. SrcValue = srcValue;
  60. TrgValue = trgValue;
  61. }
  62. public override string ToString()
  63. {
  64. var sb = new StringBuilder();
  65. sb.Append("Name=").Append(Name).Append(";DiffType=").Append(DiffType);
  66. return sb.ToString();
  67. }
  68. }
  69. public enum DiffTypeEnum
  70. {
  71. SrcMissing,
  72. TrgMissing,
  73. Different
  74. }
  75. #endregion
  76. }
  77. }