aweXpect.Json
1.0.0-pre.3
Prefix Reserved
See the version list below for details.
dotnet add package aweXpect.Json --version 1.0.0-pre.3
NuGet\Install-Package aweXpect.Json -Version 1.0.0-pre.3
<PackageReference Include="aweXpect.Json" Version="1.0.0-pre.3" />
<PackageVersion Include="aweXpect.Json" Version="1.0.0-pre.3" />
<PackageReference Include="aweXpect.Json" />
paket add aweXpect.Json --version 1.0.0-pre.3
#r "nuget: aweXpect.Json, 1.0.0-pre.3"
#:package aweXpect.Json@1.0.0-pre.3
#addin nuget:?package=aweXpect.Json&version=1.0.0-pre.3&prerelease
#tool nuget:?package=aweXpect.Json&version=1.0.0-pre.3&prerelease
aweXpect.Json
Extensions on System.Text.Json for aweXpect.
Object serializable
You can verify that an object is JSON serializable:
MyObject subject = new();
await Expect.That(subject).IsJsonSerializable();
This will try to serialize and deserialize the provided object and check that they are equivalent.
You can provide both JSON serialization and equivalency options:
MyObject subject = new();
await Expect.That(subject).IsJsonSerializable(
new JsonSerializerOptions
{
IncludeFields = includeFields,
},
o => o.IgnoringMember("MyPropertyToIgnore"));
String comparison as JSON
You can compare two strings for JSON equivalency:
string subject = "{\"foo\":{\"bar\":[1,2,3]}}";
string expected = """
{
"foo": {
"bar": [ 1, 2, 3 ]
}
}
""";
await Expect.That(subject).Is(expected).AsJson();
Validation
You can verify, that a string is valid JSON.
string subject = "{\"foo\": 2}";
await Expect.That(subject).IsValidJson();
This verifies that the string can be parsed by
JsonDocument.Parse without
exceptions.
You can also specify the
JsonDocumentOptions:
string subject = "{\"foo\": 2}";
await Expect.That(subject).IsValidJson(o => o with {CommentHandling = JsonCommentHandling.Disallow});
You can also add additional expectations on the
JsonElement created when parsing the
subject:
string subject = "{\"foo\": 2}";
await Expect.That(subject).IsValidJson().Which(j => j.Matches(new{foo = 2}));
JsonElement
Match
You can verify, that the JsonElement matches an expected object:
JsonElement subject = JsonDocument.Parse("{\"foo\": 1, \"bar\": \"baz\"}").RootElement;
await Expect.That(subject).Matches(new{foo = 1});
await Expect.That(subject).MatchesExactly(new{foo = 1, bar = "baz"});
You can verify, that the JsonElement matches an expected array:
JsonElement subject = JsonDocument.Parse("[1,2,3]").RootElement;
await Expect.That(subject).Matches([1, 2]);
await Expect.That(subject).MatchesExactly([1, 2, 3]);
You can also verify, that the JsonElement matches a primitive type:
await Expect.That(JsonDocument.Parse("\"foo\"").RootElement).Matches("foo");
await Expect.That(JsonDocument.Parse("42.3").RootElement).Matches(42.3);
await Expect.That(JsonDocument.Parse("true").RootElement).Matches(true);
await Expect.That(JsonDocument.Parse("null").RootElement).Matches(null);
Be object
You can verify that a JsonElement is a JSON object that satisfy some expectations:
JsonElement subject = JsonDocument.Parse("{\"foo\": 1, \"bar\": \"baz\"}").RootElement;
await Expect.That(subject).IsObject(o => o
.With("foo").Matching(1).And
.With("bar").Matching("baz"));
You can also verify that a property is another object recursively:
JsonElement subject = JsonDocument.Parse("{\"foo\": {\"bar\": \"baz\"}}").RootElement;
await Expect.That(subject).IsObject(o => o
.With("foo").AnObject(i => i
.With("bar").Matching("baz")));
You can also verify that a property is an array:
JsonElement subject = JsonDocument.Parse("{\"foo\": [1, 2]}").RootElement;
await Expect.That(subject).IsObject(o => o
.With("foo").AnArray(a => a.WithElements(1, 2)));
You can also verify the number of properties in a JSON object:
JsonElement subject = JsonDocument.Parse("{\"foo\": 1, \"bar\": \"baz\"}").RootElement;
await Expect.That(subject).IsObject(o => o.With(2).Properties());
Be array
You can verify that a JsonElement is a JSON array that satisfy some expectations:
JsonElement subject = JsonDocument.Parse("[\"foo\",\"bar\"]").RootElement;
await Expect.That(subject).IsArray(a => a
.At(0).Matching("foo").And
.At(1).Matching("bar"));
You can also verify the number of elements in a JSON array:
JsonElement subject = JsonDocument.Parse("[1, 2, 3]").RootElement;
await Expect.That(subject).IsArray(o => o.With(3).Elements());
You can also directly match the expected values of an array:
JsonElement subject = JsonDocument.Parse("[\"foo\",\"bar\"]").RootElement;
await Expect.That(subject).IsArray(a => a
.WithElements("foo", "bar"));
You can also match sub-arrays recursively (add null to skip an element):
JsonElement subject = JsonDocument.Parse("[[0,1,2],[1,2,3],[2,3,4],[3,4,5,6]]").RootElement;
await Expect.That(subject).IsArray(a => a
.WithArrays(
i => i.WithElements(0,1,2),
i => i.At(0).Matching(1).And.At(2).Matching(3),
null,
i => i.With(4).Elements()
));
You can also match objects recursively (add null to skip an element):
JsonElement subject = JsonDocument.Parse(
"""
[
{"foo":1},
{"bar":2},
{"bar": null, "baz": true}
]
""").RootElement;
await Expect.That(subject).IsArray(a => a
.WithObjects(
i => i.With("foo").Matching(1),
null,
i => i.With(2).Properties()
));
JSON serializable
You can verify that an object is JSON serializable:
MyClass subject = new MyClass();
await Expect.That(subject).IsJsonSerializable();
This validates, that the MyClass can be serialized and deserialized to/from JSON and that the result is equivalent to
the original subject.
You can specify both, the
JsonSerializerOptions and the
equivalency options:
MyClass subject = new MyClass();
await Expect.That(subject).IsJsonSerializable(
new JsonSerializerOptions { IncludeFields = true },
e => e.IgnoringMember("Foo"));
You can also specify an expected generic type that the subject should have:
object subject = //...
await Expect.That(subject).IsJsonSerializable<MyClass>(
new JsonSerializerOptions { IncludeFields = true },
e => e.IgnoringMember("Foo"));
| 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 was computed. 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 (>= 2.0.0-pre.5)
- System.Text.Json (>= 9.0.2)
-
net8.0
- aweXpect.Core (>= 2.0.0-pre.5)
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 |
|---|---|---|
| 1.6.0 | 176 | 10/18/2025 |
| 1.5.0 | 312 | 9/18/2025 |
| 1.4.0 | 295 | 9/10/2025 |
| 1.3.0 | 324 | 6/30/2025 |
| 1.2.0 | 210 | 5/10/2025 |
| 1.1.0 | 352 | 4/1/2025 |
| 1.0.0 | 223 | 3/19/2025 |
| 1.0.0-pre.3 | 155 | 3/19/2025 |
| 1.0.0-pre.2 | 150 | 3/16/2025 |
| 1.0.0-pre.1 | 200 | 3/14/2025 |
| 0.6.0 | 178 | 3/1/2025 |
| 0.5.0 | 315 | 2/28/2025 |
| 0.4.0 | 258 | 2/24/2025 |
| 0.3.0 | 467 | 2/17/2025 |
| 0.2.0 | 141 | 2/16/2025 |
| 0.1.0 | 139 | 2/15/2025 |