Giter Club home page Giter Club logo

json-patch's Introduction

JSON-Patch

A leaner and meaner implementation of JSON-Patch. Small footprint. High performance. Also support dual directions! I.e. you can both apply patches and generate patches.

Why you should use JSON-Patch

JSON-Patch (RFC6902) is a new standard format that allows you to update a JSON document by sending the changes rather than the whole document. JSON Patch plays well with the HTTP PATCH verb (method) and REST style programming.

Mark Nottingham has a nice blog about it.

Footprint

0.5 KB minified and gzipped (1.1 KB minified)

Performance

Fast

Features

  • Allows you to apply patches on object trees for incoming traffic.
  • Allows you to freely manipulate object trees and then generate patches for outgoing traffic.
  • ES6/7 Object.observe() is used when available.
  • Tested in IE 8-10, Firefox, Chrome and Node.js

Roadmap

  • A /bin directory will be added with minified versions
  • More unit tests

Adding to your project

In a web browser

Include json-patch.js if you want support for applying patches or include json-patch-duplex.js if you also want to generate patches.

In Node.js

Install the current version (and save it as a dependency in package.json):

$ npm install fast-json-patch --save

Call require to get the instance:

var jsonpatch = require('fast-json-patch')

Usage

Applying patches:

var myobj = { firstName:"Albert", contactDetails: { phoneNumbers: [ ] } };
var patches = [
   {op:"replace", path:"/firstName", value:"Joachim" },
   {op:"add", path:"/lastName", value:"Wester" },
   {op:"add", path:"/contactDetails/phoneNumbers/0", value:{ number:"555-123" }  }
   ];
jsonpatch.apply( myobj, patches );
// myobj == { firstName:"Joachim", lastName:"Wester", contactDetails:{ phoneNumbers[ {number:"555-123"} ] } };

Generating patches:

var myobj = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
observer = jsonpatch.observe( myobj );
myobj.firstName = "Albert";
myobj.contactDetails.phoneNumbers[0].number = "123";
myobj.contactDetails.phoneNumbers.push({number:"456"});
var patches = jsonpatch.generate(observer);
// patches  == [
//   { op:"replace", path="/firstName", value:"Albert"},
//   { op:"replace", path="/contactDetails/phoneNumbers/0/number", value:"123"},
//   { op:"add", path="/contactDetails/phoneNumbers/1", value:{number:"456"}}];

Testing

In a web browser

  1. Testing json-patch.js
  • Load src/patchtest.html in your web browser
  1. Testing json-patch-duplex.js
  • Load src/test-duplex.html in your web browser

Each of the test suite files contains Jasmine unit test suite and JSLitmus performance test suite.

To run JSLitmus performance tests, press "Run Tests" button.

In Node.js

  1. Go to directory where you have cloned the repo
  2. Install Jasmine Node.js module by running command npm install jasmine-node -g
  3. Testing json-patch.js
  • Run command jasmine-node --matchall --config duplex no src/test.js
  1. Testing json-patch-duplex.js
  • Run command jasmine-node --matchall --config duplex yes src/test.js src/test-duplex.js

json-patch's People

Contributors

warpech avatar bendiy avatar andwah avatar shackbarth avatar mattdrees avatar psmolenski avatar

Watchers

 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.