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

Output null for None values #64

Open
d6y opened this issue May 15, 2019 · 2 comments
Open

Output null for None values #64

d6y opened this issue May 15, 2019 · 2 comments

Comments

@d6y
Copy link

d6y commented May 15, 2019

Hello 👋

For a case class with an optional value set to None:

case class C(a: Int, z: Option[Int])
val c = C(1, None)

...is it possible to customise derive to preserve the None values in the JSON?

E.g., to get:

{  "a": 1, "z": null }

This is possibly related to the Play note on Customize the macro to output null (hmm, if that link doesn't take you to the right section, scroll to the bottom of the page).

@julienrf
Copy link
Owner

Hey, the way it is currently implemented is not configurable: https://github.com/julienrf/play-json-derived-codecs/blob/master/library/src/main/scala/julienrf/json/derived/DerivedOWrites.scala#L37-L44.

Maybe it is possible to do what you want by introducing a custom implicit rule and playing with the priorities to get it right!

Otherwise, I’m happy to merge a PR making this behaviour configurable (like we currently do with the NameAdapter and TypeTagOWrites)!

@d6y
Copy link
Author

d6y commented May 16, 2019

Thank you @julienrf

Looks like I can implement a local scope version of owritesLabelledHListOpt and replace...

case None => Map.empty

...with...

case None => Map(adaptedName -> JsNull)

...to get the behaviour I need in this case 🍻

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

2 participants