Skip to content

A free, lightweight library for using JSON PATCH (RFC 6902) and JSON MERGE PATCH (RFC 7386) in your RESTful .NET-Services

License

Notifications You must be signed in to change notification settings

SeeSharp7/patch4net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

patch4net

A free and easy to use .NET library for JSON PATCH (RFC 6902) and JSON MERGE PATCH (RFC 7386) in your RESTful services.

IMORTANT NOTICE!

JSON PATCH (RFC 6902) is currently not implemented! If this is a essential feature for you, wait until it's finished, please.

USAGE

The usage of this library is very simple. It only consumes the minimum required data. There is no restriction on your original model and it doesn't need to be converted because of generic types. The body of your incoming patch request simply needs to be a string.

JSON PATCH (RFC 6902)

var patchedModel = new JsonPatcher().Patch(patchJson, simpleModel);

JSON MERGE PATCH (RFC 7386)

var patchedModel = new JsonPatcher().MergePatch(mergePatchJson, simpleModel);

Flexible patch via ContentType-Header

You want to support json patch as well as json merge patch in your API? No problem. The variant of patch must be set in the ContentType-Header. When you make use of the UniversalPatch-Method, you don't even need to check the header. patch4Net will do that for you and chooses the correct patch implementation!

var patchedModel = new JsonPatcher().UniversalPatch(requestBody, originalModel, incomingContentTypeHeaderValue);

WebAPI Example

//PATCH api/values/{id}
public void Patch([FromBody] string patchRequest, [FromUri] string id)
{
    //Extract ContentType-Header
    var headerValues = Request.Headers.GetValues("ContentType");
    var contentTypeValue = headerValues.FirstOrDefault();

    //Load your original model
    var myOrignialModel = MyModel.Load(id);

    //Perform patch
    var patchedModel = new JsonPatcher().UniversalPatch(patchRequest, myOrignialModel, contentTypeValue);

    //Overwrite original model
    patchedModel.Save(id);
}

CUSTOMIZATION

Many projects make use of exotic settings within their serializers. So it's impossible to write one, that covers all needs. To solve that problem, I decided to create an interface ISerializer to implement completely custom serializers with the specific settings.

The Interface provides two simple methods:

TModelType DeserializeObject<TModelType>(string serializedObject);
string SerializeObject<TModelType>(TModelType modelObject);

About

A free, lightweight library for using JSON PATCH (RFC 6902) and JSON MERGE PATCH (RFC 7386) in your RESTful .NET-Services

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages