Giter Site home page Giter Site logo

jsonpatch's Introduction

JsonPatch

JsonPatch is a simple library which adds JSON Patch support to ASP.NET Web API (http://tools.ietf.org/html/rfc6902).

You can get it on NuGet here: https://www.nuget.org/packages/JsonPatch/

Usage

##Step 1: Install the formatter

public static void ConfigureApis(HttpConfiguration config)
{
    config.Formatters.Add(new JsonPatchFormatter());
}

##Step 2: Profit??

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);
}

##Making a PATCH request

The main thing is to make sure the content type is "application/json-patch+json" otherwise Web API won't invoke the JsonPatch media formatter. Here's an example.

PATCH /my/data HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json

[
    { "op": "add", "path": "/a/b/c", "value": "foo" }
]

##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 JsonProperty attribute
  • The DataMember attribute
  • The Property Name (Case Insensitive)
config.Formatters.Add(new JsonPatchFormatter(new JsonPatchSettings
{
     PathResolver = new FlexiblePathResolver()
}));

Now the request

PATCH /my/data HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json

[
    { "op": "add", "path": "/sampleProperty", "value": "foo" }
]

Will be valid for the class

{
    [JsonProperty("sampleProperty")]
    public string SampeProperty {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:

  1. The path "" is invalid as it refers to the whole object, if you're replacing everything, just use PUT.
  2. The path "/" is invalid as you cannot have an empty property.
  3. 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:

  1. Add will not add a property that does not exist, it can only be used to specify values on properties that are null.
  2. Remove will not remove a property, it will set its value to null.
  3. 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 ;)

Twitter: @myquay

jsonpatch's People

Contributors

myquay avatar wadeg-findly avatar donaldgray avatar tugberkugurlu avatar

Watchers

James Cloos avatar Nikita avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.