Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jbuilder vs rails-api/active_model_serializers for JSON handling in Rails 4

I have started to begin with Rails 4.

While working with handling of JSON format data, I found we can use rails/jbuilder and works well.

However, When I was taking Codeschool's Rails 4 Pattern, they mentioned gem called active_model_serializers.

While for active_model_serializers gem, all logic of JSON serialization goes into Model(which is considered as best practices).

whereas for jbuilder gem, we need to write separate view file with extension .json.jbuilder.

My questions are:

  • Which one is ideal for JSON data handling
  • Any performance difference between two
like image 263
brg Avatar asked Sep 29 '14 10:09

brg


1 Answers

It depends on your preference and needs.

If you are working with Ember.js front-end, I'd lean towards active_model_serializers since Ember.js was basically crafted to work well with it (Yehuda Katz is one of the maintainers of active_model_serializers and is on the core team for Ember.js; he gave a talk on the topic a while back).

Quick breakdown:

Active Model Serializers

Separates the serialization concern into its own folder /app/serializers, comes with its own Rails generator, and it behaves more like ActiveRecord in that you can define associations in the serializer. Then it'll do the right things automatically based on its opinionated conventions (e.g. camel casing, side-loading associations... etc). Ryan Bates has an excellent RailsCast episode on the topic: http://railscasts.com/episodes/409-active-model-serializers

Jbuilder

Jbuilder takes almost the opposite approach in that it considers JSON format construction is just another Rails view. You build your responses in the corresponding /app/views/ directories just like you would with view templates. And it can take on many of the characteristics of a view template, like understanding what current_user is, out of box (this is not as straight forward with AMS), chaining relations (@user.posts)... etc. And of course, Ryan Bates also made a RailsCast on the subject: http://railscasts.com/episodes/320-jbuilder

Alternative: Rabl

Ryan Bates (naturally) made a RailsCast on Rabl as well: http://railscasts.com/episodes/322-rabl. In concept, it's a lot closer to Jbuilder than AMS. And it's also been around longer. Personally I am not very fond of its syntax. But that's a matter of opinion.


If I wasn't working on an Ember.js project, I'd go with Jbuider for its simplicity and the more approachable concept.

As for performance, at least one user claims that you can make Jbuilder a lot faster than both Rabl and AMS: https://medium.com/@lgmspb/how-we-increased-the-speed-of-json-generation-by-3000-times-ca9395ab7337


Follow up (01/22/2015): Leigh Halliday wrote a nice crash course comparing some of the gems. The article covers a couple more alternatives in addition to the ones mentioned here. https://www.leighhalliday.com/responding-with-json-in-rails

like image 54
poweratom Avatar answered Sep 23 '22 19:09

poweratom