play-json-derived-codecs icon indicating copy to clipboard operation
play-json-derived-codecs copied to clipboard

full auto derivation

Open kailuowang opened this issue 7 years ago • 2 comments

As of now, if you have a nested tree of case classes for example

case class A(a: String)
case class B(a: Option[A])

You would have to write a derived instance for each class. In kittens, @joroKr21 developed a technique (with some facilities he added to shapeless 2.3.3), we can achieve full auto derivation. Using the same technique, a single import will provide Format instant for all case classes, while still taking a lower priority than any other existing instances. In the example above, this technique generate Format instances for B and A but respect the existing instance for Option.

It's very easy to implement here with very few code change. (mostly just need to replace your Lazy[Writes[H]] with a Writes[H] OrElse DerivedOWrites[H] in here

If you are interested I can submit a PR. Let me know.

kailuowang avatar Feb 02 '18 16:02 kailuowang

I’m not a big fan of full auto derivation to be honest. I think it’s better to explicitly show what is going to be serialized (ie every type X that has an implicit Format[X] in its companion). Also, are the results of full derivation cached by the compiler? I’m afraid this can cause compile time issues.

That being said, if people want this feature I’m happy to support it under a different import than the current “semi auto” derivation.

What do you think?

julienrf avatar Feb 02 '18 16:02 julienrf

Being able to know explicitly what are being serialized is definitely useful. At kittens we also provide "semi auto" derivation alone side the full auto one. The full auto one can be cached using shapeless.Cached (I didn't realize it until you mentioned, so I am adding it to kittens, Thanks!).

For my current use case, I can live with either way. And it seems that you don't have a use case for full auto. I guess we can defer this to whoever really needs an full auto derivation ( I can imagine some user may need it for an external big ADT that they don't want to be too coupled with. )

kailuowang avatar Feb 02 '18 18:02 kailuowang