FluentAssertions.Extension.Json 1.1.2

dotnet add package FluentAssertions.Extension.Json --version 1.1.2                
NuGet\Install-Package FluentAssertions.Extension.Json -Version 1.1.2                
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="FluentAssertions.Extension.Json" Version="1.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentAssertions.Extension.Json --version 1.1.2                
#r "nuget: FluentAssertions.Extension.Json, 1.1.2"                
#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 FluentAssertions.Extension.Json as a Cake Addin
#addin nuget:?package=FluentAssertions.Extension.Json&version=1.1.2

// Install FluentAssertions.Extension.Json as a Cake Tool
#tool nuget:?package=FluentAssertions.Extension.Json&version=1.1.2                

FluentAssertions.Extension.Json

The library is an extensions for validating a json documents in Fluent assertions

Unlike FluentAssertions.Json it is based on System.Text.Json instead of Newtonsoft.Json.

The syntax of the assertions is self-explaining.

Please check the example below for details:

Test Json file

{
    "a" : { "a1" : "text" },
    "b" : true,
    "c" : false,
    "d" : 1,
    "e" : 3.1415,
    "f" : "string",
    "i" : [ 1, 2, 3 ],
    "j" : null
}

The assertions

            //validate that the text is a correct json
            jsonText.Should().BeCorrectJson();

            //parse json using AsJson extension
            var json = jsonText.AsJson();

            //HaveProperty extension for Json Element
            json.HasProperty("a").Should().BeTrue();

            //validate that json has a property
            json.Should()
                .HaveProperty("a");

            //validate that json has no property
            json.Should()
                .HaveNoProperty("x");

            //validate that json has a property and the property is an object and has a text property
            //using which property
            json.Should()
                .HaveProperty("a")
                .Which.Should()
                    .NotBeNull()
                    .And
                    .BeObject()
                    .And
                    .HaveProperty("a1")
                        .Which.Should()
                        .Be("text");

            //or do the same without chaining
            json.Should()
                .HaveProperty("a");
            json.GetProperty("a").Should()
                    .BeObject()
                    .And
                    .HaveProperty("a1");

            json.GetProperty("a").GetProperty("a1").Should()
                    .Be("text");

            //validate boolean properties
            json.Should()
                .HaveProperty("b")
                .Which.Should()
                    .BeValue()
                    .And
                    .BeTrue()
                    .And
                    .Be(true);

            json.Should()
                .HaveProperty("c")
                .Which.Should()
                    .BeFalse()
                    .And
                    .Be(false);

            //check numbers
            json.Should()
                .HaveProperty("d")
                .Which.Should()
                    .Be(1)
                    .And
                    .BeIntegerMatching(x => x < 2);
;

            json.Should()
                .HaveProperty("e")
                .Which.Should()
                    .Be(3.1415)
                    .And
                    .Be(3.14, 0.005)
                    .BeNumberMatching(x => x > 3.0);

            //check string for equality
            json.Should()
                .HaveProperty("f")
                .Which.Should()
                    .Be("string")
                    .And
                    .Be("STRING", StringComparison.OrdinalIgnoreCase)
                    .And
                    .BeStringMatching(s => s.StartsWith("s"));

            //check string for matching a regular expression
            json.Should()
                .HaveProperty("f")
                .Which.Should()
                    .Match("^.tr.{3}$");

            //check three properties at a time
            json.Should()
                .HaveIntegerProperty("d", x => x == 1)
                .And
                .HaveNumberProperty("e", x => x > 3)
                .And
                .HaveStringProperty("f", s => s == "string");

            //check array
            json.Should()
                .HaveProperty("i")
                .Which.Should()
                .BeArray()
                .And
                .NotBeEmpty()
                .And
                .HaveLength(3)
                .And
                .HaveLengthAtLeast(1);

            json.GetProperty("i")[0].Should()
                .BeValue()
                .And
                .Be(1);

            json.Should()
                .HaveProperty("j")
                .Which.Should()
                    .BeNull();
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 was computed.  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. 
.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
1.1.2 36,289 8/3/2022