fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Custom serialization of scalar values

Open sandstrom opened this issue 7 years ago • 3 comments

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

sandstrom avatar Dec 12 '18 17:12 sandstrom

This would be really useful!

jnfbatista avatar Jan 16 '19 11:01 jnfbatista

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

sivalingams avatar Nov 26 '19 10:11 sivalingams

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, [email protected] 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 .

kapilnarula avatar Nov 26 '19 10:11 kapilnarula