Question

QUESTION:

I do this with my controller:

class Api::Product::V1::LicenseController < ApplicationController

Why can't I do this with my serializer? (or can I?)

class Api::Product::V1::LicenseSerializer < ActiveModel::Serializer

CONTEXT:

I have multiple controllers/routes that correspond to a single model.

And I need to have multiple serializers per model that correspond 1-to-1 with my controllers.

ActiveModel::Serializers allow you to specify a serializer from a controller like this:

render :json => @license_token, :serializer => LicenseSerializer

So why can't I also do this?

render :json => @license_token, :serializer => Api::Product::V1::LicenseSerializer

I am trying to avoid the ugliness of compound names like these, even though I know they will work:

  • ProductAlphaLicenseSerializer
  • ProductBravoLicenseSerializer
  • ProductCharlieLicenseSerializer

Each of my models supports multiple APIs, which is why I want to namespace the serializers. Each model is used differently by each corresponding serializer.

Was it helpful?

Solution

It might help if you explain the error (if any) that you get. I had a similar concern regarding Active Model Serializers, and according to the documentation it appears AMS will only perform automatic serializer lookup in the app/serializers path based on the model class, so namespaced controllers have no bearing on the serializer lookup.

There doesn't appear to be anything that prevents you from specifying any serializer class you want manually, in fact from within your namespaced controller the use of LicenseSerializer should be looking for the namespaced class in the module Api::Product::V1 by default. Have you tried organising your serializers under a proper namespace so that rails class loading will resolve them automatically? eg, put Api::Product::V1::LicenceSerializer in app/serializers/api/product/v1/license_serializer.rb ?

You may also want to look at roar-rails gem which integrates with rails and uses the ruby web framework agnostic ROAR gem which supports two way JSON/XML/JSON+HAL handling using a representer pattern. Be aware that you won't get jbuilder/jsonify like control over the serialization, but if you're looking at AMS I'm guessing you want to be elevated from the detail somewhat. Using ROAR you will get a uniform API based on the representer format you choose and be much closer to a true hypermedia API.

Some of the rationale for the representer/ROAR approach here, here and here.

EDIT: You may also want to consider my to_json implementation. Performance and flexibility with all the current JSON serializer libraries was a significant issue in my project. After experimenting with all the alternatives I ended up developing a clean JSON DSL and collaborating with the Oj author to develop a highly performant string-buffer/stream marshalling API. My to_json gem easily serializes 18,000 complex objects per second on budget hosting servers and has no limitations on the JSON structures that can be generated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top