People want to know what they need to do to enable https://github.com/Netflix/fast_jsonapi on their Grape projects. Or do they at all? Grape uses whatever JSON provider is available and maybe all the gains are in the object serialization part and not in pure json generation. Potentially this issue belongs in other presenter libraries.
https://medium.com/netflix-techblog/fast-json-api-serialization-with-ruby-on-rails-7c06578ad17f
I'm using Grape for many projects of mine. To answer this issue, I personally ended up enriching the SerializableHash formatter by creating a custom formatter, available in this gist.
For a given object, if this object is a model instance or a model collection, it basically seeks for the corresponding serializer according to the Rails ActiveModel::Serializer conventions.
Hope this helps. Although it certainly can be improved a lot, but for now, it does the trick for me :)
Very interesting @EmCousin. Do you think there's something here that can be turned into a generally useful PR into Grape?
@dblock I see three options.
fast_jsonapi. I guess it would be mostly a matter of managing dependencies afterwardsSerializableHash formatter by making it looking for serializers, whether the serializers use fast_jsonapi or not. It would assume that a serializer instance always responds to serializable_hash.What's the Grape project's spirit on that matter?
I didn't even know about https://github.com/cdunn/grape-jsonapi-resources! Isn't that what we want?
All three sound like good ideas, you should try to make a PR and we can talk over code.
@dblock I ended up creating a dedicated gem, available here, At least dependencies management will be done in one place only!
This is my first gem, so I'm open to suggestions for improving it!
Good work. At the very least I would add it to https://github.com/ruby-grape/ruby-grape.github.io (with the other one you mention), please. I'll open issues in the gem itself.
@EmCousin struggling to figure out how to tell grape to use a different serializer than the object type passed to render, is there way to specify the serializer to use?
render @user, serializer: :custom_compact_user_serializer
Doesn't seem like there is any obvious way short of subclassing my object
looking here
https://github.com/EmCousin/grape_fast_jsonapi/blob/master/lib/grape_fast_jsonapi/formatter.rb#L40
@EmCousin struggling to figure out how to tell grape to use a different serializer than the object type passed to render, is there way to specify the serializer to use?
render @user, serializer: :custom_compact_user_serializerDoesn't seem like there is any obvious way short of subclassing my object
@rromanchuk Not by passing a serializer option. But if your api renders json data and you wish to use a different serializer, you can do this:
jsonapi_options = {} # use whatever available fastjson_api options you like
render CustomCompactUserSerializer.new(@user, jsonapi_options).serialized_json
This way you may keep the convention over configuration system, and can use the above method for the exceptions you might need at some point.
@EmCousin 馃憣 thanks, seems embarrassingly obvious now.
@dblock Is this issue still worth being opened?
Let's close
Most helpful comment
@dblock I ended up creating a dedicated gem, available here, At least dependencies management will be done in one place only!
This is my first gem, so I'm open to suggestions for improving it!