Developers Club geek daily blog

3 years, 7 months ago
Short overview of ways of forming of JSON

Today in Rails there are next ways of serialization of objects in JSON:
  1. to_json call () directly.
  2. RABL
  3. Active model serializers
  4. JBuilder

The first way is ideally suited for situations when it is not required to serialize the enclosed objects.
@posts.to_json(:include => [...], :methods => [...])

However, creating JSON the answer with the list of models, there is need to serialize the connected models in the necessary look.

From personal experience, is the most organically fitting into Rails-development, included by default in Rails 4, JBuilder.
json.extract! message, :id, :content, :chat_id
json.user do
    json.cache!(["user_", message.user_id]) do
      json.partial! 'users/user', user: message.user

RABL, in comparison with other gems, is represented ancient because of syntax and the monster, slow because of poor performance.
collection :@messages
attributes :id, :content, :chat_id
child(:user) { attributes :name }
node(:read) { |message| message.read_by?(@user) }

ActiveModel:: Serializer
Active model serializers suggests to describe JSON in the form of ruby of classes, without the thought-up syntax that reduces zoo of use of different DSL in the project.

Solution of problems with productivity

Small comparison

Test data: 1000 typical messages with the sender and additional fields.
id: 1,
content: "Lorem",
chat_id: 1,
created_at: "2014-06-18T21:47:49.363Z",
kind: 0,
formatted_content: "<span>Lorem</span>",
user: {
    id: 1,
    name: "Ivan Petrov",
    image: ""
files: [ ],
links: [ ]

to_json: 0.2ms
Oj: 0.1ms
JBuilder: 129.0ms
JBuilder with OJ: 88.0ms

Numbers are average, but the overall picture is clear. It does not make sense to compare different gems among themselves as the difference is not essential, but objectively, JBuilder is the fastest today.
It is necessary to pay for convenience in speed. All described gems on speed are lost to the to_json method () several times.
Use of JBuilder without setup will cost to you ~ 600.0ms

There are some soposob to accelerate JBuilder:

  • To connect gems: oj, oj_mimic_json, multi_json.
    To add to initialization MultiJson.use(:oj)
    That will replace the standard serializer with faster Oj.
  • Not to use partials which significantly are slowing down JBuilder. Long ago issues in which, despite the closed status, this problem is actually not solved is created
  • To cache templates, using the cache method which is built in JBuilder!

In the Staply project (almost open beta) we used JBuilder, only at the initial stage of development, gradually passing to forming of JSON manually by means of to_json (). That is olichny demonstration of all concept of Rails providing quite rapid and easy development at the beginning with optimization at the end.

This article is a translation of the original post at
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here:

We believe that the knowledge, which is available at the most popular Russian IT blog, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.

comments powered by Disqus