Tingle.Extensions.JsonPatch 5.0.0

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

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

Tingle.Extensions.JsonPatch

The primary goal of this library is to provide functionalities to perform JsonPatch operations on documents using System.Text.Json library on clients (not server side). We'll show how to do this in some examples below.

Let us first define a class representing a customer with orders.

class Customer
{
    public string Name { get; set; }
    public List<Order> Orders { get; set; }
    public string AlternateName { get; set; }
}

class Order
{
    public string Item { get; set; }
    public int Quantity { get; set;}
}

An instantiated Customer object would then look like this:

{
    "name": "John",
    "alternateName": null,
    "orders":
    [
        {
            "item": "Toy car",
            "quantity": 1
        },
        {
            "item": "C# In A Nutshell Book",
            "quantity": 1
        }
    ]
}

A JSON Patch document has an array of operations. Each operation identifies a particular type of change. Examples of such changes include adding an array element or replacing a property value.

Let us create JsonPatchDocument<Customer> instance to demonstrate the various patching functionalities we provide.

var patchDoc = new JsonPatchDocument<Customer>();
// Define operations here...

By default, the case transform type is LowerCase. Other options available are UpperCase, CamelCase and OriginalCase. These can be set via the constructor of the JsonPatchDocument<T>. For our example purposes we'll go with the default casing. Now let us see the supported patch operations.

Add Operation

Let us set the Name of the customer and add an object to the end of the orders array.

var order = new Order
{
    Item = "Car tracker",
    Quantity = 10
};

var patchDoc = new JsonPatchDocument<Customer>();
patchDoc.Add(x => x.Name, "Ben")
        .Add(y => y.Orders, order);

Remove Operation

Let us set the Name to null and delete orders[0]

var patchDoc = new JsonPatchDocument<Customer>();
patchDoc.Remove(x => x.Name, null)
        .Remove(y => y.Orders, 0);

Replace Operation

This is the same as a Remove operation followed by an Add. Let us show how to do this below:

var order = new Order
{
    Item = "Air Fryer",
    Quantity = 1
};

var patchDoc = new JsonPatchDocument<Customer>();
patchDoc.Replace(x => x.Name, null)
        .Replace(y => y.Orders, order, 0);

The Replace operation can also be used to replace items in a dictionary by the given key.

Move Operation

Let us Move orders[1] to before orders[0] and set AlternateName from the Name value.

var patchDoc = new JsonPatchDocument<Customer>();
patchDoc.Move(x => x.Orders, 0, y => y.Orders, 1) // swap the orders
        .Move(x => x.Name, y => y.AlternateName); // set AlternateName to Name while leaving Name as null

Copy Operation

This operation is fundamentally the same as Move without the final Remove step.

Let us in the example below copy the value of Name to the AlternateName and insert a copy of orders[1] before orders[0].

var patchDoc = new JsonPatchDocument<Customer>();
patchDoc.Copy(x => x.Orders, 1, y => y.Orders, 0)
        .Copy(x => x.Name, y => y.AlternateName);

Test Operation

This operation is commonly used to prevent an update when there's a concurrency conflict.

The following sample patch document has no effect if the initial value of Name is "John", because the test fails:

var patchDoc = new JsonPatchDocument<Customer>();
patchDoc.Test(x => x.Name, "Andrew")
        .Add(x => x.Name, "Ben");

The instantiated patch document can then be serialized or deserialized using the JsonSerializer in the System.Text.Json library.

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 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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
5.0.0 69 11/19/2024
4.14.1 90 10/14/2024
4.14.0 91 9/16/2024
4.13.0 143 8/13/2024
4.12.0 131 8/7/2024
4.11.2 131 7/15/2024
4.11.1 156 6/26/2024
4.11.0 107 6/6/2024
4.10.1 104 6/5/2024
4.10.0 505 5/27/2024
4.9.0 104 5/16/2024
4.8.0 109 5/5/2024
4.7.0 141 3/25/2024
4.6.0 282 3/8/2024
4.5.0 288 11/22/2023
4.4.1 2,380 11/20/2023
4.4.0 357 11/15/2023
4.3.0 156 10/18/2023
4.2.2 149 9/20/2023
4.2.1 216 8/4/2023
4.2.0 3,030 5/31/2023
4.1.1 175 5/26/2023
4.1.0 853 5/22/2023
4.0.0 229 3/14/2023
3.8.1 556 11/30/2022
3.8.0 1,779 11/21/2022
3.7.0 3,117 9/4/2022
3.6.0 4,334 7/4/2022
3.5.3 662 4/8/2022
3.5.2 2,780 1/7/2022
3.5.1 2,163 11/11/2021
3.5.0 355 11/10/2021
3.4.7 394 11/2/2021
3.4.6 378 10/29/2021
3.4.5 447 10/9/2021
3.4.4 2,642 8/9/2021
3.4.3 313 8/9/2021
3.4.2 6,207 6/12/2021