- Morcatko.AspNetCore.JsonMergePatch (ASP.NET Core 2.x)
- Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson (ASP.NET Core 3+)
- Morcatko.AspNetCore.JsonMergePatch.SystemText (ASP.NET Core 3+)
- Morcatko.AspNetCore.JsonMergePatch.Document (ASP.NET Core 3+ - base package)
C# object:
var backendModel = new Model()
{
Name = "James Bond"
Age = "45"
Weapon = "Gun"
}
JSON Merge Patch:
var mergePatch = new object() {
"Weapon": "Knife"
}
Resulting C# object:
var newObject = new Model() {
Name = "James Bond"
Age = "45"
Weapon = "Knife"
}
See 2.1-testApp
, 3.0 testApp
or 6.0/testApp
for sample
Install nuget (Use JsonMergePathch version same as your .NET) .
Add to your startup class
using Morcatko.AspNetCore.JsonMergePatch;
public void ConfigureServices(IServiceCollection services) { ... services .AddMvc() // or .AddMvcCore() //.AddJsonMergePatch(); // 2.x //.AddNewtonsoftJsonMergePatch(); // 3+ (Newtonsoft.Josn) //.AddSystemTextJsonMergePatch(); // 3+ (System.Text) ... }
3. Use in your controller
````csharp
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)
copy & paste this class into your app
services.AddSwaggerGen(c =>
{
c.OperationFilter<JsonMergePatchDocumentOperationFilter>();
});
services
.AddMvc()
.AddJsonMergePatch(o => ....)
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 = PatchBuilder.Build<Model>("{ integer: 1}");
...
or
...
var original = new Model();
var patched = new Model() { Integer = 1};
var patch2 = PatchBuilder.Build(original, patched);
}