Fast JSON API Serializer vs AMS

A Quickstart Guide for Serializer in Rails — Fast JSON API vs Active Model Serializer

Gitanjali Mule

--

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)

Let’s start with What is Serialization? Well , It’s a process of converting one data structure into another which can be stored or transmitted over the network. examples of serialization format includes JSON(JavaScript Object Notation), XML and YAML.

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.

Traditional JSON rendering method

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

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 times faster than Active Model Serializers on up to current benchmark of 1000 records.”

Fast_jsonapi has 4.9k+ stars on Github while AMS has 5k+

Benchmark times for 250 records for fast_jsonapi and active_model_serializer

Demo App

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
invoke active_record
create db/migrate/20200324000135_create_notes.rb
create app/models/note.rb
invoke controller
create app/controllers/notes_controller.rb
invoke resource_route
route resources :notes

migrate the database:

rails db:migrate

Now add gem into Gemfile:

adding gem into Gemfile

Save Gemfile and run terminal command:

bundle install

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:

Model note.rb
Controller notes_controller.rb
routes.rb
note_serializer.rb

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.

NotesController

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.

I have used Postman to make the request to my local server. https://www.postman.com/

Here is the comparison of data received using fast_jsonapi and traditional json method:

Traditional Method:

traditional json method

By using fast_jsonapi method:

Fast JSON API data

Conslusion:

  1. 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.
  2. Also it gives additional information regarding the type of data which is actually the name of the model.
  3. All the attributes are put into attributes object which will systematic and well organized.
  4. 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

Happy Coding

--

--

Gitanjali Mule

Data Analyst |Python | R | Tableau | Web Application Developer | 10k+ views