Tingle.AspNetCore.JsonPatch 4.8.0

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

// Install Tingle.AspNetCore.JsonPatch as a Cake Tool
#tool nuget:?package=Tingle.AspNetCore.JsonPatch&version=4.8.0

Tingle.AspNetCore.JsonPatch

The primary goal of this library is to provide functionalities to perform Json Patch and JSON Merge Patch operations on documents using System.Text.Json library.

Json Patch

JSON patch support is quite similar to Microsoft's equivalent for Newtonsoft.Json. The only difference is how you configure the input and output formatters for all JSON content. This should be done as shown below:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers()
                .AddJsonPatch() // would be .AddNewtonsoftJson() in the Newtonsoft.Json equivalent
                .AddJsonOptions(options => {}); // Add and configure JSON formatters

var app = builder.Build();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Json Merge Patch

The library helps to deserialize HTTP requests' and responses' JSON body content for merge patch operation. If the merge patch request contains members that appear as null on the target object, those members are added. If the target object contains the member, the value is replaced. Members with null values in the merge patch requests, are removed from the target object (set to null or default).

For example, the following JSON documents represent a resource, a JSON Merge Patch document for the resource, and the result of applying the Patch operations.

Resource Example

{
    "id": "1",
    "name": null,
    "phone": "+254722000000",
    "country": "ken"
}

JSON Merge Patch Example

{
    "name": "Fabrikam",
    "phone": "+254722000001",
    "country": null
}

Resource after patch

{
    "id": "1",
    "name": "Fabrikam",
    "phone": "+254722000001",
    "country": null
}

id property remains unchanged as it is not part of the merge patch request.

JSON Merge Patch in ASP.NET Core

Define a Customer model:

class Customer
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Country { get; set; }
}

Add the following logic in the Program.cs file. This same logic can be added to Startup.cs.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers()
                .AddJsonPatch()
                .AddJsonOptions(options => {}); // Add and configure JSON formatters

var app = builder.Build();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Use in your controller

[HttpPatch]
public void Patch([FromBody] JsonMergePatchDocument<Customer> patch)
{
    ...
    patch.ApplyTo(customer, ModelState);
    ...
}

In a real app, the code would retrieve the data from a store such as a database and update the database after applying the patch.

The preceding action method example calls an overload of ApplyTo that takes model state as one of its parameters. With this option, you can get error messages in responses.

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

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

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
4.8.0 0 5/5/2024
4.7.0 371 4/6/2024