jsoniter-scala icon indicating copy to clipboard operation
jsoniter-scala copied to clipboard

Support multiple parameter lists for case classes

Open fdietze opened this issue 2 years ago • 9 comments

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

fdietze avatar Jun 01 '22 19:06 fdietze

This would be really useful for me as well, in order to switch a circe-based application over to jsoniter.

cornerman avatar Jun 01 '22 19:06 cornerman

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)

plokhotnyuk avatar Jun 02 '22 05:06 plokhotnyuk

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 avatar Jun 02 '22 11:06 cornerman

@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.

plokhotnyuk avatar Jun 03 '22 06:06 plokhotnyuk

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? :)

cornerman avatar Jun 04 '22 12:06 cornerman

@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 or var 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 ;)

plokhotnyuk avatar Jun 05 '22 10:06 plokhotnyuk

@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 avatar Jun 06 '22 06:06 plokhotnyuk

@plokhotnyuk This is awesome. Thank you so much for the fast help :)

Will try it out!

cornerman avatar Jun 06 '22 10:06 cornerman

Thank you so much!

fdietze avatar Jun 07 '22 17:06 fdietze