|
| 1 | +# JsonPathAttribute |
| 2 | + |
| 3 | +JsonPathAttribute is a simple but powerful object mapper to map JSON or Hash data into Ruby objects, using [JsonPath](https://github.com/joshbuddy/jsonpath). |
| 4 | + |
| 5 | +Although this gem has been released fairly recently, it was originally developed for [Beequip](http://beequip.com) and a very similar version has been used for a few years. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Given a JSON (or Ruby hash) like this: |
| 10 | + |
| 11 | +```json |
| 12 | +{ |
| 13 | + "post": { |
| 14 | + "title": "How to drive on snow?", |
| 15 | + "body": "Just use a low gear and slowly build up speed", |
| 16 | + "likes": 12, |
| 17 | + "comments": [ |
| 18 | + { |
| 19 | + "body": "Thank you for the tip! It is very useful.", |
| 20 | + "user": { |
| 21 | + "name": "Charles Careful" |
| 22 | + } |
| 23 | + } |
| 24 | + ] |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +You can include `JsonPathAttribute` to a Ruby class: |
| 30 | + |
| 31 | +```ruby |
| 32 | +class Post |
| 33 | + include JsonPathAttribute |
| 34 | + |
| 35 | + json_path_attribute :title, path: 'post.title' |
| 36 | + json_path_attribute :likes, path: 'post.likes', type: :integer |
| 37 | + json_path_attribute :comments, path: 'post.comments[*]', type: Comment, array: true |
| 38 | +end |
| 39 | + |
| 40 | +class Comment |
| 41 | + include JsonPathAttribute |
| 42 | + |
| 43 | + json_path_attribute :body, path: 'body' |
| 44 | + json_path_attribute :name, path: 'user.name' |
| 45 | +end |
| 46 | +``` |
| 47 | + |
| 48 | +And it gets parsed like this |
| 49 | + |
| 50 | +```ruby |
| 51 | +post = Post.parse(json) # => <Post:...> |
| 52 | +post.title # => "How to drive on snow?" |
| 53 | +post.body # => "Just use a low gear and slowly build up speed" |
| 54 | +post.likes # => 12 |
| 55 | +post.comments # => [#<Comment:0x000000011e4354d0 @body="Thank you for the tip! It is very useful.", @name="Charles Careful">] |
| 56 | +``` |
| 57 | + |
| 58 | +## Installation |
| 59 | + |
| 60 | +Install the gem and add to the application's Gemfile by executing: |
| 61 | + |
| 62 | +```bash |
| 63 | +bundle add json_path_attribute |
| 64 | +``` |
| 65 | + |
| 66 | +If bundler is not being used to manage dependencies, install the gem by executing: |
| 67 | + |
| 68 | +```bash |
| 69 | +gem install json_path_attribute |
| 70 | +``` |
| 71 | + |
| 72 | +## Development |
| 73 | + |
| 74 | +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. |
| 75 | + |
| 76 | +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). |
| 77 | + |
| 78 | +## Contributing |
| 79 | + |
| 80 | +Bug reports and pull requests are welcome on GitHub at https://github.com/BEEQUIP/json_path_attribute. |
| 81 | + |
| 82 | +## Acknowledgements |
| 83 | + |
| 84 | +Thanks to the original authors [jdongelmans](https://github.com/jdongelmans) and [jandintel](https://github.com/jandintel). |
| 85 | + |
| 86 | +Also thanks to [joshbuddy](https://github.com/joshbuddy) for creating [JsonPath](https://github.com/joshbuddy/jsonpath). |
0 commit comments