Morcatko.AspNetCore.JsonMergePatch
2.0.5
dotnet add package Morcatko.AspNetCore.JsonMergePatch --version 2.0.5
NuGet\Install-Package Morcatko.AspNetCore.JsonMergePatch -Version 2.0.5
<PackageReference Include="Morcatko.AspNetCore.JsonMergePatch" Version="2.0.5" />
paket add Morcatko.AspNetCore.JsonMergePatch --version 2.0.5
#r "nuget: Morcatko.AspNetCore.JsonMergePatch, 2.0.5"
// Install Morcatko.AspNetCore.JsonMergePatch as a Cake Addin #addin nuget:?package=Morcatko.AspNetCore.JsonMergePatch&version=2.0.5 // Install Morcatko.AspNetCore.JsonMergePatch as a Cake Tool #tool nuget:?package=Morcatko.AspNetCore.JsonMergePatch&version=2.0.5
JSON Merge Patch support for ASP.NET Core
- Morcatko.AspNetCore.JsonMergePatch (ASP.NET Core 2.x)
- Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson (ASP.NET Core 3.x)
- Morcatko.AspNetCore.JsonMergePatch.SystemText (ASP.NET Core 3.x)
- Morcatko.AspNetCore.JsonMergePatch.Document (ASP.NET Core 3.x - base package)
JSON Merge Patch
- RFC 7396
- performs partial resource update similar to JSON Patch
- Supports Swagger
- netstandard 2.0
C# object:
var backendModel = new Model()
{
Name = "James Bond"
Age = "45"
Weapon = "Gun"
}
JSON Merge Patch:
{
"Weapon": "Knife"
}
resulting C# object:
{
Name = "James Bond"
Age = "45"
Weapon = "Knife"
}
How to
See 2.1-testApp
or 3.0 testApp
for sample
- Install nuget.
- ASP.NET Core 2.x Morcatko.AspNetCore.JsonMergePatch
- ASP.NET Core 3.x (Newtonsoft.Json) Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson
- ASP.NET Core 3.x (System.Text) Morcatko.AspNetCore.JsonMergePatch.SystemText nuget
- Add to your startup class
using Morcatko.AspNetCore.JsonMergePatch;
public void ConfigureServices(IServiceCollection services)
{
...
services
.AddMvc() // or .AddMvcCore()
//.AddJsonMergePatch(); // 2.x
//.AddNewtonsoftJsonMergePatch(); // 3.x (Newtonsoft.Josn)
//.AddSystemTextJsonMergePatch(); // 3.x (System.Text)
...
}
- Use in your controller
using Morcatko.AspNetCore.JsonMergePatch;
[HttpPatch]
[Consumes(JsonMergePatchDocument.ContentType)]
public void Patch([FromBody] JsonMergePatchDocument<Model> patch)
{
...
patch.ApplyTo(backendModel);
...
}
You can apply a patch to a different Type (be carefull, all C# static typing is ignored) - see #16 for more details.
BackendModel backendModel;
JsonMergePatch<DtoModel> patch;
patch.ApplyToT(backendModel)
- Swagger config (optional)
copy & paste this class into your app
- 2.x - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/src/2.1-testApp/JsonMergePatchDocumentOperationFilter.cs
- 3.x - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/src/3.0-testApp/JsonMergePatchDocumentOperationFilter.cs
services.AddSwaggerGen(c =>
{
c.OperationFilter<JsonMergePatchDocumentOperationFilter>();
});
Options
services
.AddMvc()
.AddJsonMergePatch(o => ....)
- bool EnableDelete - Deletes items when target object is Dictionary and patched value is null
How to - unit testing
See tests in ...Builder.Json.Simple
class for more examples
Morcatko.AspNetCore.JsonMergePatch.Tests.Builder.Json
public void UnitTest()
{
var model = new Model();
var patch1 = PatchBuidler.Build<Model>("{ integer: 1}");
...
or
...
var original = new Model();
var patched = new Model() { Integer = 1};
var patch2 = PatchBuilder.Build(original, patched);
}
Known issues/Not working
- ModelState.IsValid is false when a required property is missing
- Enums with [EnumMember(Value = "....")] attribute
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 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 was computed. 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- Microsoft.AspNetCore.Mvc.Formatters.Json (>= 2.1.2)
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 |
---|---|---|
2.0.5 | 34,538 | 3/24/2021 |
2.0.4 | 1,343 | 8/26/2020 |
2.0.3 | 697 | 8/7/2020 |
2.0.2 | 14,817 | 11/7/2019 |
2.0.1 | 1,639 | 11/3/2019 |
2.0.0 | 196,088 | 6/20/2019 |
2.0.0-beta10 | 1,091 | 5/27/2019 |
2.0.0-beta09 | 8,848 | 4/11/2019 |
2.0.0-beta08 | 584 | 4/2/2019 |
2.0.0-beta07 | 19,519 | 12/13/2018 |
2.0.0-beta06 | 2,794 | 12/7/2018 |
2.0.0-beta05 | 2,407 | 11/20/2018 |
2.0.0-beta04 | 53,082 | 9/7/2018 |
2.0.0-beta03 | 5,406 | 11/6/2017 |
2.0.0-beta02 | 839 | 11/3/2017 |
2.0.0-beta01 | 728 | 10/24/2017 |
2.0.0-alpha02 | 1,718 | 9/14/2017 |
1.0.0-alpha01 | 1,086 | 6/23/2017 |