jsoniter-scala
jsoniter-scala copied to clipboard
Support multiple parameter lists for case classes
I'd like to derive a codec for
case class A(x:Int)(y:Int)
implicit val jsonCodec: JsonValueCodec[A] = JsonCodecMaker.make
but multiple parameter lists are not supported:
[error] 'A' hasn't a primary constructor with one parameter list. Please consider using a custom implicitly accessible codec for this type.
[error] L197: implicit val jsonCodec: JsonValueCodec[A] = JsonCodecMaker.make
This would be really useful for me as well, in order to switch a circe-based application over to jsoniter.
It cannot be done for arbitrary case classes with multiple parameter lists because some of them can have default values that depend on parameters of previous list:
case class A(x: Int)(y: Int = x + 2)(z: Int = x * y)
I think, it would be totally okay to reject certain cases like that if needed. Though, I am just wondering, why the above example would be a problem.
@cornerman The problem is that default values are pre-calculated before parsing of JSON key-value pairs into some internal variables. I don't know technology how they can be injected in that kind of dependent default value definitions.
Having and ability to check presence of dependent default values would try to implement support of multiple parameter list constructors.
Thanks for the explaination. Makes sense!
Would it be possible to support multiple parameter lists then for the other cases? Can i help somewhere to implement it? :)
@fdietze @cornerman Here is a draft that supports primary constructors with multi-parameter lists: https://github.com/plokhotnyuk/jsoniter-scala/pull/905
Limitations for definitions are:
-
val
orvar
for parameters in non-first parameter lists - no default values in non-first parameter lists
For sure your support and any help are welcome! Including GitHub stars and sponsoring ;)
@fdietze @cornerman Please peek a release with an initial support of multiple parameter lists in primary constructors: https://github.com/plokhotnyuk/jsoniter-scala/releases/tag/v2.13.29
Next steps will be refactoring of calculation of default values during parsing and adding support of default values for non-first parameter lists.
@plokhotnyuk This is awesome. Thank you so much for the fast help :)
Will try it out!
Thank you so much!