From 7158145206d3f9182a1b4f7f587cb92715554f7c Mon Sep 17 00:00:00 2001 From: Brenton Farmer Date: Sat, 8 Jun 2024 12:21:33 -0700 Subject: [PATCH] Fixed merge formatting issue --- src/Hyperbee.Json/JsonPathBuilder.cs | 74 +++++++++++++--------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/src/Hyperbee.Json/JsonPathBuilder.cs b/src/Hyperbee.Json/JsonPathBuilder.cs index a97a6eab..08d5ac01 100644 --- a/src/Hyperbee.Json/JsonPathBuilder.cs +++ b/src/Hyperbee.Json/JsonPathBuilder.cs @@ -1,4 +1,4 @@ -using System.Text.Json; +using System.Text.Json; namespace Hyperbee.Json; @@ -17,14 +17,12 @@ public JsonPathBuilder( JsonElement rootElement ) { _rootElement = rootElement; - // avoid allocating full paths for every node by building a - // dictionary cache of (parentIdx, segment) pairs. + // we will avoid allocating full paths for every node by + // building a dictionary cache of (parentId, segment) pairs. _parentMap[GetIdx( _rootElement )] = (-1, "$"); // seed parent map with root } - // OPTIMIZED TO USE SEGMENT DICTIONARY CACHE - public string GetPath( JsonElement targetElement ) { // quick out @@ -76,7 +74,7 @@ public string GetPath( JsonElement targetElement ) } } - return null; // Target not found + return null; // target not found } private static int GetIdx( JsonElement element ) @@ -99,48 +97,42 @@ private static string BuildPath( int elementId, Dictionary( 4 ); + stack.Push( (_rootElement, "$") ); - public string GetPath( JsonElement targetElement ) - { - var stack = new Stack<(JsonElement element, string path)>( 4 ); - stack.Push( (_rootElement, "$") ); + while ( stack.Count > 0 ) + { + var (currentElement, currentPath) = stack.Pop(); - while ( stack.Count > 0 ) - { - var (currentElement, currentPath) = stack.Pop(); + if ( _comparer.Equals( currentElement, targetElement ) ) + return currentPath; - if ( _comparer.Equals( currentElement, targetElement ) ) - return currentPath; + switch ( currentElement.ValueKind ) + { + case JsonValueKind.Object: + foreach ( var property in currentElement.EnumerateObject() ) + { + var newPath = $"{currentPath}.{property.Name}"; + stack.Push( (property.Value, newPath) ); + } - switch ( currentElement.ValueKind ) + break; + + case JsonValueKind.Array: + var index = 0; + foreach ( var element in currentElement.EnumerateArray() ) { - case JsonValueKind.Object: - foreach ( var property in currentElement.EnumerateObject() ) - { - var newPath = $"{currentPath}.{property.Name}"; - stack.Push( (property.Value, newPath) ); - } - - break; - - case JsonValueKind.Array: - var index = 0; - foreach ( var element in currentElement.EnumerateArray() ) - { - var newPath = $"{currentPath}[{index++}]"; - stack.Push( (element, newPath) ); - } - - break; + var newPath = $"{currentPath}[{index++}]"; + stack.Push( (element, newPath) ); } - } - return null; // Target no + break; } - - return null; // Target no } - */ + + return null; + } +*/ }