简体   繁体   中英

Why do we need Ruby Serializer libraries?

I've been experimenting with RubyOnRails API project. My project is not large, it is simply a toy project having a couple of tables with two associations. All my responses are rendered n jon format using oj gem like this render json: Oj.dump({...}), status: ok . Everything works fine.

On the other hand we have a bunch of libraries such as ActiveModelSerialization , jsonapi-rb , fast_jsonapi , etc.

My question is: why do we need these libs if we simply render json:... ? What do these libs do what render json:... can't do? Is it performance? And my second question. Could you please suggest how I can benchmark these libs in Ruby (on Rails) (best practice)?

It is simply a matter of code organization / reuse. Using libraries such as these helps keep the serialization constrained to a class which is responsible for it and therefore allows the controllers / models to remain cleaner. Notice that some libraries also provide utilities such as allowing you to choose which fields are going to get displayed, useful DSLs and caching.

You can think of it as an application of the SOLID principe "Single Responsability": don't make a class (the controller) responsible for both rendering a request and serializing the data; delegate this work to another class responsible for this (a serializer).

Ruby has a class called Benchmark which you could use to test the speed of different serialization libraries. https://github.com/evanphx/benchmark-ips may also prove useful. Lastly, you can see the output of the rails server, which contains the time to respond to the request, in ms, to figure out whether serialization is impacting the performance negatively, but it usually shouldn't.

Serialization libraries mainly do two things: preparing data for serialization and the actual serialization (converting to JSON). Some serialization gems uses OJ instead of the standard JSON library for serialization, but you can also decide to go with OJ if you want to, check this example from jsonapi-serializer gem on how to use it with OJ gem.

Data preparation Serialization gems offers a better separation of concerns and less redundant code, for example if you are sending some same data in different places you will have to write the response shape in every controller you are sending these data from. So instead you can define what (attributes, relationships, ....) in its corresponding serializer.

Another advantage of using serialization libraries, if you are working with the JSON:API format specifications which standardize how JSON responses format should look like. Some libraries like AMS offer multiple adapters to choose from, other libraries like fastjson or jsonapi-serializers works only with JSON:API format.

Serialization libraries are just ruby code, you can use inheritance or composition to construct more complex JSON responses.

Check out this article for serialization libraries comparison and benchmark.

I had a similar question when I started exploring these libraries, especially since I don't do a lot of work with rails, and I found that most JSON:API serialization libraries I have seen are really for supporting the Rails/ActiveModel style of MVC web development. For development styles that don't fall into that category, these serialization libraries begin to lose their convenience.

As someone that does more API work, but still needs to work with JSON:API, The best serialization library I have found so far is JSONAPI-SERIALIZER , which was built by the Netflix team. It is lightweight, it doesn't force Active Record and Rails integration (even though it still supports it), and it's way faster than ActiveModelSerialization and other serialization libraries.

Another lightweight, non-rails-dependent, Ruby gem that pairs really well with JSONAPI-SERIALIZER is the newer gem EASY-JSONAPI . It provides middleware for JSON:API compliance checking, a parser to work with requests, and a validator for already serialized responses. It might be useful for you if you prefer working with Oj to the other serialization libraries out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM