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

Simple converters for serialize and deserialize #22

Open
jgornick opened this issue Sep 17, 2018 · 2 comments
Open

Simple converters for serialize and deserialize #22

jgornick opened this issue Sep 17, 2018 · 2 comments

Comments

@jgornick
Copy link

For simple converters, creating an IPropertyConverter seems a bit overkill. It would be nice to specify a function as part of a decorator for serialization or deserialization.

For example:

@JsonProperty('wifi_signal')
@JsonDeserailizer((value) => !value ? 0 : value * 20)
public wifiSignalStrength: number
@JsonProperty('wifi_signal')
@JsonSerializer((value) => !value ? 0 : value / 20)
public wifiSignalStrength: number

Thoughts?

Cheers,

  • Joe
@edcarroll
Copy link
Owner

Hi Joe, thank you for opening this issue!

I do really like the idea actually - though I feel it would be cleaner to have it as a single decorator, as I worry 2 is a bit verbose at times.

My first thought would be something like @JsonDeserializer(v => ..., v => ...) - however this doesn't catch the case where you may only want one, in a readonly or writeonly property perhaps.

Instead, what about something like passing an object, { serializer: v => ..., deserializer: v => ... }, where each one is optional? Though again, heading into the realm of needless verbosity...

What are your thoughts here?

@jgornick
Copy link
Author

I also thought about a single decorator with a serializer and/or deserializer object property, but figured since one was going to type the word serializer and/or deserializer, then it might make more sense to split them into different decorators.

I brought it up because for my scenario, I was creating a DTO where my deserialization is different than the serialization. That's where it would be nice to provide individual decorators for serialize and deserialize.

Thoughts?

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

No branches or pull requests

2 participants