Tingle.Extensions.JsonPatch 4.7.0

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

// Install Tingle.Extensions.JsonPatch as a Cake Tool
#tool nuget:?package=Tingle.Extensions.JsonPatch&version=4.7.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 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. 
.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 (1)

Showing the top 1 NuGet packages that depend on Tingle.Extensions.JsonPatch:

Package Downloads
Falu

The official client library for Falu. (https://falu.io)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.7.0 81 3/25/2024
4.6.0 193 3/8/2024
4.5.0 255 11/22/2023
4.4.1 1,777 11/20/2023
4.4.0 314 11/15/2023
4.3.0 125 10/18/2023
4.2.2 119 9/20/2023
4.2.1 188 8/4/2023
4.2.0 2,967 5/31/2023
4.1.1 147 5/26/2023
4.1.0 811 5/22/2023
4.0.0 203 3/14/2023
3.8.1 528 11/30/2022
3.8.0 1,742 11/21/2022
3.7.0 3,058 9/4/2022
3.6.0 4,254 7/4/2022
3.5.3 630 4/8/2022
3.5.2 2,733 1/7/2022
3.5.1 2,113 11/11/2021
3.5.0 330 11/10/2021
3.4.7 374 11/2/2021
3.4.6 342 10/29/2021
3.4.5 417 10/9/2021
3.4.4 2,581 8/9/2021
3.4.3 285 8/9/2021
3.4.2 6,144 6/12/2021