Browse Source

Added the case where node can also be leaf

Piotr Czajkowski 2 years ago
parent
commit
4b932f1baa

+ 13 - 8
AnalyzeStructure.cs

@@ -4,7 +4,7 @@ using Newtonsoft.Json.Linq;
 namespace analyzeJSON
 {
     public record AnalysisResult<T>(T Nodes, T Leafs);
-    public record Token(string Name, JTokenType Type);
+    public record Token(string Name, JTokenType Type, bool IsLeaf = false);
 
     public class AnalyzeStructure
     {
@@ -21,27 +21,32 @@ namespace analyzeJSON
                 return;
 
             var tokenName = AnalyzeJSON.GetNameFromPath(token.Path);
-            var currentToken = new Token(tokenName, token.Type);
 
             if (token.HasValues)
             {
+                var nodeToken = new Token(tokenName, token.Type);
+
                 if (token.First.Equals(token.Last) &&
                     token.First.Type != JTokenType.Array &&
                     token.First.Type != JTokenType.Property &&
                     token.First.Type != JTokenType.Object)
                     return;
 
-                if (nodes.ContainsKey(currentToken))
-                    nodes[currentToken]++;
+                if (nodes.ContainsKey(nodeToken))
+                    nodes[nodeToken]++;
                 else
-                    nodes.Add(currentToken, 1);
+                    nodes.Add(nodeToken, 1);
             }
             else
             {
-                if (leafs.ContainsKey(currentToken))
-                    leafs[currentToken]++;
+                var leafToken = new Token(tokenName, token.Type, true);
+
+                if (leafs.ContainsKey(leafToken))
+                    leafs[leafToken]++;
                 else
-                    leafs.Add(currentToken, 1);
+                {
+                    leafs.Add(leafToken, 1);
+                }
             }
         }
 

+ 1 - 1
analyzeJSONTests/AnalyzeStructureIntegrationTests.cs

@@ -16,7 +16,7 @@ namespace analyzeJSONTests
 
             var result = analyze.Result;
             Assert.Equal(9, result.Nodes.Count);
-            Assert.Equal(9, result.Leafs.Count);
+            Assert.Equal(10, result.Leafs.Count);
         }
     }
 }

+ 6 - 3
analyzeJSONTests/testFiles/complex.json

@@ -121,17 +121,20 @@
     {
       "name": "Chest X-Ray",
       "time": "Today",
-      "location": "Main Hospital Radiology"
+      "location": "Main Hospital Radiology",
+      "imaging": "one"
     },
     {
       "name": "Chest X-Ray",
       "time": "Today",
-      "location": "Main Hospital Radiology"
+      "location": "Main Hospital Radiology",
+      "imaging": "two"
     },
     {
       "name": "Chest X-Ray",
       "time": "Today",
-      "location": "Main Hospital Radiology"
+      "location": "Main Hospital Radiology",
+      "imaging": "three"
     }
   ]
 }