GithubHelp home page GithubHelp logo

tenderlove / hana Goto Github PK

View Code? Open in Web Editor NEW
216.0 12.0 27.0 63 KB

An implementation of JSON Patch and JSON Pointer for Ruby

License: MIT License

Ruby 100.00%
json-pointer ruby json json-patch

hana's Introduction

hana

DESCRIPTION:

Implementation of JSON Patch and JSON Pointer RFC.

FEATURES/PROBLEMS:

Implements specs of the JSON Patch and JSON pointer RFCs:

This works against Ruby objects, so you should load the JSON to Ruby, process it, then emit as JSON again.

SYNOPSIS:

patch = Hana::Patch.new [
  { 'op' => 'add', 'path' => '/baz', 'value' => 'qux' }
]

patch.apply('foo' => 'bar') # => {'baz' => 'qux', 'foo' => 'bar'}

REQUIREMENTS:

  • Ruby

INSTALL:

$ gem install hana

DEVELOPMENT:

hana runs tests from json-patch/json-patch-tests. Fetch the git submodule by running:

git submodule init
git submodule update

Install dependencies with:

bundle install

Then run the tests with:

rake test

hana's People

Contributors

jpterry avatar morganatwishpond avatar ndbroadbent avatar ngfreiter avatar odlp avatar pvdb avatar steveklabnik avatar tenderlove avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hana's Issues

New release

Hi,

Since the latest release, both code and spec as evolved. It would be nice to release a new version.

New release?

Any chance we could get a new release of this gem? There has been at least one PR merged since the last release.

Any way to add a nested object?

I'm trying to build up a JSON document hash starting from {}. I keep ending up here when I add a nested path. I see the problem -- you can't know whether applying the patch {"op": "add", "path": "/foo/0", "value": "bar"} to {} should yield

{"foo": {"0": "bar"}}

or

{"foo": ["bar"]}

However, for paths with all non-numeric segments there should be no confusion.

Is there a way to add a nested element with Hana that I can't see? Is it reasonable to think of adding support for this?

[Question] How to use JSON Patch with SSE in Rails Controller?

Hi @tenderlove ,

In the controller:

module Api::V1
  class TestController < ApiController
    include ActionController::Live

    def ticker
      response.headers['Content-Type'] = 'text/event-stream'
      sse = Ticker::SSE.new(response.stream)
      begin
        loop do
          test = Test.desc
          a = test.second.as_json(except: ["created_at", "updated_at", "_id"]) 
          b = test.first.as_json(except: ["created_at", "updated_at", "_id"])
          diff = JsonDiff.generate(a, b)
         patch = Hana::Patch.new [diff]
         #what to do then with `patch.apply` and `sse.write`?
          sse.write(json)
        end
      rescue IOError
        logger.info 'Client disconnects causes IOError on write'
      ensure
        sse.close
      end
    end
  end
end

In app/lib/ticker/sse.rb

module Ticker
  class SSE
    def initialize(io)
      @io = io
    end

    def write(object, options = {})
      options.each do |k,v|
        @io.write("#{k}: #{v}\n")
      end
      @io.write("data: #{JSON.dump(object)}\n\n")
    end

    def close
      @io.close
    end
  end
end

Many thanks,

MIssing gemspec & ruby minimal version

Heey,

I noticed that this gem does not set a ruby minimal required version, though does not work with ruby 2.2
Please set a minimal ruby version to make this visible, and also check in the gemspec in the git repository.

unable to use JSON Patch at {"link":{"self":{"href":"gmail.com"}},"name":"google"}

response = '{"link":{"self":{"href":"gmail.com"}},"name":"google"}';
#2 the response is the string value, we are parse string to JSON object by using JSON.parse()

Obj = JSON.parse(response);
#2 displaying Obj after parsing to JSON object.

puts Obj
#2 It displaying {"link"=>{"self"=>{"href"=>"gmail.com"}},"name"=>"google"}
#2 After that i am using hana and JSON Patch to update the name,

patch = Hana::Patch.new [
{ 'replace' => '/name', 'value' => 'gmail' }
]
Obj2 = patch.apply(Obj)
#2 displaying Obj2 after applying JSON Patch.

puts Obj2

Getting error "undefined method `strip' for nil:NilClass" when using patch.apply(Obj)
Please tell me what is the mistake here and give a solution to this problem !

Client error handling?

Per 6901, JSON pointer error handling is up to the implementation. I see that Pointer is used here primarily to implement Patch, but would it be possible to make it so that an error could be handled by the user of Hana?

Specifically, in cases where there's no value at (an intermediate) index there's a lot of room to do useful things like fill in empty objects.

Also, - to refer to "the next item of an array" raises an error, where a client implementation might want to handle that somehow. (And Patch might want to add at /list/-)

If so, I'll volunteer a PR.

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.