A Quickstart Guide for Serializer in Rails — Fast JSON API vs Active Model Serializer
Disclaimer: This article will serve as a QuickStart guide for using serializer in Ruby on Rails api based application. Here I am comparing Fast JSON API(Netflix) vs Active Model Serializer(AMS)
When we create a API(Application Programming Interface) based web application, We use JSON format for data exchange. It is easier and faster to store and transmit.
In Ruby on Rails application we use different serializers to convert this data into json format and customize the attributes of the models in a model serializer files according to our needs. for example, which data to send , how the data should look like, relationship of model to another (belongs_to, has_many)etc.
By default Rails has a method to convert data into json format by using json method on a variable.
But it is very difficult to customize the data that needs to be rendered.The easiest way to render JSON data into Ruby on Rails application is to use serializer.
There are number of gems to serialize data. Most popular are Active Model Serializer(AMS) , FastJSON API (fast_jsonapi), jbuilder. In this article I am going to compare AMS and fast_jsonapi and eventually develop a simple application in Fast_jsonapi.
Fast JSON API
A lightning fast JSON:API serializer for Ruby Objects. We compare serialization times with Active Model Serializer as…
As the name suggest it is light and fast compare to Active Model Serializer. Fast JSON API is developed and maintained by Netflix. Netflix has to transmit thousands of bytes over the network, So having fast serializer is very critical for them .
“We want to ensure that with every change on this library, serialization time is at least
25 timesfaster than Active Model Serializers on up to current benchmark of 1000 records.”
Fast_jsonapi has 4.9k+ stars on Github while AMS has 5k+
Let’s get started with our application. As the purpose of this article to serve as a QuickStart, I have kept the demo app simple in structure. It will have only one model for now. Basically we will be able to make Notes with attributes title and descriptions in string format.
rails g resource Note title:string description:string
It will generate the following file structure:
Running via Spring preloader in process 57455
route resources :notes
migrate the database:
Now add gem into Gemfile:
Save Gemfile and run terminal command:
Now there are two ways to generate serializer by running following rails generator terminal command:
rails g serializer Note
and then adding attributes manually into serializer file OR we can use bundled rails generator command to add attributes while generating serializers.
rails g serializer Note title:string description:string
After running this command, note_serializer.rb file is created into serializer folder in App
App > serializer > notes_serializer.rb
File structure so far is as follows:
As you can see, fast_json serializer is build over a traditional serializer. Attributes are added into the serializer file. Fast_json doesn’t have to pass :id attribute specifically, as it passes it by default.
Now let’s setup our controller to render the show and index methods for Notes. Here we will have to use special method to convert the data into json object.
As you can see on line 6, we have used a method from NoteSerializer . It is used to convert the data into serialized format as per the attributes and methods we have written in the Note_serializer.rb file. After converting it into serialized form we are rendering that json object. So when will make a request on localhost:3000/notes we will get the json object.
Here is the comparison of data received using fast_jsonapi and traditional json method:
By using fast_jsonapi method:
- As you can see it puts the actual data in data array and in hash objects which makes it easier to iterate through the array.
- Also it gives additional information regarding the type of data which is actually the name of the model.
- All the attributes are put into attributes object which will systematic and well organized.
- Using a fast and reliable serializer will boost speed of your app and improve the performance of your application overall.
I will be elaborating more advantages of using Fast JSON API in my next article post. Link will be posted soon.
If you like my article then please spare some claps for this. It will definitely motivate me to right more and also pay my bills. Thank You