JSON API VANILLA
Deserialize JSON API formats into vanilla Ruby objects. The simplest JSON API library at all altitudes above Earth centre.
# gem install json-api-vanilla
require "json-api-vanilla"
json = IO.read("articles.json") # From http://jsonapi.org
doc = JSON::Api::Vanilla.parse(json)
doc.data[0].comments[1]..last_name # "Gebhardt"
Compare with jsonapi:
# gem install jsonapi --pre
require "jsonapi"
json = IO.read("articles.json")
doc = JSONAPI.parse(json)
comment_ref = doc.data[0].relationships.comments.data[1]
comment = doc.included.select do |obj|
obj.type == comment_ref.type && obj.id == comment_ref.id
end[0]
= comment.relationships..data
= doc.included.select do |obj|
obj.type == .type && obj.id == .id
end[0]
.attributes['last-name']
Documentation
JSON::Api::Vanilla.parse(json_string) returns a document with the following
fields:
datais an object corresponding to the JSON API's data object.errorsis an array containing errors. Each error is a Hash.linksis a Hash from objects (obtained fromdata) to their links, as a Hash.rel_linksis a Hash from objects' relationships (obtained fromdata) to the links defined in that relationship, as a Hash.metais a Hash from objects to their meta information (a Hash).find('type', 'id')returns the object with that type and that id.find_all('type')returns an Array of all objects with that type.keysis a Hash from objects to a Hash from their original field names (non-snake_case'd) to the corresponding object.
License
Copyright © Trainline.com Limited. All rights reserved. See LICENSE.txt in the project root for license information.