Using Lift, I'm trying to "extract" (get a case class representation of) my JSON.
val json: JValue = getJson()
case class BigObj(name: String, age: Int, ...)
json.extract[BigObj]
When using more than 22 arguments, I get a JVM run-time exception that case classes cannot exceed 22 arguments.
How can I work around this limit?
As has been mentioned, you can't overcome that limit with a case class explicitly, however you can use the extract method without a case class. See this example, which I ran through REPL to verify:
And then going back from the class to JSON
Overriding the
toString
wasn't necessary - I just needed to see what data was being added. However, since this is not acase class
, you will lose some of the automatic methods that come with it. So, if you need to put it in an HTTP Session, you may want to implementSerializable
. For pattern matching, you'd need anunapply
method, etc...You should also be able to use
formats
to specify your own additional rules for parsing and extracting data from your JSON.There's no getting around the 22-limit with case classes, tuples, etc. It's a limitation of Scala. However, if you can deal with having a regular class, then you can certainly do this. You just won't get the matching,
equals
,hashCode
, etc. for free.