Object Serializer with Fast JSON API in Rails

Tram Ho

What is object serializer?

Object serializer is a tool for us to convert objects, namely objects in rails, into JSON format to serve HTTP responses that return JSON data. In rails, to implement serialize objects, we can use gems like fast_jsonapi , active_model_serializers or can use the Active Model Serializer (AMS) provided by rails. But within the framework of this article, I will introduce to you gem fast_jsonapi okay

Introducing the Fast JSON API gem

Fast JSON API provides almost every function that the Active Model Serializer provides. However, the advantage of Fast JSON API lies in speed and performance, even up to 25 times in time compared to Active Model Serializer . We can see through the following example:

Through the above example, we can see how Fast JSON API superior

Features of Fast JSON API

Basically, Fast JSON API will provide us with the following main functions:

  • The declaration syntax is similar to Active Model Serializer
  • Supports relations belongs_to , has_many , has_one
  • Support for compound documents
  • Caching

Setting

To install the Fast JSON API, we add this line to the gem file and then run bundle install :

After installation is complete, we can use the features of the Fast JSON API  Let’s find out!

Use

We can use the bundled generator command

This command will create in your project a serializer located in app/serializers/movie_serializer.rb

Model Definition

Serializer definition

This is the most important part. We will define how an object will be serialized:

Let’s see the results through the following example

We will create an object of the Movie class as follows:

We will then serialize this object:

Return hash

Return JSON

The result will produce the following output:

Attributes

Attributes are defined in Fast JSON API via method attributes . This method also has an alias as an attribute , which will help our code more clearly when defining a single attribute.

By default, the attributes of the object serializer are defined by taking attributes of the same name from the object in the Model. For example, the Movie class above, attributes such as name , year , .. will also be transferred directly to MovieSerializer :

With the Fast JSON API , we can define our own attributes besides the object’s existing attributes as follows:

By implementing additional attribute :name_with_year , object serializer will have additional attribute :name_with_year as we have defined above.

In addition, we can override the default attributes (attributes taken directly from the Model object):

Attributes can be used under a different name by passing the original method or accessor with the shortcut proc :

Collection serialization

Besides serializing an independent object like the examples we have shown above, we can also serialize a collection of objects. The declaration syntax is quite similar:

We can pass an is_collection option to more clearly manage the serialization of an individual collection or single object. By default, if we do not pass this option, the Fast JSON API will understand and distinguish when it is passed into a collection and when it is passed to a single object. However, Fast JSON API is not always able to understand and distinguish as we wish. That’s why the Fast JSON API has given us options for managing serialize collections or objects more easily:

The is_colletion options are:

  • nil or not: The Fast JSON API will automatically understand if the resource is a collection or a single object (However, there will be some limitations as I wrote above).
  • true : will always treat the resource passed as a collection
  • false : will always treat the resource passed as a single object

Params

In some cases, the serializer object attributes can carry more information than the object attributes in the model. Passing params will help us handle different cases more easily. It’s a bit abstract and confusing, let’s take a look at the following example ?

In the above example, we will pass params[:current_user] into the attribute :can_view_early , this attribute will return true if params[:current_user].is_employee? is true, and returns false otherwise. That’s why the value of the can_view_early attribute will depend on the params we passed.

Conditional Relationship

Conditional Relationship can be defined by passing a Proc via the if: keyword. Relationship will be serialized if Proc returns true and vice versa, will not be serialized if Proc returns false , similarly we can pass in both the record the params . Let’s take a look at the following example:

Epilogue

Above, I have shared the most basic knowledge enough that we can apply the Fast JSON API in our project, to learn more, you can check out the reference link to below. Good luck! https://github.com/Netflix/fast_jsonapi

Share the news now

Source : Viblo