Fast JSON API Serializer by Netflix in Ruby On Rails

Netflix in Ruby On Rails

This tutorial will guide you about Fast JSON API Serializer. It gives you better performance as compared to Active Model Serializer.

Let’s start with a brief introduction to JSON and Serialization, and how we can use it in Rails application.

What is JSON?

JSON (Javascript Object Notation) is a text-based, human-readable data interchange format used for representing simple data structures and objects in Web browser-based code. It supports:

What is Serialization?

Serialization is the process of turning data structures into another format that can be stored or transmitted over the network.

Examples of serialization formats include JSON, XML, and YAML, so the process of turning any data type into one of these formats is called “serialization.”

Features:

Further Reading: Integrating Google Cloud Vision API with Ruby for Image Object Detection

Implementations:

Let’s create the Rails application.

$ rails new fast_json_api_serializer --database=postgresql

$ cd fast_json_api_serializer

Let’s start with Active Model Serializer and see how much time is required to serializing the data. Then, we will see how we can increase the performance with Fast JSON API.

We are going to use gem ‘active_model_serializers’. Add the following to ‘Gemfile’.

gem 'active_model_serializers'

Run the installer:

$ bundle install

$ rails g scaffold Article title content:text

This command will create the scaffold named ‘Article’ and two columns ‘title’ and ‘content’ to the article model.

$ rails g serializer article title content

This command will create an ‘article’ serializer. It adds ‘article_serializer.rb’ file in ‘app/serializers’ folder. You can see a directory structure as below:

Now our ‘article_serializer.rb’ looks like this:

Let’s have a look at ‘articles_controller.rb’ and change the ‘index’ method like below:


Let’s create some sample data. For this, open the ‘seeds.rb’ and add the following code to create the data.

Run the following command:

$ rails db:seed

Start rails server:

$ rails s

Now open the browser and fire the JSON request using localhost:3000/articles.json or you can also use the POSTMAN app.

Let’s see how much time it took for serialization –

It took 38.8ms for serialization.

Now we will go with Fast JSON API for better performance.

We are going to use gem ‘fast_jsonapi’. Add the following to ‘Gemfile’.

gem 'fast_jsonapi'

# Note: Don’t remove the gem 'active_model_serializers'.

Run the installer:

$ bundle install

Now change the ‘article_serializer.rb’ as below:

Also, change the ‘index’ method of ‘articles_controller.rb’.

Hit the same URL, http://localhost:3000/articles.json and see response time in terminal:

Voila!! Serialization time is fast compared to AMS. It took 5.9ms for serialization.

Compare Responses:

Active Model Serializer:

{

“id”:1,

“title”:”Article-1″,

“content”:”This is article number 1″

}

Fast JSON API:

{ “data”:

{ “id”:”1″,

“type”:”article”,

“attributes”:{

“title”:”Article-1″,

“content”:”This is article number 1″

}

}

As you see above, Fast JSON API exposes more information compared to Active Model Serializer.

You can find the complete code on Github here.

BoTree Technologies is a leading software development company in Ahmedabad that builds top-notch software and solutions to increase business revenue and maximize productivity. 

Click here for more details…

Consulting is free – let us help you grow!

Related posts

BigCommerce API: Accessing for Ruby on Rails application

byNishant Upadhyay
7 years ago

Super Fast VCR Configuration for RSpec and MiniTest

byAmit Patel
7 years ago

Ruby on Rails Consulting Company: Things to Check Before Hiring!

byShardul Bhatt
11 months ago
Exit mobile version