Skip to content

Ruby implementation of JSON Patch (identified by json-patch+json media type).

License

Notifications You must be signed in to change notification settings

guillec/json-patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON::Patch

Build Status Code Climate Coverage Status

Inspired by:

This gem augments Ruby's built-in JSON library to support JSON Patch (identified by the json-patch+json media type). http://tools.ietf.org/html/rfc6902

Installation

Add this line to your application's Gemfile:

gem 'json-patch'

And then execute:

$ bundle

Or install it yourself as:

$ gem install json-patch

Usage

Then, use it:

# The example from http://tools.ietf.org/html/rfc6902#appendix-A

# Add Object Member
target_document = <<-JSON
  { "foo": "bar"}
JSON

operations_document = <<-JSON
[
  { "op": "add", "path": "/baz", "value": "qux" }
]
JSON

JSON.patch(target_document, operations_document)
# => 
{ "baz": "qux", "foo": "bar" }


# Add Array Element
target_document = <<-JSON
  { "foo": [ "bar", "baz" ] }
JSON

operations_document = <<-JSON
[
  { "op": "add", "path": "/foo/1", "value": "qux" }
]
JSON

JSON.patch(target_document, operations_document)
# => 
{ "foo": [ "bar", "qux", "baz" ] }

If you'd prefer to operate on pure Ruby objects rather than JSON strings, you can construct a JSON::Patch object instead.

target_document = { "foo" => [ "bar", "baz" ] }
operations_document = [{ "op" => "add", "path" => "/foo/1", "value" => "qux" }]

JSON::Patch.new(target_document, operations_document).call
# => 
{ "foo" => [ "bar", "qux", "baz" ] }

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Ruby implementation of JSON Patch (identified by json-patch+json media type).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages