Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JSON API in the Ruby world

JSON API in the Ruby world

Slides from my first talk ever, given at the first edition of BalkanRuby conference in Sofia, Bulgaria will give answers to questions like such: What is json:api? What gems could you use in your Rails API in order to meet the json:api specification? What are the pros and cons of Active Model Serializers (AMS) and Fast JSON API gems?

Gabriela Luhova

May 26, 2018
Tweet

More Decks by Gabriela Luhova

Other Decks in Programming

Transcript

  1. { "type": "articles", "id": "1", "attributes": { "title": "Rails is

    Omakase" }, "relationships": { "author": { "links": { "self": "/articles/1/relationships/author", "related": "/articles/1/author" }, "data": { "type": "people", "id": "9" } } } }
  2. { "data": [{ "type": "articles", "id": "1", "attributes": { "title":

    "JSON API paints my bikeshed!" }, "links": { "self": "http://example.com/articles/1" }, "relationships": { "author": { "links": { "self": "http://example.com/articles/1/relationships/author", "related": "http://example.com/articles/1/author" }, "data": { "type": "people", "id": "9" } } } }], "included": [{ "type": "people", "id": "9", "attributes": { "first-name": "Dan", "last-name": "Gebhardt", "twitter": "dgeb" }, "links": { "self": "http://example.com/people/9" } }] }
  3. • You have a resource which is an ActiveRecord/ActiveModel object.

    • You create the ActiveModel::Serializer for it. • Every time you render it as JSON, the serializer will be used.
  4. {"data":{"id":"1", "type":"customers", "attributes":{"email":"[email protected]"}, "relationships":{"orders":{"data":[{"id":"1","type":"orders"}]}}}, "included":[{"id":"1", "type":"orders", "attributes":{"payment-type":"cash", "status":"finished", "shipping-address":"from the

    seller's office"}, "relationships":{"warranty-policies": {"data":[{"id":"1", "type ":"warranty-policies"}]}}}, {"id":"1", "type":"warranty-policies", "attributes":{"model-type-serialized":"Laptops", "serial-number":"H7N0CV09406574G", "period":24}}]}
  5. @customer = Customer.find params[:id] options = {} options[:include] = [:orders,

    :'orders.warranty_policies'] serialized_customer = CustomerSerializer.new(@customer, options) .serialized_json render json: serialized_customer
  6. {"data":{"id":"1", "type":"customers", "attributes":{"email":"[email protected]"}, "relationships":{"orders":{"data":[{"id":"1","type":"orders"}]}}}, "included":[{"id":"1", "type":"orders", "attributes":{"payment-type":"cash", "status":"finished", "shipping-address":"from the

    seller's office"}, "relationships":{"warranty-policies": {"data":[{"id":"1", "type ":"warranty-policies"}]}}}, {"id":"1", "type":"warranty-policies", "attributes":{"model-type-serialized":"Laptops", "serial-number":"H7N0CV09406574G", "period":24}}]}
  7. class CustomerSerializer include FastJsonapi::ObjectSerializer set_type :customer # optional cache_options enabled:

    true, cache_length: 12.hours attributes :id, :email, :name, :phone has_many :orders end
  8. AMS • Support of multiple adapters • Compound documents •

    Sparse fields • Caching issues • Handles homogenous and not homogenous lists Fast JSON API • Only JSON API specification support • Compound documents • No sparse fields implementation • Better performance of collection serialization • Aimed at serialization of homogenous list of objects