Grape: Does Grape care about fast_jsonapi?

Created on 1 Feb 2018  路  12Comments  路  Source: ruby-grape/grape

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

chore you can help

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!

All 12 comments

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.

  • A PR is definitely possible if we want Grape to actively support fast_jsonapi. I guess it would be mostly a matter of managing dependencies afterwards
  • If not a PR, a dedicated gem making the bridge between Grape and fast_jsonapi could be another interesting option, like the gem grape-jsonapi-resources. The dependencies management matter remains the same though.
  • We can also think of enriching the SerializableHash 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_serializer Doesn'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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Legogris picture Legogris  路  6Comments

mcfoton picture mcfoton  路  6Comments

jereynolds picture jereynolds  路  5Comments

nab0310 picture nab0310  路  4Comments

dblock picture dblock  路  7Comments