-
Notifications
You must be signed in to change notification settings - Fork 34
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
Adding support for using plain Writes
/Format
as custom implicits.
#79
Conversation
When using custom formats with the the `nested` format it's now possible to use any `Writes`/`Format`. The `flat` format still requires an `OWrites`/`OFormat` type. Partially addresses julienrf#76.
Hey @ncreep thank you for the PR! I think your solution is rather elegant, implementation-wise. However, I’m a bit worried by all the things we need to take care of to correctly provide custom formats that work as expected, and by the complexity the PR adds to the current design (which is already quite complex, IMHO). |
Seeing how I'm responsible for introducing the weird behavior with user-provided formats, I felt responsible to try and do something about it. I can see your point about the complexity this adds, but the edge cases with mismatched/ignored formats are rather unpleasant. The alternative solution that I can see, was the one I suggested in #76, lifting the restriction on Or, worst case for my purposes, is to completely remove support for custom formats this way, and revert to the behavior before I started breaking things... |
If I understand correctly, we would not be able to detect, at compile-time, that someone tries to use a I prefer what we have in this PR, if that’s the case.
I think it’s a valuable feature, I wouldn’t drop it like that. I just wanted to take some time to brainstorm a little bit more about possible alternative solutions… |
I'm not aware of a technique to detect the sealed trait Foo
case class Bar(x: Int) extends Foo
object Bar { implicit val format: Format[Bar] = Json.valueFormat }
object Foo { implicit val format: Format[Foo] = derived.flat[Foo]() } Then the resulting JSON for { "type": "Bar", "value": 42 } Where the |
Interesting, I didn’t think about that idea. I think that could be a good compromise that would reduce the complexity in play-json-derived-codecs, and offer a reasonable behavior (it needs to be properly documented, though). Would you be interested in changing to that? |
Yes, I will try to come up with a new pull request in the coming days. Thanks |
Opened another pull request (#81) |
When using custom formats with the the
nested
format it's now possible to use anyWrites
/Format
.The
flat
format still requires anOWrites
/OFormat
type.Partially addresses #76.
This change required duplicating the content of the
withTypeTag
object, so that now we havewithTypeTag.flat
. Hopefully, this change won't be noticed by most users (as usingwithTypeTag
with non-default arguments is probably rare).