We've created a PR on the original repo and it's waiting for more than 1 year. :( Hopefully it will be merged someday so we can archive this repo for good.
This gem augments Ruby's built-in JSON library to support merging JSON blobs in accordance with the draft-snell-merge-patch draft.
As far as I know, it is a complete implementation of the draft. If you find something that's not compliant, please let me know.
Add this line to your application's Gemfile:
gem 'json-merge_patch'
And then execute:
$ bundle
Or install it yourself as:
$ gem install json-merge_patch
First, require the gem:
require 'json/merge_patch'
Then, use it:
# The example from http://tools.ietf.org/html/draft-snell-merge-patch-08#section-2
document = <<-JSON
{
"title": "Goodbye!",
"author" : {
"givenName" : "John",
"familyName" : "Doe"
},
"tags":["example","sample"],
"content": "This will be unchanged"
}
JSON
merge_patch = <<-JSON
{
"title": "Hello!",
"phoneNumber": "+01-123-456-7890",
"author": {
"familyName": null
},
"tags": ["example"]
}
JSON
JSON.merge(document, merge_patch)
# =>
{
"title": "Hello!",
"phoneNumber": "+01-123-456-7890",
"author" : {
"givenName" : "John",
},
"tags":["example"],
"content": "This will be unchanged"
}
If you'd prefer to operate on pure Ruby objects rather than JSON strings, you
can construct a MergePatch
object instead.
JSON::MergePatch.new({}, {"foo" => "bar"}).call
# => {"foo"=>"bar"}
Also check out http://json-merge-patch.herokuapp.com/,
which is a Rails app that serves up json-merge-patch
responses.
JSON::MergePatch provides a Railtie that registers the proper MIME type with Rails. To use it, do something like this:
def update
safe_params = params.require(:merge).permit(:original, :patch)
@result = JSON::MergePatch.new(
safe_params[:original],
safe_params[:patch]
).call
respond_to do |format|
format.json_merge_patch do
render :json => @result
end
end
end
- 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