Repository is archived
No commit activity in last 3 years
No release in over 3 years
Makes work with JSON:API compound documents easy
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0.9
~> 3.5
 Project Readme

JSONAPI Ruby Deserializer

Makes work with JSON:API compound documents easy

Status

CircleCI  

Installation

gem 'jsonapi-ruby-deserializer'
gem install jsonapi-ruby-deserializer

Usage

Simple example:

hash = {"data"=>{"type"=>"articles", "attributes"=>{"title"=>"Lorem Ipsum"}}}
document = JSONAPI::Ruby::Deserializer::Document.new(hash)

document.data.type
# => "articles"
document.data.attributes.title
# => "Lorem Ipsum"
document.data.attributes.to_h
# => {"title"=>"Lorem Ipsum"}

Advanced example:

hash = {"meta"=>{"license"=>"MIT", "authors"=>["James Smith", "Maria Hernandez"]}, "links"=>{"self"=>"http://example.com/articles", "next"=>"http://example.com/articles?page[offset]=2", "last"=>"http://example.com/articles?page[offset]=10"}, "data"=>[{"type"=>"articles", "id"=>"1", "attributes"=>{"title"=>"JSON:API paints my bikeshed!"}, "relationships"=>{"author"=>{"links"=>{"self"=>"http://example.com/articles/1/relationships/author", "related"=>"http://example.com/articles/1/author"}, "data"=>{"type"=>"people", "id"=>"9"}}, "comments"=>{"links"=>{"self"=>"http://example.com/articles/1/relationships/comments", "related"=>"http://example.com/articles/1/comments"}, "data"=>[{"type"=>"comments", "id"=>"5"}, {"type"=>"comments", "id"=>"12"}]}}, "links"=>{"self"=>"http://example.com/articles/1"}}], "included"=>[{"type"=>"people", "id"=>"9", "attributes"=>{"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}, "links"=>{"self"=>"http://example.com/people/9"}}, {"type"=>"comments", "id"=>"5", "attributes"=>{"body"=>"First!"}, "relationships"=>{"author"=>{"data"=>{"type"=>"people", "id"=>"2"}}}, "links"=>{"self"=>"http://example.com/comments/5"}}, {"type"=>"comments", "id"=>"12", "attributes"=>{"body"=>"I like XML better"}, "relationships"=>{"author"=>{"data"=>{"type"=>"people", "id"=>"9"}}}, "links"=>{"self"=>"http://example.com/comments/12"}}]}
document = JSONAPI::Ruby::Deserializer::Document.new(hash)

Data

document.data[0].id
# => "1"

document.data[0].type
# => "articles"

Attributes

document.data[0].attributes.title
# => "JSON:API paints my bikeshed!"

document.data[0].attributes.to_h
# => {"title"=>"JSON:API paints my bikeshed!"}

document.data[0].relationships.comments.data[0].attributes.body
# => "First!"

document.data[0].relationships.comments.data[0].attributes.to_h
# => {"body"=>"First!"}

One-to-one relations

document.data[0].relationships.author.data.id
# => "9"

document.data[0].relationships.author.data.attributes.first_name
# => "Dan"

document.data[0].relationships.author.data.attributes.to_h
# => {"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}

One-to-many relations

document.data[0].relationships.comments.data[0].id
# => "5"

document.data[0].relationships.comments.data[0].attributes.body
# => "First!"

document.data[0].relationships.comments.data[0].attributes.to_h
# => {"body"=>"First!"}

Nested relations

document.data[0].relationships.comments.data[1].relationships.author.data.id
# => "9"

document.data[0].relationships.comments.data[1].relationships.author.data.attributes.first_name
# => "Dan"

document.data[0].relationships.comments.data[1].relationships.author.data.attributes.to_h
# => {"first_name"=>"Dan", "last_name"=>"Gebhardt", "twitter"=>"dgeb"}

List relationships

document.data[0].relationships.to_a
# => ["author", "comments"]

Meta

document.meta.authors
# => ["James Smith", "Maria Hernandez"]

document.meta.to_h
# => {"license"=>"MIT", "authors"=>["James Smith", "Maria Hernandez"]}

Links

document.links.self
# => "http://example.com/articles"

document.data[0].relationships.author.links.self
# => "http://example.com/articles/1/relationships/author"

document.data[0].relationships.author.data.links.self
# => "http://example.com/people/9"

document.data[0].relationships.author.data.links.to_h
# => {"self"=>"http://example.com/people/9"}

jsonapi

document.jsonapi.version
# => "1.0"

document.jsonapi.to_h
# => {"version"=>"1.0"}

Dynamic resources

document.data[0].attributes.dynamic_key = 'dynamic_value'
document.data[0].attributes.dynamic_key
# => "dynamic_value"

License

jsonapi-ruby-deserializer is released under the MIT License.