Inspired by:
- Steve Klabnik: https://github.com/steveklabnik/json-merge_patch
- Tenderlove: https://github.com/tenderlove/hana
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
Add this line to your application's Gemfile:
gem 'json-patch'
And then execute:
$ bundle
Or install it yourself as:
$ gem install json-patch
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" ] }
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request