Skip to content

Commit

Permalink
Fixed merge formatting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bfarmer67 committed Jun 8, 2024
1 parent 8618020 commit 7158145
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions src/Hyperbee.Json/JsonPathBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json;
using System.Text.Json;

namespace Hyperbee.Json;

Expand All @@ -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
Expand Down Expand Up @@ -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 )
Expand All @@ -99,48 +97,42 @@ private static string BuildPath( int elementId, Dictionary<int, (int parentId, s
return string.Join( string.Empty, pathSegments );
}

/*
// NAIVE IMPLEMENTATION
public string GetPath( JsonElement targetElement )
{
var stack = new Stack<(JsonElement element, string path)>( 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;
}
*/

Check failure on line 137 in src/Hyperbee.Json/JsonPathBuilder.cs

View workflow job for this annotation

GitHub Actions / test

Invalid token '*' in class, record, struct, or interface member declaration

Check failure on line 137 in src/Hyperbee.Json/JsonPathBuilder.cs

View workflow job for this annotation

GitHub Actions / test

Invalid token '*' in class, record, struct, or interface member declaration
}

0 comments on commit 7158145

Please sign in to comment.