From ac8dfb833c1613a983ba009091c16a5b8547839b Mon Sep 17 00:00:00 2001 From: HeroponRikiBestest <50224630+HeroponRikiBestest@users.noreply.github.com> Date: Mon, 10 Nov 2025 12:45:56 -0500 Subject: [PATCH] Fixes last minute issue with json parsing (#392) * fix * add comment back * slight simplification * Remove newline * Remove newline * Fix --- ProtectionScan/Features/MainFeature.cs | 52 ++++++++++++++++++++------ 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/ProtectionScan/Features/MainFeature.cs b/ProtectionScan/Features/MainFeature.cs index 263f5bbc..61edd476 100644 --- a/ProtectionScan/Features/MainFeature.cs +++ b/ProtectionScan/Features/MainFeature.cs @@ -263,6 +263,10 @@ namespace ProtectionScan.Features // A nested dictionary is used to achieve proper serialization. var nestedDictionary = new Dictionary(); var trimmedPath = path.TrimEnd(['\\', '/']); + + // Move nested dictionary into final dictionary with the base path as a key. + //var finalDictionary = new Dictionary>(); + //finalDictionary.Add(trimmedPath, nestedDictionary); // Sort the keys for consistent output string[] keys = [.. protections.Keys]; @@ -283,7 +287,7 @@ namespace ProtectionScan.Features Array.Sort(fileProtections); // Inserts key and protections into nested dictionary, with the key trimmed of the base path. - InsertNode(nestedDictionary, key[trimmedPath.Length..], fileProtections, modifyNodeList); + InsertNode(nestedDictionary, key[trimmedPath.Length..], trimmedPath, fileProtections, modifyNodeList); } // Adds the non-leaf-node protections back in @@ -297,15 +301,9 @@ namespace ProtectionScan.Features modifyNodeList[i].Item1[modifyNodeList[i].Item2] = modifyNode; } - - // Move nested dictionary into final dictionary with the base path as a key. - var finalDictionary = new Dictionary>() - { - {trimmedPath, nestedDictionary} - }; - + // Create the output data - serializedData = System.Text.Json.JsonSerializer.Serialize(finalDictionary, jsonSerializerOptions); + serializedData = System.Text.Json.JsonSerializer.Serialize(nestedDictionary, jsonSerializerOptions); } else { @@ -334,15 +332,26 @@ namespace ProtectionScan.Features /// The scanned protection(s) for a given file private static void InsertNode(Dictionary nestedDictionary, string path, + string fullPath, string[] protections, List<(Dictionary, string, string[])> modifyNodeList) { - var current = nestedDictionary; var pathParts = path.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); + if (pathParts.Length <= 0) + { + modifyNodeList.Add((nestedDictionary, fullPath, protections)); + return; + } + + if (!nestedDictionary.ContainsKey(fullPath)) + nestedDictionary[fullPath] = new Dictionary(); + var current = (Dictionary)nestedDictionary[fullPath]; + // Traverses the nested dictionary until the "leaf" dictionary is reached. for (int i = 0; i < pathParts.Length - 1; i++) { + var part = pathParts[i]; // Inserts new subdictionaries if one doesn't already exist @@ -367,7 +376,28 @@ namespace ProtectionScan.Features } // If the "leaf" dictionary has been reached, add the file and its protections. - current.Add(pathParts[^1], protections); + if (current.ContainsKey(pathParts[^1])) + { + var array1 = (string[])current[pathParts[^1]]; + var array2 = protections; + + string[] result = new string[array1.Length + array2.Length]; + for (int i = 0; i < array1.Length; i++) + { + result[i] = array1[i]; + } + + for (int i = 0; i < array2.Length; i++) + { + result[array1.Length + i] = array2[i]; + } + + current[pathParts[^1]] = result; + } + else + { + current.Add(pathParts[^1], protections); + } } #endif }