Class JsonPath
- Namespace
- JsonAssertions
- Assembly
- JsonAssertions.TUnit.dll
Framework-agnostic JSON path navigation primitives. This JsonAssertions namespace
holds the matching core; the TUnit-specific fluent entry points live in the sibling
JsonAssertions.TUnit namespace. Both ship in the single JsonAssertions.TUnit
package: the two-namespace split keeps the same consumer feel as the rest of the assertion
family (a framework-agnostic core plus a TUnit adapter) and future-proofs a package split
if the bare JsonAssertions identifier ever becomes available.
public static class JsonPath
- Inheritance
-
JsonPath
- Inherited Members
Remarks
The path grammar is a small subset of JSONPath. Segments are either dot-separated property names or zero-based bracket indices, both composable in the same path:
user.address.city— nested property navigation.items[0]— array element access.objects[0].entries[0].id— mixed nesting.$— the root element itself;$.user.nameequivalent touser.name;$[0]equivalent to[0]against a root array.
Methods
ContainsWildcard(string)
Reports whether path contains a wildcard array segment
[], which matches every element of an array rather than a single index. Wildcard
paths are resolved with ResolveAll(JsonElement, string); Resolve(JsonElement, string) rejects []
as a malformed (non-numeric) index.
public static bool ContainsWildcard(string path)
Parameters
pathstringThe path to inspect.
Returns
Exceptions
- ArgumentNullException
pathis null.
Exists(JsonElement, string)
Reports whether a value exists at the given path within
element. Equivalent to Resolve(element, path).Found.
public static bool Exists(JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA path of dot-separated property names and zero-based bracket indices; see Resolve(JsonElement, string).
Returns
- bool
true if every segment resolves; false if any segment is missing, out of range, or applied to a value of the wrong kind.
Exceptions
- ArgumentException
pathis invalid; see Resolve(JsonElement, string).
Resolve(JsonElement, string)
Resolves path against element, returning a
JsonPathResolution that carries the resolved element on success and the
failure-point context (how far it got, which segment failed, what kind of value blocked
it) on failure.
public static JsonPathResolution Resolve(JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA path of dot-separated property names and zero-based bracket indices, for example
items[0].name. A leading\(.</code> or bare <code>\)is accepted as the JSONPath root reference;$alone resolves toelementitself.
Returns
- JsonPathResolution
A JsonPathResolution describing the outcome.
Exceptions
- ArgumentException
pathis null, empty, whitespace, contains an empty property segment (a leading, trailing, or doubled dot), or contains a malformed index segment (an empty[], a non-numeric or negative index, or an unclosed[).
ResolveAll(JsonElement, string)
Resolves path against element, expanding each
[] wildcard across every element of the array it targets, and returns one
JsonPathResolution per expanded concrete path. A path with no wildcard yields
exactly one resolution (identical to Resolve(JsonElement, string)). A [] over an empty
array yields zero resolutions: a "for all" over an empty set, so an all-must-resolve check
passes vacuously (pair with a non-empty-array assertion when emptiness must fail). A
[*] applied to a non-array yields a single failed resolution describing the mismatch.
Each failed resolution carries the concrete prefix (e.g. [1]) so a wildcard failure
names which element failed.
public static IReadOnlyList<JsonPathResolution> ResolveAll(JsonElement element, string path)
Parameters
elementJsonElementThe JSON element to navigate from.
pathstringA path as in Resolve(JsonElement, string), optionally containing one or more
[*]wildcard segments.
Returns
- IReadOnlyList<JsonPathResolution>
The resolutions for every concrete path the wildcards expand to, in document order.
Exceptions
- ArgumentException
pathis invalid; see Resolve(JsonElement, string).