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 82 11/19/2024
4.14.1 95 10/14/2024
4.14.0 96 9/16/2024
4.13.0 150 8/13/2024
4.12.0 136 8/7/2024
4.11.2 135 7/15/2024
4.11.1 160 6/26/2024
4.11.0 111 6/6/2024
4.10.1 110 6/5/2024
4.10.0 509 5/27/2024
4.9.0 108 5/16/2024
4.8.0 115 5/5/2024
4.7.0 143 3/25/2024
4.6.0 284 3/8/2024
4.5.0 290 11/22/2023
4.4.1 2,382 11/20/2023
4.4.0 359 11/15/2023
4.3.0 158 10/18/2023
4.2.2 151 9/20/2023
4.2.1 218 8/4/2023
4.2.0 3,032 5/31/2023
4.1.1 177 5/26/2023
4.1.0 855 5/22/2023
4.0.0 231 3/14/2023
3.8.1 558 11/30/2022
3.8.0 1,781 11/21/2022
3.7.0 3,121 9/4/2022
3.6.0 4,340 7/4/2022
3.5.3 666 4/8/2022
3.5.2 2,784 1/7/2022
3.5.1 2,167 11/11/2021
3.5.0 359 11/10/2021
3.4.7 398 11/2/2021
3.4.6 382 10/29/2021
3.4.5 451 10/9/2021
3.4.4 2,646 8/9/2021
3.4.3 317 8/9/2021
3.4.2 6,211 6/12/2021