38

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
Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60

1 Answers1

58

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

Community
  • 1
  • 1
poweratom
  • 2,270
  • 23
  • 25
  • 1
    JBuilder has had examples in its documentation where a 'builder' object is constructed inside an ActiveRecord::Model... you can't use any of the url helpers, though. This goes to as far back as 2013, when jbuilder was 1.0. – Kurt Mueller Aug 04 '15 at 23:34
  • 1
    Interestingly, another blog post finds AMS ~10x faster than Jbuilder for some cases: https://kirillplatonov.com/2014/11/04/active_model_serializer_vs_jbuilder/ – Halil Özgür Aug 09 '15 at 19:33
  • in personal usage i have found AMS faster at rendering content, this on a development box. Jbuilder IMO has more flexibility but requires a lot of coding to get running. AMS has a faster spin up time. The main issue i have with AMS is the lack of documentation for 0.10.0rc4. The referenced docs appear to be for 0.9.0 and have not been updated. – Tom T Mar 28 '16 at 12:13
  • 2
    I totally agree with "It depends on your preference and needs." I've written an updated review of the various JSON serialization options which you might find helpful. [Which JSON Serializer To Use For A New Rails API?](http://www.carlosramireziii.com/which-json-serializer-to-use-for-a-new-rails-api.html?utm_source=stackoverflow) – Carlos Ramirez III Oct 11 '17 at 23:45