Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bfarmer67 committed Nov 25, 2024
1 parent 5beb8d4 commit d598e20
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ choice for pinpointing exact values. Hyperbee.Json's JsonPointer implementation
## JSONPatch

JSONPatch is a format for describing changes to a JSON document. It allows you to apply partial modifications to JSON data efficiently.
Hyperbee.Json supports JsonPatch as defined in [RFC 6902](https://www.rfc-editor.org/rfc/rfc6902.html), ensuring compatibility and reliability.
Hyperbee.Json supports JsonPatch as defined in [RFC 6902](https://www.rfc-editor.org/rfc/rfc6902.html).

[Read more about JsonPatch](https://stillpoint-software.github.io/hyperbee.json/jsonpatch.html)

Expand Down
44 changes: 22 additions & 22 deletions docs/additional-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,59 @@ title: Additional Classes
nav_order: 99
---

## Additional Classes
# Additional Classes

In addition to JSONPath, a few additional classes are provided to support pointer-style
property diving, element comparisons, and dynamic property access.

### JsonNodeFactory
## JsonNodeFactory

The JsonNodeFactory efficiently converts a `JsonElement` to a `JsonNode` with minimal memory allocation. This
method performs substantially better than the common serialize deserialize, or parse raw string techniques.

| Class | Description
|:---------------------------|:-----------
| `JsonNodeFactory` | Efficiently convert a `JsonElement` to a `JsonNode`
| Class | Description
|:----------------------------------|:-----------
| `JsonNodeFactory` | Efficiently convert a `JsonElement` to a `JsonNode`


### JsonPath Pointer
## JsonPath Pointer

JsonPathPointer acts **similarly** to JSON Pointer; it expects an absolute path that returns a single element.
Unlike JSON Pointer, property diving notation expects normalized JSON Path notation.

| Class | Description
|:-----------------------------------|:-----------
| `JsonPathPointer<JsonElement>` | Dives for properties using absolute locations like `$.store.book[2].author`
| Class | Description
|:----------------------------------|:-----------
| `JsonPathPointer<JsonElement>` | Dives for properties using absolute locations like `$.store.book[2].author`

The syntax supports absolute (normalized) paths; dotted notation, quoted names, and simple bracketed array accessors only.
The intention is to return a single element by literal path.

Json path style wildcard '*', '..', and '[a,b]' multi-result selector notations and filters are **not** supported.

### JsonElement Path
## JsonElement Path

Unlike `JsonNode`, `JsonElement` does not have a `Path` property. `JsonPathBuilder` will find the path
for a given `JsonElement`.

| Class | Description
|:---------------------------|:-----------
| `JsonPathBuilder` | Returns the JsonPath location string for a given element
| Class | Description
|:----------------------------------|:-----------
| `JsonPathBuilder` | Returns the JsonPath location string for a given element

### Equality Helpers
## Equality Helpers

| Method | Description
|:-----------------------------------|:-----------
| `JsonElement.DeepEquals` | Performs a deep equals comparison on two `JsonElements`
| `JsonElementDeepEqualityComparer` | A deep equals equality comparer that compares two `JsonElements`
| Method | Description
|:----------------------------------|:-----------
| `JsonElement.DeepEquals` | Performs a deep equals comparison on two `JsonElements`
| `JsonElementDeepEqualityComparer` | A deep equals equality comparer that compares two `JsonElements`

### Dynamic Object Serialization
## Dynamic Object Serialization

Basic support is provided for serializing to and from dynamic objects through the use of a custom `JsonConverter`.
The `DynamicJsonConverter` class is useful for simple scenareos. It is intended as a simple helper for
basic use cases only. A helper methods `JsonHelper.ConvertToDynamic` is provided to simplify the process of
serializing and deserializing dynamic objects.

#### Example: ConvertToDynamic
### Example: Convert To Dynamic

```csharp
var root = JsonDocument.Parse(jsonInput); // jsonInput contains the bookstore example
Expand All @@ -70,7 +70,7 @@ Assert.IsTrue( price == 8.95 );
Assert.IsTrue( author == "Nigel Rees" );
```

#### Example: Serialize To Dynamic
### Example: Serialize To Dynamic

```csharp
var serializerOptions = new JsonSerializerOptions
Expand All @@ -88,7 +88,7 @@ var jsonOutput = JsonSerializer.Serialize<dynamic>( jobject, serializerOptions )
Assert.IsTrue( jsonInput == jsonOutput );
```

##### Enum handling
#### Enum handling

When deserializing, the converter will treat enumerations as strings. You can override this behavior by setting
the `TryReadValueHandler` on the converter. This handler will allow you to intercept and convert string and
Expand Down
10 changes: 5 additions & 5 deletions docs/jsonpath/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ JSONPath operates on JSON documents:
### JSONPath Syntax

| JSONPath | Description
|:---------------------------------------------|:-----------------------------------------------------------
|:---------------------------------------------|:-------------------------------------------
| `$` | Root JSON node
| `@` | Current JSON node
| `.<name>`, `.'<name>'`, or `."<name>"` | Object member dot operator
Expand All @@ -38,10 +38,10 @@ JSONPath operates on JSON documents:

The library extends the JSONPath expression syntax to support additional features.

| Operators | Description | Example
|---------------------|-----------------------------------------------|------------------------------------------------
| `+` `-` `*` `\` `%` | Basic math operators. | `$[?(@.a + @.b == 3)]`
| `in` | Tests is a value is in a set. | `$[[email protected] in ['a', 'b', 'c'] ]`
| Operators | Description | Example
|---------------------|---------------------------------|----------------------------------
| `+` `-` `*` `\` `%` | Basic math operators. | `$[?(@.a + @.b == 3)]`
| `in` | Tests is a value is in a set. | `$[[email protected] in ['a', 'b', 'c'] ]`


## JSONPath Operators
Expand Down
3 changes: 1 addition & 2 deletions src/Hyperbee.Json/Hyperbee.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>true</IsPackable>

Expand All @@ -9,7 +9,6 @@
<PackageTags>json-path;jsonpath;json-pointer;jsonpointer;json-patch;jsonpatch;query;path;patch;diff;json;rfc9535;rfc6901;rfc6902</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<PackageProjectUrl>https://stillpoint-software.github.io/hyperbee.json/</PackageProjectUrl>
<TargetFrameworks>net9.0</TargetFrameworks>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Copyright>Stillpoint Software, Inc.</Copyright>
<Title>Hyperbee Json</Title>
Expand Down
6 changes: 3 additions & 3 deletions src/Hyperbee.Json/Patch/JsonDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static void ProcessArrayDiff( DiffOperation operation, Stack<DiffOperati

try
{
CalculateLevenshteinMatrix( matrix, source, target );
CalculateLevenshteinMatrix( ref matrix, source, target );

while ( row > 0 || col > 0 )
{
Expand Down Expand Up @@ -177,7 +177,7 @@ private static void ProcessArrayDiff( DiffOperation operation, Stack<DiffOperati
else
{
// We already check if these were values when calculating the matrix,
// so we know this are objects or arrays, and we need further processing.
// so we know this is an object or array, and we need further processing.
stack.Push( new DiffOperation( source[row - 1], target[col - 1], path ) );
}

Expand All @@ -201,7 +201,7 @@ private static void ProcessValueDiff( DiffOperation operation, List<PatchOperati
operations.Add( new PatchOperation { Operation = PatchOperationType.Replace, Path = operation.Path, Value = operation.Target } );
}

private static void CalculateLevenshteinMatrix( Matrix matrix, TNode[] source, TNode[] target )
private static void CalculateLevenshteinMatrix( ref Matrix matrix, TNode[] source, TNode[] target )
{
var accessor = Descriptor.ValueAccessor;

Expand Down
9 changes: 6 additions & 3 deletions src/Hyperbee.Json/Patch/JsonPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,12 @@ private static JsonNode PatchValue( PatchOperation patch )
if ( patch.Value is null )
throw new JsonPatchException( "The 'value' property was missing." );

return (patch.Value is JsonNode node)
? (node.Parent != null ? node.DeepClone() : node)
: JsonValue.Create( patch.Value );
return patch.Value switch
{
JsonNode node when node.Parent == null => node,
JsonNode node => node.DeepClone(),
_ => JsonValue.Create( patch.Value )
};
}

public IEnumerator<PatchOperation> GetEnumerator()
Expand Down
8 changes: 4 additions & 4 deletions src/Hyperbee.Json/Path/JsonPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ private static IEnumerable<TNode> EnumerateMatches( TNode root, NodeArgs args, N

var (parent, value, key, segmentNext, flags) = args;

// call node processor if it exists and the `key` is not null.
// the key is null when a descent has re-pushed the descent target.
// this should be safe to skip; we will see its values later.
ProcessArgs:
// call node processor if it exists and the `key` is not null.
// the key is null when a descent has re-pushed the descent target.
// this should be safe to skip; we will see its values later.

ProcessArgs:
if ( key != null )
processor?.Invoke( parent, value, key, segmentNext );

Expand Down
2 changes: 1 addition & 1 deletion test/Hyperbee.Json.Cts/Hyperbee.Json.Cts.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFrameworks>net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
2 changes: 1 addition & 1 deletion test/Hyperbee.Json.Tests/Hyperbee.Json.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<RootNamespace>Hyperbee.Json.Tests</RootNamespace>
</PropertyGroup>
Expand Down

0 comments on commit d598e20

Please sign in to comment.