PosInformatique.FluentAssertions.Json 1.0.5

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package PosInformatique.FluentAssertions.Json --version 1.0.5
NuGet\Install-Package PosInformatique.FluentAssertions.Json -Version 1.0.5
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="PosInformatique.FluentAssertions.Json" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PosInformatique.FluentAssertions.Json --version 1.0.5
#r "nuget: PosInformatique.FluentAssertions.Json, 1.0.5"
#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 PosInformatique.FluentAssertions.Json as a Cake Addin
#addin nuget:?package=PosInformatique.FluentAssertions.Json&version=1.0.5

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

PosInformatique.FluentAssertions.Json

PosInformatique.FluentAssertions.Json is a library to assert JSON serialization using the Fluent Assertions library style coding.

Installing from NuGet

The PosInformatique.FluentAssertions.Json library is available directly on the Nuget official website.

To download and install the library to your Visual Studio unit test projects use the following NuGet command line

Install-Package PosInformatique.FluentAssertions.Json

How it is work?

Imagine that you have the following JSON class:

public class Customer
{
    [JsonPropertyName("id")]
    [JsonPropertyOrder(1)]
    public int Id { get; set; }

    [JsonPropertyName("name")]
    [JsonPropertyOrder(2)]
    public string Name { get; set; }
}

With the following instance:

var customer = new Customer()
{
    Id = 1234,
    Name = "Gilles TOURREAU",
};

You would like to check this class is serializable into the following JSON object:

{
    "id": 1234,
    "name": "Gilles TOURREAU",
}

Using standard assertions you should write the following code 😨:

[Fact]
public void Serialization()
{
    var customer = new Customer()
    {
        Id = 1234,
        Name = "Gilles TOURREAU",
    };

    var json = JsonSerializer.Serialize(customer);

    json.Should().Be("{\"id\":1234,\"name\":\"Gilles TOURREAU\"}");
    
    // Or
    json.Should().Be(@"{""id"":1234,""name"":""Gilles TOURREAU""}");
}

With the following kind of exception when the unit test is incorrect: Ugly exception

As you can see the previous code is not sexy to read (and to write!) and the exception is hard to understand...

Test the serialization of a .NET Object to a JSON object.

With the new fluent style using this library you can write previous unit test like that:

[Fact]
public void Serialization()
{
    var customer = new Customer()
    {
        Id = 1234,
        Name = "Gilles TOURREAU",
    };

    customer.Should().BeJsonSerializableInto(new
    {
        id = 1234,
        name = "Gilles TOURREAU",
    });
}

And when an exception is occured, the exception message contains the JSON path of the property which is error: Pretty exception

Test the deserialization of a JSON object to a .NET Object

You can in the same way test the deserialization JSON object into a .NET object.

[Fact]
public void Deserialization()
{
    var json = new
    {
        id = 1234,
        name = "Gilles TOURREAU",
    };

    json.Should().BeJsonDeserializableInto(new Customer()
    {
        Id = 1234,
        Name = "Gilles TOURREAU",
    });
}

Library dependencies

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.2.0 1,002 10/29/2023
1.1.0 134 10/25/2023
1.0.5 151 10/15/2023
1.0.4 139 10/15/2023
1.0.3 118 10/13/2023
1.0.2 137 10/11/2023
1.0.1 167 10/10/2023
1.0.0 141 10/10/2023

1.0.5
     - Use the FluentAssertions library version 6.0.0 instead of 5.0.0 to fix breaking changes of the API.

     1.0.4
     - Ignore the properties with the [JsonIgnore] attribute.

     1.0.3
     - Target the .NET Standard 2.0 instead of .NET Core 6.0

     1.0.2
     - Fix the README.md file.

     1.0.1
     - Various fixes for the NuGet package description.

     1.0.0
     - Initial version