@@ -263,6 +263,10 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
263263 // A nested dictionary is used to achieve proper serialization.
264264 var nestedDictionary = new Dictionary < string , object > ( ) ;
265265 var trimmedPath = path . TrimEnd ( [ '\\ ' , '/' ] ) ;
266+
267+ // Move nested dictionary into final dictionary with the base path as a key.
268+ //var finalDictionary = new Dictionary<string, Dictionary<string, object>>();
269+ //finalDictionary.Add(trimmedPath, nestedDictionary);
266270
267271 // Sort the keys for consistent output
268272 string [ ] keys = [ .. protections . Keys ] ;
@@ -283,7 +287,7 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
283287 Array . Sort ( fileProtections ) ;
284288
285289 // Inserts key and protections into nested dictionary, with the key trimmed of the base path.
286- InsertNode ( nestedDictionary , key [ trimmedPath . Length ..] , fileProtections , modifyNodeList ) ;
290+ InsertNode ( nestedDictionary , key [ trimmedPath . Length ..] , trimmedPath , fileProtections , modifyNodeList ) ;
287291 }
288292
289293 // Adds the non-leaf-node protections back in
@@ -297,15 +301,9 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
297301
298302 modifyNodeList [ i ] . Item1 [ modifyNodeList [ i ] . Item2 ] = modifyNode ;
299303 }
300-
301- // Move nested dictionary into final dictionary with the base path as a key.
302- var finalDictionary = new Dictionary < string , Dictionary < string , object > > ( )
303- {
304- { trimmedPath , nestedDictionary }
305- } ;
306-
304+
307305 // Create the output data
308- serializedData = System . Text . Json . JsonSerializer . Serialize ( finalDictionary , jsonSerializerOptions ) ;
306+ serializedData = System . Text . Json . JsonSerializer . Serialize ( nestedDictionary , jsonSerializerOptions ) ;
309307 }
310308 else
311309 {
@@ -334,15 +332,26 @@ private void WriteProtectionResultJson(string path, Dictionary<string, List<stri
334332 /// <param name="protections">The scanned protection(s) for a given file</param>
335333 private static void InsertNode ( Dictionary < string , object > nestedDictionary ,
336334 string path ,
335+ string fullPath ,
337336 string [ ] protections ,
338337 List < ( Dictionary < string , object > , string , string [ ] ) > modifyNodeList )
339338 {
340- var current = nestedDictionary ;
341339 var pathParts = path . Split ( Path . DirectorySeparatorChar , StringSplitOptions . RemoveEmptyEntries ) ;
340+ if ( pathParts . Length <= 0 )
341+ {
342+ modifyNodeList . Add ( ( nestedDictionary , fullPath , protections ) ) ;
343+ return ;
344+ }
345+
346+ if ( ! nestedDictionary . ContainsKey ( fullPath ) )
347+ nestedDictionary [ fullPath ] = new Dictionary < string , object > ( ) ;
342348
349+ var current = ( Dictionary < string , object > ) nestedDictionary [ fullPath ] ;
350+
343351 // Traverses the nested dictionary until the "leaf" dictionary is reached.
344352 for ( int i = 0 ; i < pathParts . Length - 1 ; i ++ )
345353 {
354+
346355 var part = pathParts [ i ] ;
347356
348357 // Inserts new subdictionaries if one doesn't already exist
@@ -367,7 +376,28 @@ private static void InsertNode(Dictionary<string, object> nestedDictionary,
367376 }
368377
369378 // If the "leaf" dictionary has been reached, add the file and its protections.
370- current . Add ( pathParts [ ^ 1 ] , protections ) ;
379+ if ( current . ContainsKey ( pathParts [ ^ 1 ] ) )
380+ {
381+ var array1 = ( string [ ] ) current [ pathParts [ ^ 1 ] ] ;
382+ var array2 = protections ;
383+
384+ string [ ] result = new string [ array1 . Length + array2 . Length ] ;
385+ for ( int i = 0 ; i < array1 . Length ; i ++ )
386+ {
387+ result [ i ] = array1 [ i ] ;
388+ }
389+
390+ for ( int i = 0 ; i < array2 . Length ; i ++ )
391+ {
392+ result [ array1 . Length + i ] = array2 [ i ] ;
393+ }
394+
395+ current [ pathParts [ ^ 1 ] ] = result ;
396+ }
397+ else
398+ {
399+ current . Add ( pathParts [ ^ 1 ] , protections ) ;
400+ }
371401 }
372402#endif
373403 }
0 commit comments