Most popular JSON libraries for Scala have the ability to serialize and deserialize to case classes.
Unfortunately, until Scala 2.11 is released, there is a restriction on the number of parameters a case class can have (22 maximum). As a workaround to go over this limit, it is possible to use regular classes instead. (for example: How can I deserialize from JSON with Scala using *non-case* classes?).
However, this loses the benefits of case classes. For example, there is no automatically-generated copy constructor, and lenses don't work with regular classes, so manipulating the structure becomes very cumbersome (unless one makes every field in the class a var
, giving up on the benefits of immutability).
Is there a way to make regular classes behave more like case classes so that, for example, lenses would also work on them?
If you're using lenses anyway, just nest your case classes. You'll have more potential for reuse of data, and the main reason not to nest is to avoid monstrosities like
which are (largely) solved if you use lenses. JSON can handle nested classes.
It seems to be that, by defining a copy function (unfortunately by hand), regular classes can work with lenses, as Travis mentioned in his comment to the question, above.
Below is a proof of concept that works (using json4s and a copy of an old Scalaz lens implementation, borrowed from Daniel Sobral's answer to Cleaner way to update nested structures):