Show / Hide Table of Contents

Class JsonPointer

Represents a JSON Pointer as defined by RFC 6901

Inheritance
System.Object
JsonPointer
Implements
System.Collections.Generic.IEnumerable<System.String>
System.Collections.IEnumerable
System.IEquatable<JsonPointer>
Inherited Members
System.Object.Equals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
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 Source

JsonPointer()

Constructs a JSON Pointer to the root value of a JSON document

Declaration
public JsonPointer()
| Improve this Doc View Source

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 Source

Default

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
| Improve this Doc View Source

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 Source

Append(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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

true if the provided System.Text.Json.JsonElement contains a value at the referenced location, otherwise false.

| Improve this Doc View Source

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

true if the provided System.Text.Json.JsonElement contains a value at the referenced location, otherwise false.

Exceptions
Type Condition
System.ArgumentNullException

The pointer is null.

| Improve this Doc View Source

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

true if other is a JsonPointer and has exactly the same reference tokens as this instance; otherwise, false. If other is null, the method returns false.

| Improve this Doc View Source

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
System.Object.Equals(System.Object)
| Improve this Doc View Source

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 token is null.

| Improve this Doc View Source

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 IEnumerator<string> for a list of reference tokens.

| Improve this Doc View Source

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
System.Object.GetHashCode()
| Improve this Doc View Source

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 input is null.

System.ArgumentException

The input is invalid.

| Improve this Doc View Source

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
System.Object.ToString()
| Improve this Doc View Source

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.

| Improve this Doc View Source

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

true if the value was found at the referenced location, otherwise false.

Exceptions
Type Condition
System.ArgumentNullException

The pointer is null.

| Improve this Doc View Source

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

true if the value was found at the referenced location, otherwise false.

| Improve this Doc View Source

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

true if the input string can be parsed into a list of reference tokens, false otherwise.

Exceptions
Type Condition
System.ArgumentNullException

The input is null.

Explicit Interface Implementations

| Improve this Doc View Source

IEnumerable.GetEnumerator()

Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type Description
System.Collections.IEnumerator

Implements

System.Collections.Generic.IEnumerable<T>
System.Collections.IEnumerable
System.IEquatable<T>
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX