AnalyzeStructure.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections.Generic;
  2. using Newtonsoft.Json.Linq;
  3. namespace analyzeJSON
  4. {
  5. public record AnalysisResult(Dictionary<string, int> Nodes, Dictionary<string, int> Leafs);
  6. public class AnalyzeStructure
  7. {
  8. private Dictionary<string, int> nodes = new Dictionary<string, int>();
  9. private Dictionary<string, int> leafs = new Dictionary<string, int>();
  10. public AnalyzeStructure()
  11. {
  12. }
  13. public void AnalyzeToken(JToken token)
  14. {
  15. if (token.Type == JTokenType.Object || token.Type == JTokenType.Array)
  16. return;
  17. var tokenName = AnalyzeJSON.GetNameFromPath(token.Path);
  18. if (token.HasValues)
  19. {
  20. if (token.First.Equals(token.Last) &&
  21. token.First.Type != JTokenType.Array && token.First.Type != JTokenType.Property && token.First.Type != JTokenType.Object)
  22. {
  23. return;
  24. }
  25. if (nodes.ContainsKey(tokenName))
  26. nodes[tokenName]++;
  27. else
  28. nodes.Add(tokenName, 1);
  29. }
  30. else
  31. {
  32. if (leafs.ContainsKey(tokenName))
  33. leafs[tokenName]++;
  34. else
  35. leafs.Add(tokenName, 1);
  36. }
  37. }
  38. public AnalysisResult Result => new AnalysisResult(nodes, leafs);
  39. }
  40. }