Scala 2.10 seems to have broken some of the old libraries (at least for the time being) like Jerkson and lift-json.
The target usability is as follows:
case class Person(name: String, height: String, attributes: Map[String, String], friends: List[String])
//to serialize
val person = Person("Name", ....)
val json = serialize(person)
//to deserialize
val sameperson = deserialize[Person](json)
But I'm having trouble finding good existing ways of generating and deserializing Json that work with Scala 2.10.
Are there best practice ways of doing this in Scala 2.10?
Mentioning json4s that wraps jackson, lift-json or its own native implementation as a long term solution:
So, based on the absence of an error message and the incorrect sample code, I'm suspecting this is more of an issue of just not understanding how the lift-json extraction works. If I've misunderstood, do comment and let me know. So, if I'm right then here's what you need.
To serialize:
Then to reverse the process you'd do something like:
If that's not the part you're having trouble with, then do let me know and I can try to revise my answer to be more helpful.
Jackson is a Java library to process JSON fast. The Jerkson project wraps Jackson, but appears to be abandoned. I've switched to Jackson's Scala Module for serialization and deserialization to native Scala data structures.
To get it, include the following in your
build.sbt
:Then your examples will work verbatim with the following Jackson wrapper (I extracted it from jackson-module-scala test files):
Other Scala 2.10 JSON options include Twitter's scala-json based on the Programming Scala book--it's simple, at the cost of performance. There is also spray-json, which uses parboiled for parsing. Finally, Play's JSON handling looks nice, but it does not easily decouple from the Play project.
There is now a fork of Jerkson that supports Scala 2.10 at https://github.com/randhindi/jerkson.
I can heartily recommend argonaut for json support in scala. All you need to configure it to serialize your Customer object is one line:
That will pimp your class to give it an
.asJson
method which turns it into a string. It will also pimp the string class to give it a method.decodeOption[List[Customer]]
to parse strings. It handles the options in your class fine. Here is a working class with a passing test and a running main method which you can drop into a git clone of argonaut to see it all working fine:The developers are also helpful and responsive to folks getting started.