JsonPath

Namespace: SnowBank.Data.Json · struct

Implements: IEnumerable<JsonPathSegment>, IEnumerable, IJsonSerializable, IJsonPackable, IJsonDeserializable<JsonPath>, IEquatable<JsonPath>, IEquatable<string>, ISpanFormattable, IFormattable, IEquatable<ReadOnlySpan<char>>, IEquatable<ReadOnlyMemory<char>>

Represents a path inside a JSON document to a nested child (ex: "id", "user.id"``"tags[2].id"

Constructors

JsonPath

JsonPath(ReadOnlyMemory<char> path)

JsonPath(string path)

Properties

Item

JsonPath Item { get; }

JsonPath Item { get; }

JsonPath Item { get; }

JsonPath Item { get; }

JsonPath Item { get; }

JsonPath Item { get; }

JsonPath Item { get; }

Methods

AsMemory

ReadOnlyMemory<char> AsMemory()

AsSpan

ReadOnlySpan<char> AsSpan()

Combine

static JsonPath Combine(JsonPath parent, JsonPath child)

Create

static JsonPath Create(ReadOnlyMemory<char> path)

Returns a JsonPath that wraps a ReadOnlySpan<char> literal

static JsonPath Create(ReadOnlySpan<char> path)

Returns a JsonPath that wraps a ReadOnlySpan<char> literal

static JsonPath Create(string path)

Returns a JsonPath that wraps a ReadOnlySpan<char> literal

static JsonPath Create(int index)

Returns a JsonPath that wraps an index

static JsonPath Create(Index index)

Returns a JsonPath that wraps an index

static JsonPath Create(in JsonPathSegment segment)

Returns a JsonPath that wraps a single path segment

static JsonPath Create(in JsonPathSegment segment0, in JsonPathSegment segment1)

static JsonPath Create(in JsonPathSegment segment0, in JsonPathSegment segment1, in JsonPathSegment segment2)

static JsonPath Create(in JsonPathSegment segment0, in JsonPathSegment segment1, in JsonPathSegment segment2, in JsonPathSegment segment3)

DecodeKeyName

static string DecodeKeyName(string key)

static ReadOnlySpan<char> DecodeKeyName(ReadOnlySpan<char> key)

EncodeKeyName

static string EncodeKeyName(string name)

static ReadOnlyMemory<char> EncodeKeyName(ReadOnlyMemory<char> name)

static ReadOnlySpan<char> EncodeKeyName(ReadOnlySpan<char> name)

EndsWith

bool EndsWith(ReadOnlySpan<char> suffix)

bool EndsWith(char suffix)

Equals

bool Equals(object obj)

bool Equals(JsonPath other)

bool Equals(string other)

bool Equals(ReadOnlySpan<char> other)

bool Equals(ReadOnlyMemory<char> other)

FromSegments

static JsonPath FromSegments(JsonPathSegment[] segments, bool reversed = false)

static JsonPath FromSegments(ReadOnlySpan<JsonPathSegment> segments, bool reversed = false)

static JsonPath FromSegments(IEnumerable<JsonPathSegment> segments, bool reversed = false)

GetCommonAncestor

JsonPath GetCommonAncestor(JsonPath other, out JsonPath left, out JsonPath right)

Returns the common ancestor of both paths, and both relative branches from this ancestor to both paths

GetEnumerator

SegmentTokenizer GetEnumerator()

GetHashCode

int GetHashCode()

GetLastIndex

Index? GetLastIndex()

Returns the last segment as an index, or null if it is a field access

"" => false
"foo" => null
"[42]" => 42
"[^1]" => ^1
"foo.bar" => null
"foo.bar[^1]" => ^1
"foo.bar[^1].baz" => null

GetLastKey

ReadOnlySpan<char> GetLastKey()

Returns the last segment as a key, unless it is an index

"" => ""
[42] => ""
"foo" => "foo"
"foo.bar" => "bar"
"foo.bar[42]" => ""
"foo.bar[42].baz" => "baz"

GetParent

JsonPath GetParent()

Returns the parent path

Returns the empty path if the path is already empty

"foo" => ""
"foo.bar" => "foo"
"foo.bar.baz" => "foo.bar"
"foo.bar[1]" => "foo.bar"
"foo.bar[1].baz" => "foo.bar[1]"

GetSegmentCount

int GetSegmentCount()

GetSegments

List<JsonPathSegment> GetSegments()

Returns the list of segments that compose this path

IsChildOf

bool IsChildOf(JsonPath parent)

Tests if this path is a child of another path

  • parent — Path to the parent

Returns: true if parent is a parent of the current path; otherwise, false.

A path is not its own child, so path.IsChildOf(path) will be .

"foo".IsChildOf("foo") => false
"foo.bar".IsChildOf("foo") => true
"foo[42]".IsChildOf("foo") => true
"[42].foo".IsChildOf("[42]") => true
"foo".IsChildOf("foo.bar") => false
"foo[42].bar".IsChildOf("foo[42]") => true
"foo[42][^1]".IsChildOf("foo[42]") => true

bool IsChildOf(ReadOnlySpan<char> parent)

Tests if this path is a child of another path

  • parent — Path to the parent

Returns: true if parent is a parent of the current path; otherwise, false.

A path is not its own child, so path.IsChildOf(path) will be .

"foo".IsChildOf("foo") => false
"foo.bar".IsChildOf("foo") => true
"foo[42]".IsChildOf("foo") => true
"[42].foo".IsChildOf("[42]") => true
"foo".IsChildOf("foo.bar") => false
"foo[42].bar".IsChildOf("foo[42]") => true
"foo[42][^1]".IsChildOf("foo[42]") => true

bool IsChildOf(JsonPath parent, out JsonPath relative)

Tests if this path is a child of another path

  • parent — Path to the parent
  • relative — If the method returns true, receives the relative path from parent to the current path

Returns: true if parent is a parent of the current path; otherwise, false.

A path is not its own child, so path.IsChildOf(path) will be .

"foo".IsChildOf("foo", out ...) => false, relative: ""
"foo.bar".IsChildOf("foo", out ...) => true, relative: "bar"
"foo[42]".IsChildOf("foo", out ...) => true, relative: "[42]
"[42].foo".IsChildOf("[42]", out ...) => true, relative: "foo"
"foo".IsChildOf("foo.bar", out ...) => false, relative: ""
"foo[42].bar".IsChildOf("foo[42]", out ...) => true, relative: "bar"
"foo[42][^1]".IsChildOf("foo[42]", out ...) => true, relative: "[^1]"

bool IsChildOf(ReadOnlySpan<char> parent, out JsonPath relative)

Tests if this path is a child of another path

  • parent — Path to the parent
  • relative — If the method returns true, receives the relative path from parent to the current path

Returns: true if parent is a parent of the current path; otherwise, false.

A path is not its own child, so path.IsChildOf(path) will be .

"foo".IsChildOf("foo", out ...) => false, relative: ""
"foo.bar".IsChildOf("foo", out ...) => true, relative: "bar"
"foo[42]".IsChildOf("foo", out ...) => true, relative: "[42]
"[42].foo".IsChildOf("[42]", out ...) => true, relative: "foo"
"foo".IsChildOf("foo.bar", out ...) => false, relative: ""
"foo[42].bar".IsChildOf("foo[42]", out ...) => true, relative: "bar"
"foo[42][^1]".IsChildOf("foo[42]", out ...) => true, relative: "[^1]"

IsEmpty

bool IsEmpty()

Tests if this the empty path (root of the document)

IsParentOf

bool IsParentOf(JsonPath child)

Tests if this path is a parent of another path

"foo".IsParentOf("foo") => false
"foo".IsParentOf("foo.bar") => true
"foo".IsParentOf("foo[42]") => true
"[42]".IsParentOf("[42].foo") => true
"foo.bar".IsParentOf("foo") => false
"foo[42]".IsParentOf("foo[42].bar") => true
"foo[42]".IsParentOf("foo[42][^1]") => true

bool IsParentOf(ReadOnlySpan<char> child)

Tests if this path is a parent of another path

"foo".IsParentOf("foo") => false
"foo".IsParentOf("foo.bar") => true
"foo".IsParentOf("foo[42]") => true
"[42]".IsParentOf("[42].foo") => true
"foo.bar".IsParentOf("foo") => false
"foo[42]".IsParentOf("foo[42].bar") => true
"foo[42]".IsParentOf("foo[42][^1]") => true

IsSibling

bool IsSibling(JsonPath sibling)

Tests if two paths are siblings (have the same parent)

ParseNext

static int ParseNext(ReadOnlySpan<char> path, out int keyLength, out Index index)

Slice

JsonPath Slice(int start)

Returns a subsection of this path, beginning at a specified position and continuing to its end.

  • start — Number of segments to skip

Returns: Slice of the path

JsonPath Slice(int start, int length)

Returns a subsection of this path, starting at start position for length segments.

  • start — Number of segments to skip
  • length — Number of segments to include

Returns: Slice of the path

StartsWith

bool StartsWith(ReadOnlySpan<char> prefix)

bool StartsWith(char prefix)

ThrowIfEmpty

static void ThrowIfEmpty(JsonPath path, string paramName = null)

Throws an exception if the specified path is empty

  • path — Path to check
  • paramName — Name of the parameter that contains the path

static void ThrowIfEmpty(string path, string paramName = null)

Throws an exception if the specified path is empty

  • path — Path to check
  • paramName — Name of the parameter that contains the path

static void ThrowIfEmpty(ReadOnlyMemory<char> path, string paramName = null)

Throws an exception if the specified path is empty

  • path — Path to check
  • paramName — Name of the parameter that contains the path

ToJson

JsonValue ToJson()

ToJsonValue

JsonValue ToJsonValue()

ToString

string ToString()

string ToString(string format, IFormatProvider formatProvider)

Tokenize

Tokenizable Tokenize()

TryDecodeKeyName

static bool TryDecodeKeyName(ReadOnlySpan<char> key, Span<char> buffer, out int written)

TryFormat

bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)

TryGetLastIndex

bool TryGetLastIndex(out Index index)

Tests if the last segment of this path is an index

Returns: true if the path ends with an array indexer (ex: "foo[1]" or "foo.bar[1]"), or false if the last segment is a key name (ex: "foo.bar" or "foo[1].bar"),

"" => false
"foo" => false
"[42]" => true, 42
"[^1]" => true, ^1
"foo.bar" => false
"foo.bar[^1]" => true, ^1
"foo.bar[^1].baz" => false

TryGetLastKey

bool TryGetLastKey(out ReadOnlySpan<char> key)

Tests if the last segment of this path is a key

Returns: true if the path ends with a key name (ex: "Foo.Bar" or "...[1].Bar"), or false if the last segment is a key indexer (ex: "...[1]"),

"" => false
[42] => false
"foo" => true, "foo"
"foo.bar" => true, "bar"
"foo.bar[42]" => false
"foo.bar[42].baz" => true, "baz"

TryGetSegments

bool TryGetSegments(Span<JsonPathSegment> buffer, out ReadOnlySpan<JsonPathSegment> segments)

Writes the segments that compose this path into the specified buffer, if it has enough capacity

  • buffer — Destination buffer (that must be large enough)
  • segments — Receives the span of the buffer with the successfully decoded segments

Returns: true if buffer was large enough; otherwise, false

TryGetSingleIndex

bool TryGetSingleIndex(out Index index)

Tests if this path consists of a single array index

Returns: true if the path is a single array index (ex: "[123]"), or false if it is a key name (ex: "foo") or has multiple segments ("foo[1]")

This can be used to speed up processing of selectors where the vast majority of cases is a single child

TryGetSingleKey

bool TryGetSingleKey(out ReadOnlyMemory<char> key)

Tests if this path consists of a single key name

Returns: true if the path is a single property name (ex: "Foo"), or false if it is an array index ("[1]") or has multiple segments ("Foo.Bar" or "Foo[1]")

This can be used to speed up processing of selectors where the vast majority of cases is a single child

WriteTo

static void WriteTo(StringBuilder sb, string key)

static void WriteTo(StringBuilder sb, ReadOnlyMemory<char> key)

static void WriteTo(StringBuilder sb, ReadOnlySpan<char> key)

static void WriteTo(StringBuilder sb, Index index)

static void WriteTo(StringBuilder sb, int index)

Fields

Empty

static readonly JsonPath Empty

The empty path (root of the document)

Value

readonly ReadOnlyMemory<char> Value

String literal