Class JsonPointer
Represents a JSON Pointer as defined by RFC 6901
Inheritance
Implements
Inherited Members
Namespace: JsonCons.Utilities
Assembly: JsonCons.Utilities.dll
Syntax
public sealed class JsonPointer : IEnumerable<string>, IEnumerable, IEquatable<JsonPointer>
Examples
The following example shows how to get a value at a referenced location in a JSON document.
using System;
using System.Diagnostics;
using System.Text.Json;
using JsonCons.Utilities;
public class Example
{
public static void Main()
{
using var doc = JsonDocument.Parse(@"
[
{ ""category"": ""reference"",
""author"": ""Nigel Rees"",
""title"": ""Sayings of the Century"",
""price"": 8.95
},
{ ""category"": ""fiction"",
""author"": ""Evelyn Waugh"",
""title"": ""Sword of Honour"",
""price"": 12.99
}
]
");
var options = new JsonSerializerOptions() { WriteIndented = true };
JsonPointer pointer = JsonPointer.Parse("/1/author");
JsonElement element;
if (pointer.TryGetValue(doc.RootElement, out element))
{
Console.WriteLine($"{JsonSerializer.Serialize(element, options)}\n");
}
}
}
Output:
"Evelyn Waugh"
Constructors
| Improve this Doc View SourceJsonPointer()
Constructs a JSON Pointer to the root value of a JSON document
Declaration
public JsonPointer()
JsonPointer(IReadOnlyList<String>)
Constructs a JSON Pointer from a list of (unescaped) reference tokens
Declaration
public JsonPointer(IReadOnlyList<string> tokens)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IReadOnlyList<System.String> | tokens | A list of (unescaped) reference tokens. |
Properties
| Improve this Doc View SourceDefault
Gets a singleton instance of a JsonPointer to the root value of a JSON document.
Declaration
public static JsonPointer Default { get; }
Property Value
Type | Description |
---|---|
JsonPointer |
Tokens
Returns a list of (unescaped) reference tokens
Declaration
public IReadOnlyList<string> Tokens { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<System.String> |
Methods
| Improve this Doc View SourceAppend(JsonPointer, Int32)
Creates a new JsonPointer by appending an index token to the provided JsonPointer.
Declaration
public static JsonPointer Append(JsonPointer pointer, int token)
Parameters
Type | Name | Description |
---|---|---|
JsonPointer | pointer | The provided JsonPointer |
System.Int32 | token | An index token |
Returns
Type | Description |
---|---|
JsonPointer | A new JsonPointer |
Append(JsonPointer, String)
Creates a new JsonPointer by appending a name token to the provided JsonPointer.
Declaration
public static JsonPointer Append(JsonPointer pointer, string token)
Parameters
Type | Name | Description |
---|---|---|
JsonPointer | pointer | The provided JsonPointer |
System.String | token | A name token |
Returns
Type | Description |
---|---|
JsonPointer | A new JsonPointer |
ContainsValue(JsonElement)
Returns true
if the provided System.Text.Json.JsonElement contains a value at the referenced location.
Declaration
public bool ContainsValue(JsonElement root)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | root | The root System.Text.Json.JsonElement that is to be queried. |
Returns
Type | Description |
---|---|
System.Boolean |
|
ContainsValue(JsonElement, String)
Returns true
if the provided System.Text.Json.JsonElement contains a value at the referenced location.
Declaration
public static bool ContainsValue(JsonElement root, string pointer)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | root | The root System.Text.Json.JsonElement that is to be queried. |
System.String | pointer | The JSON string or URI Fragment representation of the JSON pointer. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The |
Equals(JsonPointer)
Determines whether two JSONPointer objects have the same value.
Declaration
public bool Equals(JsonPointer other)
Parameters
Type | Name | Description |
---|---|---|
JsonPointer | other |
Returns
Type | Description |
---|---|
System.Boolean |
|
Equals(Object)
Determines whether this instance and a specified object, which must also be a JSONPointer object, have the same value.
Declaration
public override bool Equals(object other)
Parameters
Type | Name | Description |
---|---|---|
System.Object | other |
Returns
Type | Description |
---|---|
System.Boolean |
Overrides
Escape(String)
Escapes a JSON Pointer token
Declaration
public static string Escape(string token)
Parameters
Type | Name | Description |
---|---|---|
System.String | token |
Returns
Type | Description |
---|---|
System.String |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The |
GetEnumerator()
Returns an enumerator that iterates through a list of reference tokens.
Declaration
public IEnumerator<string> GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerator<System.String> | An |
GetHashCode()
Returns the hash code for this JsonPointer
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | A 32-bit signed integer hash code. |
Overrides
Parse(String)
Parses a JSON Pointer represented as a string value or a
fragment identifier (starts with #
) into a JsonPointer.
Declaration
public static JsonPointer Parse(string input)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | A JSON Pointer represented as a string or a fragment identifier. |
Returns
Type | Description |
---|---|
JsonPointer | A JsonPointer. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The |
System.ArgumentException | The |
ToString()
Returns a JSON Pointer represented as a string value.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String | A JSON Pointer represented as a string value. |
Overrides
ToUriFragment()
Returns a string representing the JSON Pointer as a URI fragment identifier
Declaration
public string ToUriFragment()
Returns
Type | Description |
---|---|
System.String | A JSON Pointer represented as a fragment identifier. |
TryGetValue(JsonElement, String, out JsonElement)
Gets the value at the referenced location in the provided System.Text.Json.JsonElement.
Declaration
public static bool TryGetValue(JsonElement root, string pointer, out JsonElement value)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | root | The root System.Text.Json.JsonElement that is to be queried. |
System.String | pointer | The JSON string or URI Fragment representation of the JSON pointer. |
System.Text.Json.JsonElement | value | Contains the value at the referenced location, if found. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The |
TryGetValue(JsonElement, out JsonElement)
Gets the value at the referenced location in the provided System.Text.Json.JsonElement.
Declaration
public bool TryGetValue(JsonElement root, out JsonElement value)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | root | The root System.Text.Json.JsonElement that is to be queried. |
System.Text.Json.JsonElement | value | Contains the value at the referenced location, if found. |
Returns
Type | Description |
---|---|
System.Boolean |
|
TryParse(String, out JsonPointer)
Parses a JSON Pointer represented as a string value or a
fragment identifier (starts with #
) into a JsonPointer.
Declaration
public static bool TryParse(string input, out JsonPointer pointer)
Parameters
Type | Name | Description |
---|---|---|
System.String | input | A JSON Pointer represented as a string or a fragment identifier. |
JsonPointer | pointer | The JsonPointer. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The |
Explicit Interface Implementations
| Improve this Doc View SourceIEnumerable.GetEnumerator()
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IEnumerator |