googleads
JSON Serialization In Rails | Hire Ruby On Rails Developers
preloder
Ruby on Rails Web Development

JSON Serialization In Rails: A Complete Guide

 

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format that encodes the object into a string. JSON is completely language independent. It is used in Java, JavaScript, Perl, Python, Ruby, etc. Such data representation can easily be translated between server and client(Browser) but also between server and server. Serialization is a process that transforms an object into that string.

JSON Serialization In Rails

If your rails application is used with popular Javascript frameworks and presents API that utilizes the JSON.
Basically, JSON serialization has two parts,

1) Data preparation
2) Data transformation

Data preparation

Data preparation is converting or transforming Ruby object into a hash. There are various Ruby gems available for you to implement the JSON serialization. In this tutorial, we can use the following gems for preparing JSON data. Now we can create new rails project and models.

Now we can add assosiations to author model,

Now run the following command to open rails console,

Now you can add the required data to the table,

Also Read: A Guide for Web Automation Testing With Ruby Cucumber And Watir

 

Active Model Serializers

ActiveModelSerializers used to create custom JSON format by representing serializer as a class which is inherited from ActiveModel::Serializer.
Add the gem into our Gemfile, and run bundle install

Now we can generate the serializers for our models

You could see our generated serializers in the app/serializers directory. Now add the following code into our serializers,

From rails console execute the following code to test our serializer,

 

Jbuilder

Jbuilder gem provides a simple DSL for declaring JSON structures. There is no need to add the gem into the gemfile because Jbuilder gem is defaultly available. Now we can create JSON template in app/view/articles.json.jbuilder and the add the following code.

Let’s generate the json format from our rails console without using template,

 

# With Template

 

 

JSONAPI-RB

Jsonapi-rb is an intuitive ruby library for producing and consuming JSON API documents. It is comprised of 4 independent micro-libraries: jsonapi-parser, jsonapi-renderer, jsonapi-serializable, jsonapi-deserializable.

Add the gem into our Gemfile, and run bundle install

In jaonapi-rb resources are defined by subclass of JSONAPI::Serializable::Resource. Since we have already created serializers so we just need to update our serializers now.

From rails console execute the following code to test our serializer

Don’t miss to try 10 Useful Tips & Tricks for Ruby On Rails Developers

 

ROAR

Roar (Resource-Oriented Architectures in Ruby) is a framework for parsing and rendering REST documents. Roar gives a DSL and behavior for creating hypermedia APIs.

Add the following gem into our Gemfile and bundle it.

Now we can create representer for our models under app/representers/ directory,

From Rails console executes the following code to generate JSON data,

 

Related: 10 Useful Ruby On Rails Gems We Couldn’t Live Without

 

Fast JSON API

A lightning fast JSON:API serializer for Ruby Objects.
Add the following gem into your gemfile and run bundle install,

Since we have already created serializers so we just need to update our serializers now,

From rails console execute the following code to prepare data for json,

 

JSONAPI::Serializers

JSONAPI::Serializers is a simple library for serializing Ruby objects and their relationships into the JSON:API format.

Add the following gem into our gemfile and run bundle install,

Now we can update our serializers to use JSONAPI::Serializers like following,

From rails console execute the following code to prepare data for json,

 

RABL

RABL (Ruby API Builder Language) is a ruby templating system for generating JSON.
Now add the following gem into our gemfile and run bundle install.

Create the json template under app/views

Execute the following code from rails console to prepare json data

 

JSONAPI::Resources

JSONAPI::Resources is focused on the resources served by an API, To know more about JSONAPI::Resources check here http://jsonapi-resources.com/
Add the following gem into your gemfile and run bundle install

after bundle install, use the following comment to generate the resources.

You could see our generated resources in the app/resources directory

Start executing the following code from rails console to prepare json data,

 

Data Transformation

Data transformation is the process of converting or transforming hash into JSON format. A set of rich complement built in operators and functions is provided for manipulating and combining extracted data.

JSON

Ruby provide JSON as default standard serialization library and we can directly use it in our rails console to convert the hash into a JSON format.

 

Oj

Oj is a fast JSON parser and Object marshaller as a Ruby gem. Add the following gem into your gemfile, and run bundle install.

Execute following code into rails console to convert the hash into json format,

 

YAJL

Yajl is JSON parsing and encoding directly to and from a stream or String. This will Parse and encode multiple JSON objects to and from streams or strings continuously. Add the following gem into your gemfile, and run bundle install.

Execute following code into rails console to convert the hash into json format,

On finalizing how fast is each library, i have used Ruby Benchmark to compare data preparation and transformation time for each tool to check. Here  i have used 30 articles with author and each article will have 50 comments.

S. No. Tool Name Time Taken
1Fast JSON API0.181660
2ROAR0.217807
3JSONAPI::Serializers0.273731
4JSONAPI-RB0.285836
5Active Model Serializers0.293275
6Jbuilder0.413474
7JSONAPI::Resources0.421009
8RABL0.656725

 

S.NoToll NameTime Taken
1Oj0.006654
2Yajl0.011478
3JSON0.042426

I have listed down the libraries and placed it based on its performance so how fast it is and as top it would be. On the go,  you could also see precise and collaborative result for each library along with its serving speed. On the other hand, we respect your suggestions! Run your benchmark test and share your result with us. We appreciate your valuable feedback’s and improvements.

Hope this helps you! and if you also wish to explore more about technologies then join us on the excursion of exploring Technologies subscribe us  to get more blogs & updates on latest technologies. Agira Technologies one of the fastest growing Web and Mobile Development Company in India. Precisely, extends its research on Web technologies, Mobile Apps, Blockchain, Chatbot, Artificial Intelligence, iOT and lot more. Explore more about us on www.agiratech.com.

For any inquiries reach us at info@agiratech.com

The following two tabs change content below.

Arjunan Subramani

Senior Software Engineer | Around 4 Years of experience in Web development and Testing arena. Plus, hands on expertise in Ruby, Ruby on Rails, Watir, Ruby Cucumber.

Leave a Reply

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

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call