Giter Club home page Giter Club logo

tavis.jsonpatch's Introduction

Tavis.JsonPatch

This library is an implementation of a Json Patch RFC 6902.

The default example from the specification looks like this,

[
     { "op": "test", "path": "/a/b/c", "value": "foo" },
     { "op": "remove", "path": "/a/b/c" },
     { "op": "add", "path": "/a/b/c", "value": ["foo", "bar"] },
     { "op": "replace", "path": "/a/b/c", "value": 42 },
     { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
     { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
]

This library allows you to create this document by doing,

   var patchDoc = new PatchDocument( new Operation[]
        {
         new TestOperation() {Path = new JsonPointer("/a/b/c"), Value = new JValue("foo")}, 
         new RemoveOperation() {Path = new JsonPointer("/a/b/c") }, 
         new AddOperation() {Path = new JsonPointer("/a/b/c"), Value = new JArray(new JValue("foo"), new JValue("bar"))}, 
         new ReplaceOperation() {Path = new JsonPointer("/a/b/c"), Value = new JValue(42)}, 
         new MoveOperation() {FromPath = new JsonPointer("/a/b/c"), Path = new JsonPointer("/a/b/d") }, 
         new CopyOperation() {FromPath = new JsonPointer("/a/b/d"), Path = new JsonPointer("/a/b/e") }, 
        });

This document can be serialized to the wire format like this,

     var outputstream = patchDoc.ToStream();

You can also read patch documents from the wire representation and apply them to a JSON document.

    var targetDoc = JToken.Parse("{ 'foo': 'bar'}");
    var patchDoc = PatchDocument.Parse(@"[
                  { 'op': 'add', 'path': '/baz', 'value': 'qux' }
                ]");

    patchDoc.ApplyTo(targetDoc);

    Assert.True(JToken.DeepEquals(JToken.Parse(@"{
                                                     'foo': 'bar',
                                                     'baz': 'qux'
                                                   }"), targetDoc));

You can also apply the Json patch format to other targets by implementing the IPatchTarget interface.

The unit tests provide examples of other usages.

This library is a PCL based library and so will work on Windows 8, Windows Phone 7.5, .Net 4.

A nuget package is available here.

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.