Skip to content

Custom serialization of scalar values #361

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

Open
sandstrom opened this issue Dec 12, 2018 · 3 comments
Open

Custom serialization of scalar values #361

sandstrom opened this issue Dec 12, 2018 · 3 comments

Comments

@sandstrom
Copy link

sandstrom commented Dec 12, 2018

It would be nice if we could wire up the serializer to always transform certain scalar values a certain way. For example, all instances of Time may be transformed/serialized in a special way. Same with instances of Money or Duration.

For example, something like this:

# application_serializer.rb
class ApplicationSerializer
  include FastJsonapi::ObjectSerializer

  value_transformer Time do |value|
    value.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
  end

  value_transformer Money do |value|
    "#{value.currency}-#{value.amount}"
  end

end

(obviously this can also be solved by repeating attribute blocks over and over again in every serializer where the value object occur, but the whole idea with serializers is to standardise behaviour, also when it comes to scalar serialization)

Let me know what you think! 😄

Some related issues: #49, #67, #135

@jnfbatista
Copy link

This would be really useful!

@sivalingams
Copy link

Similarly I need all datetime fields to be formated in Timestamp. Any suggestion?

@kapilnarula
Copy link

kapilnarula commented Nov 26, 2019 via email

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

No branches or pull requests

4 participants