Mallow's Blog

JSON Serialization in Rails – PART 2

In the previous blog post we learned some basics about JSON Serialization: how it works and how Active model serializer works. So far, we’ve been discussing mostly basic concepts with examples. Let’s see the other serializer Fast JSON API in detail in this blog.

JSON API serialization is often one of the slowest parts of many well implemented Rails APIs. With use cases like infinite scroll on complex models and bulk update on index pages, you could observe degraded performance on our Rails APIs. With this being the case, the Fast JSON API has been built with their own object serialization library.

Active Model Serializer (AMS) is designed to serialize JSON in several different formats, not just JSON:API and also handle lists that are not homogenous whereas on the other hand the Fast JSON API always use JSON:API for our APIs and almost always serialize a homogenous list of objects

Fast JSON API is not a replacement for AMS. AMS is a great gem, and it does many things and is very flexible. It can still be used for non JSON:API serialization and deserialization.

Fast JSONAPI is aimed at providing all the major functionality that Active Model Serializer (AMS) provides, along with more focus on speed and performance as mentioned in the previous post by meeting a benchmark requirement of being 25 times faster than AMS.

When a model has one or more relationships, AMS begins to slow down. And on including related resources along with the primary resource, AMS slows down further.

With Fast JSONAPI, it is mentioned that performance gain is significant when the number of serialized records increases. As far as the features are concerned, it provides all the major functionalities that AMS provides

1) Declaration syntax similar to Active Model Serializer

2) Support for belongs_to, has_many and has_one

3) Support for compound documents (included)

4) Optimized serialization of compound documents

5) Caching

Syntax:

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  set_type :movie  # optional
  cache_options enabled: true, cache_length: 12.hours
  attributes :name, :year
  has_many :actors
  belongs_to :owner, record_type: :user
  belongs_to :movie_type
end

Object Serialization

Return a hash

hash = MovieSerializer.new(movie).serializable_hash

Return Serialized JSON

json_string = MovieSerializer.new(movie).serialized_json

Serialized Output

{
  "data": {
    "id": "3",
    "type": "movie",
    "attributes": {
      "name": "test movie",
      "year": null
    },
    "relationships": {
      "actors": {
        "data": [
          {
            "id": "1",
            "type": "actor"
          },
          {
            "id": "2",
            "type": "actor"
          }
        ]
      },
      "owner": {
        "data": {
          "id": "3",
          "type": "user"
        }
      }
    }
  }
}

The block syntax can also be used to override the property on the object:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attribute :name do |object|
    "#{object.name} Part 2"
  end
end

So, Fast JSON API feels like better solution to use with Ruby on Rails. The response standard is great and saves time. Another reason why I prefer using Fast JSON API over AMS is that AMS’s latest update has been about three years ago.

– Logesh M,
ROR Team Lead,
Mallow Technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *