Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #to_hash conversion protocol to object serializer #89

Merged
merged 1 commit into from
Feb 22, 2018

Conversation

iamvery
Copy link
Contributor

@iamvery iamvery commented Feb 19, 2018

Problem

In order to use a serializer with Rails, you have to explicitly call serialize_hash, e.g.

render json: WidgetSerializer.new(widget).serialized_hash

Solution

While not a big problem at all, Rails does use the to_hash protocol under the hood in it's as_json core extension to Object. By implementing this interface you can conveniently pass a serializer instance directly to Rails' render method.

render json: WidgetSerializer.new(widget)

This may have already been considered and explicitly omitted from the library, but I didn't see any past issues mentioning it, so I thought I'd propose the change 🙂

I'm happy to discuss more and make changes as needed. I considered adding a test, but it seemed relatively low value, and I wasn't sure of the ideal location for such a test. Thanks for the wonderfully useful little library! ❤️💛💚💙

@guilleiguaran
Copy link
Contributor

guilleiguaran commented Feb 19, 2018

Agree with this!!1

btw, I'm preparing a proposal to have a better integration with ActionController, this will allow using:

render @widget

and/or:

render jsonapi: @widget

I'll open an issue with the details of my proposal later today.

@shishirmk shishirmk changed the base branch from master to dev February 21, 2018 05:02
@guilleiguaran
Copy link
Contributor

@iamvery can you rebase this against dev branch?

right now this has some extra commits

@iamvery iamvery force-pushed the hash-conversion-protocol branch from fc8de04 to 06f6bea Compare February 21, 2018 20:57
@iamvery
Copy link
Contributor Author

iamvery commented Feb 21, 2018

You bet! Sorry I didn't realize the project used a dev branch 😇

@shishirmk
Copy link
Collaborator

@iamvery thank you very much for rebasing

@shishirmk shishirmk merged commit 95c136f into Netflix:dev Feb 22, 2018
@iamvery iamvery deleted the hash-conversion-protocol branch February 22, 2018 16:57
@iamvery
Copy link
Contributor Author

iamvery commented Feb 22, 2018

You bet! Glad you found it useful 💫

@corinnekunze
Copy link
Contributor

@shishirmk Any timeline on a release with this fix soon? This will help folks that want to use as_json on their serializer to get a json hash instead of a serialized json string. Like:

MovieSerializer.new(movie).as_json

This is common for passing data into React/Rails components. Then we don't have to bother with serializing/deserializing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants