Netflix / fast_jsonapi

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.
Apache License 2.0
5.07k stars 426 forks source link

Custom serialization of scalar values #361

Open sandstrom opened 5 years ago

sandstrom commented 5 years ago

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: https://github.com/Netflix/fast_jsonapi/pull/49, https://github.com/Netflix/fast_jsonapi/pull/67, https://github.com/Netflix/fast_jsonapi/issues/135

jnfbatista commented 5 years ago

This would be really useful!

sivalingams commented 4 years ago

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

kapilnarula commented 4 years ago

It seems Netflix has abandoned this project. The community created a new fork to continue supporting this project. Please refer

https://github.com/fast-jsonapi/fast_jsonapi

On Tue, 26 Nov, 2019, 3:35 PM Sivalingam, notifications@github.com wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Netflix/fast_jsonapi/issues/361?email_source=notifications&email_token=ACEAHZKAGRY6KDDINV5IPETQVTYGDA5CNFSM4GKAQ4O2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFFONUA#issuecomment-558556880, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACEAHZJHJ5TYD6SD5PNNKBTQVTYGDANCNFSM4GKAQ4OQ .