PosInformatique.FluentAssertions.Json 1.0.1

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.1                
NuGet\Install-Package PosInformatique.FluentAssertions.Json -Version 1.0.1                
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.1" />                
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.1                
#r "nuget: PosInformatique.FluentAssertions.Json, 1.0.1"                
#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.1

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

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.AspNet.WebForms.DependencyInjection

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",
    });
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on PosInformatique.FluentAssertions.Json:

Package Downloads
PosInformatique.Testing.Azure.Functions.Http

This project provides utilities for testing Azure Functions (Isolated). It includes features and tools to facilitate the testing of HttpRequestData and HttpResponseData classes, which are challenging to mock.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 94 10/28/2024
1.4.0-rc.1 40 10/28/2024
1.3.0 352 6/28/2024
1.3.0-rc.1 53 6/18/2024
1.2.0 1,305 10/29/2023
1.1.0 158 10/25/2023
1.0.5 184 10/15/2023
1.0.4 166 10/15/2023
1.0.3 152 10/13/2023
1.0.2 169 10/11/2023
1.0.1 194 10/10/2023
1.0.0 167 10/10/2023

1.0.1
     - Various fixes for the NuGet package description.
     
     1.0.0
     - Initial version