aweXpect.Json 0.3.0

Prefix Reserved
dotnet add package aweXpect.Json --version 0.3.0                
NuGet\Install-Package aweXpect.Json -Version 0.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="aweXpect.Json" Version="0.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add aweXpect.Json --version 0.3.0                
#r "nuget: aweXpect.Json, 0.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install aweXpect.Json as a Cake Addin
#addin nuget:?package=aweXpect.Json&version=0.3.0

// Install aweXpect.Json as a Cake Tool
#tool nuget:?package=aweXpect.Json&version=0.3.0                

aweXpect.Json

Changelog

Nuget Coverage

Extensions on System.Text.Json for aweXpect.

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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.3.0 267 2/17/2025
0.2.0 72 2/16/2025
0.1.0 67 2/15/2025