Tingle.Extensions.JsonPatch
4.14.0
See the version list below for details.
dotnet add package Tingle.Extensions.JsonPatch --version 4.14.0
NuGet\Install-Package Tingle.Extensions.JsonPatch -Version 4.14.0
<PackageReference Include="Tingle.Extensions.JsonPatch" Version="4.14.0" />
paket add Tingle.Extensions.JsonPatch --version 4.14.0
#r "nuget: Tingle.Extensions.JsonPatch, 4.14.0"
// Install Tingle.Extensions.JsonPatch as a Cake Addin #addin nuget:?package=Tingle.Extensions.JsonPatch&version=4.14.0 // Install Tingle.Extensions.JsonPatch as a Cake Tool #tool nuget:?package=Tingle.Extensions.JsonPatch&version=4.14.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 | Versions 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. |
-
.NETStandard 2.1
- System.Text.Json (>= 8.0.4)
-
net7.0
- System.Text.Json (>= 8.0.4)
-
net8.0
- System.Text.Json (>= 8.0.4)
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.14.1 | 84 | 10/14/2024 |
4.14.0 | 89 | 9/16/2024 |
4.13.0 | 141 | 8/13/2024 |
4.12.0 | 129 | 8/7/2024 |
4.11.2 | 129 | 7/15/2024 |
4.11.1 | 154 | 6/26/2024 |
4.11.0 | 105 | 6/6/2024 |
4.10.1 | 102 | 6/5/2024 |
4.10.0 | 503 | 5/27/2024 |
4.9.0 | 102 | 5/16/2024 |
4.8.0 | 107 | 5/5/2024 |
4.7.0 | 139 | 3/25/2024 |
4.6.0 | 278 | 3/8/2024 |
4.5.0 | 286 | 11/22/2023 |
4.4.1 | 2,378 | 11/20/2023 |
4.4.0 | 355 | 11/15/2023 |
4.3.0 | 154 | 10/18/2023 |
4.2.2 | 147 | 9/20/2023 |
4.2.1 | 214 | 8/4/2023 |
4.2.0 | 3,028 | 5/31/2023 |
4.1.1 | 171 | 5/26/2023 |
4.1.0 | 851 | 5/22/2023 |
4.0.0 | 227 | 3/14/2023 |
3.8.1 | 554 | 11/30/2022 |
3.8.0 | 1,777 | 11/21/2022 |
3.7.0 | 3,115 | 9/4/2022 |
3.6.0 | 4,332 | 7/4/2022 |
3.5.3 | 660 | 4/8/2022 |
3.5.2 | 2,778 | 1/7/2022 |
3.5.1 | 2,161 | 11/11/2021 |
3.5.0 | 351 | 11/10/2021 |
3.4.7 | 392 | 11/2/2021 |
3.4.6 | 376 | 10/29/2021 |
3.4.5 | 445 | 10/9/2021 |
3.4.4 | 2,640 | 8/9/2021 |
3.4.3 | 311 | 8/9/2021 |
3.4.2 | 6,203 | 6/12/2021 |