JsonPatch.Core
2.0.0
dotnet add package JsonPatch.Core --version 2.0.0
NuGet\Install-Package JsonPatch.Core -Version 2.0.0
<PackageReference Include="JsonPatch.Core" Version="2.0.0" />
paket add JsonPatch.Core --version 2.0.0
#r "nuget: JsonPatch.Core, 2.0.0"
// Install JsonPatch.Core as a Cake Addin #addin nuget:?package=JsonPatch.Core&version=2.0.0 // Install JsonPatch.Core as a Cake Tool #tool nuget:?package=JsonPatch.Core&version=2.0.0
JsonPatch
JsonPatch is a simple library that implements basic JSON Patch functionality as per RFC-6902 for .Net Core applications. It is not tied to ASP.Net, for that you should check out Michael McKenna's version on NuGet: https://www.nuget.org/packages/JsonPatch/
The library was converted from using Newtonsoft.Json to using the native .Net Core System.Text.Json.JsonSerializer. This saves ~700KB from the deployed size, which is welcome when writing Lambda functions. For more details see https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to
Usage
public void Patch(Guid id, JsonPatchDocument<SomeDto> patchData)
{
// Remember to do some validation and all that fun stuff
var objectToUpdate = repository.GetById(id);
patchData.ApplyUpdatesTo(objectToUpdate);
repository.Save(objectToUpdate);
}
Options
The path specified in the patch request can be resolved to a different property on the model using a resolver.
E.g. the FlexiblePathResolver will match a property based on
- The property name (case insensitive)
- The DataMember attribute
config.Formatters.Add(new JsonPatchFormatter(new JsonPatchSettings
{
PathResolver = new FlexiblePathResolver()
}));
Now the patch
[
{ "op": "add", "path": "/sampleProperty", "value": "foo" }
]
Will be valid for the class
class Foo
{
[DataMember(Name = "sampleProperty")]
public string MyProperty { get; set; }
}
Available resolvers are
- ExactCasePropertyPathResolver (default for backwards compatibility)
- CaseInsensitivePropertyPathResolver
- AttributePropertyPathResolver
- FlexiblePathResolver
Notes
Because we are applying updates to C# objects rather than JSON objects we differ from the spec slightly, here are some points of difference.
JSON Pointer (rfc6901)
The JSON patch document uses JSON pointers to refer to properties, we don't accept paths in the following formats:
- The path "" is invalid as it refers to the whole object, if you're replacing everything, just use PUT.
- The path "/" is invalid as you cannot have an empty property.
- The path "/5" is invalid as you can only update an entity, not an array of entities.
Available operations (rfc6902#section-4)
We only support a subset of available operations at this moment: add, remove, and replace.
Since C# is a typed language we have a few restrictions, hence the operations work a little differently than specified in the specification.
Here's our flavour:
- Add will not add a property that does not exist, it can only be used to specify values on properties that are null.
- Remove will not remove a property, it will set its value to null.
- Replace will replace the value of a property even if it is null.
This is still very much an early project, don't use it in production yet unless you understand the source and don't mind fixing a few bugs 😉
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 | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
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.0 | 1,747 | 8/7/2020 |