aweXpect.Json
4.2.0
Prefix Reserved
dotnet add package aweXpect.Json --version 4.2.0
NuGet\Install-Package aweXpect.Json -Version 4.2.0
<PackageReference Include="aweXpect.Json" Version="4.2.0" />
<PackageVersion Include="aweXpect.Json" Version="4.2.0" />
<PackageReference Include="aweXpect.Json" />
paket add aweXpect.Json --version 4.2.0
#r "nuget: aweXpect.Json, 4.2.0"
#:package aweXpect.Json@4.2.0
#addin nuget:?package=aweXpect.Json&version=4.2.0
#tool nuget:?package=aweXpect.Json&version=4.2.0
aweXpect.Json
aweXpect.Json extends aweXpect with expectations for JSON strings, JsonElement, JsonNode and the JSON serialization of objects with System.Text.Json.
Installation
dotnet add package aweXpect.Json
The package brings Expect.That with it, but not the expectations for strings, numbers or collections, so reference
aweXpect as well, e.g. for the IsEqualTo of IsEqualTo(…).AsJson().
The expectations live in the namespace aweXpect, so using aweXpect; is all you need; only the
customization lives in aweXpect.Json.
The examples write That(...) instead of Expect.That(...), which needs using static aweXpect.Expect; (see Getting started).
At a glance
string json = """{"title": "Let It Be", "artist": "The Beatles", "durationSeconds": 243}""";
// compare JSON strings by their structure instead of their formatting
string expected = """{ "artist": "The Beatles", "title": "Let It Be", "durationSeconds": 243 }""";
await That(json).IsEqualTo(expected).AsJson();
// verify that a string is valid JSON that matches an expected value
await That(json).IsValidJsonMatching(new { title = "Let It Be" });
// verify single values of a JsonElement or JsonNode
JsonElement track = JsonDocument.Parse(json).RootElement;
await That(track)
.HasProperty("title", t => t.IsString("Let It Be")).And
.HasProperty("durationSeconds", d => d.IsNumber<int>()).And
.DoesNotHaveProperty("password");
// verify the JSON contract of a type, e.g. `record Track(string Title, string Artist)`
await That(new Track("Let It Be", "The Beatles")).IsJsonSerializedAs(
"""{"Title": "Let It Be", "Artist": "The Beatles"}""");
A failing comparison names every difference by its JSON path. If the title was "Yesterday", IsValidJsonMatching
would fail with:
Expected that json
is valid JSON which matches new { title = "Let It Be" },
but it differed as $.title was "Yesterday" instead of "Let It Be"
JSON options:
- ignore additional properties
Overview
Expectations for string:
| Expectation | Negated | Summary |
|---|---|---|
IsEqualTo(…).AsJson() |
IsNotEqualTo(…).AsJson() |
equal to the expected JSON, compared by structure |
IsValidJson |
IsNotValidJson |
can be parsed as JSON, optionally with expectations on its root |
IsValidJsonMatching |
valid JSON that matches the expected value | |
IsValidJsonMatchingExactly |
valid JSON that matches the expected value exactly |
Expectations for JsonElement and JsonNode:
| Expectation | Negated | Summary |
|---|---|---|
Matches |
matches the expected value, additional properties allowed | |
MatchesExactly |
matches the expected value exactly | |
HasProperty |
DoesNotHaveProperty |
a JSON object with the property, optionally with expectations |
HasValueAt |
DoesNotHaveValueAt |
a value at a path, optionally with expectations |
HasElementAt |
a JSON array with an element at the index, optionally with expectations | |
IsObject |
a JSON object | |
IsObject().WhoseProperties |
collection expectations on the properties of a JSON object | |
IsArray |
a JSON array | |
IsArray().WhoseElements |
collection expectations on the elements of a JSON array | |
IsString |
a JSON string, optionally equal to a value or with expectations | |
IsNumber |
a JSON number, optionally with expectations on its value | |
IsString().StartingWith(…), … |
a JSON string compared with a string operator, e.g. EqualTo, EndingWith or Containing(…).AtLeast(2) |
|
IsNumber().GreaterThan(…), … |
a JSON number compared as double with a number operator, e.g. Between(…).And(…) or EqualTo(…).Within(…) |
|
IsNumber<TNumber> |
a JSON number that fits into the number type, e.g. an int, optionally equal to a value |
|
IsBoolean |
a JSON boolean, i.e. true or false, optionally equal to a value or with expectations |
|
IsNull |
the JSON value null, only for a JsonElement |
Expectations for any object, serialized with System.Text.Json:
| Expectation | Negated | Summary |
|---|---|---|
IsJsonSerializable |
survives a round trip through the JSON serializer | |
IsJsonSerializedAs |
serialized by the JSON serializer as the expected JSON |
The comparisons can be relaxed with options like TreatingNullAsAbsent(), IgnoringArrayOrder(),
IgnoringMember(name) or ComparingNumbersWithin(tolerance), see
Matching JSON.
Customization
Set a value under Customize.aweXpect.Json() for the current
async flow, or under
Customize.aweXpect.Global.Json() for
all tests.
| Value | Default | Summary |
|---|---|---|
DefaultJsonDocumentOptions |
AllowTrailingCommas = true |
the options to parse JSON strings |
DefaultJsonSerializerOptions |
AllowTrailingCommas = true |
the options to format and serialize JSON values |
Documentation
The full documentation is available at docs.testably.org:
- JSON strings: comparing strings as JSON and validating them
- Matching JSON:
MatchesandMatchesExactly, and the options to relax the comparison - Objects, arrays and value kinds:
HasProperty,HasValueAt,HasElementAt,IsObject,WhoseProperties,IsArray,WhoseElementsand the value kinds JsonNode: the same expectations for aJsonNodeand its subtypes- Serialization:
IsJsonSerializableandIsJsonSerializedAs - Customization: the default
JsonDocumentOptionsandJsonSerializerOptions
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- aweXpect.Core (>= 4.2.0)
- System.Text.Json (>= 10.0.14)
-
net10.0
- aweXpect.Core (>= 4.2.0)
-
net8.0
- aweXpect.Core (>= 4.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.2.0 | 7 | 11/15/2026 |
| 4.1.0 | 51 | 11/13/2026 |
| 4.0.0 | 64 | 11/10/2026 |
| 4.0.0-pre.4 | 60 | 11/6/2026 |
| 4.0.0-pre.3 | 70 | 10/29/2026 |
| 4.0.0-pre.2 | 69 | 10/27/2026 |
| 4.0.0-pre.1 | 69 | 10/22/2026 |
| 2.1.0 | 665 | 10/16/2026 |
| 2.0.0 | 540 | 10/10/2026 |
| 2.0.0-pre.2 | 284 | 10/8/2026 |
| 2.0.0-pre.1 | 134 | 9/28/2026 |
| 1.8.0 | 390 | 9/5/2026 |
| 1.7.0 | 319 | 5/10/2026 |
| 1.6.0 | 704 | 10/18/2025 |
| 1.5.0 | 393 | 9/18/2025 |
| 1.4.0 | 385 | 9/10/2025 |
| 1.3.0 | 394 | 6/30/2025 |
| 1.2.0 | 267 | 5/10/2025 |
| 1.1.0 | 435 | 4/1/2025 |
| 1.0.0 | 297 | 3/19/2025 |