Argonaut is a great library. It's by far the best JSON library for Scala, and the best JSON library on the JVM. If you're doing anything with JSON in Scala, you should be using Argonaut.
Conclusion: 2021 The obvious elephant in the room is that java and the json libraries got faster. Like way faster then back when the first benchmarks were run. It is also obvious that GSON stepped up big and won both benchmarks for Big and small files. In both cases very clearly.
Jackson. Jackson is a multi-purpose Java library for processing JSON data format.
Unfortunately writing a JSON library is the Scala community's version of coding a todo list app.
There are quite a variety of alternatives. I list them in no particular order, with notes:
💣 = has not fixed security vulnerabilities, § = has Scalaz integration, ± = supports interop with Jackson JsonNode
In Snowplow we use json4s with the Jackson back-end; we've had good experiences with Argonaut too.
Lift-json is at version 2.6 and it works really well (and is also very well supported, the maintainer is always ready to fix any bugs users may find. You can find examples using it on the github repository
The maintainer (Joni Freeman) is always reachable on the Lift mailing list. There are also other users on the mailing list who are very helpful as well.
As @Alexey points out, if you want to use the library with other Scala version, say 2.11.x
, change scalaVersion
and use %%
as follows:
scalaVersion := "2.11.5"
"net.liftweb" %% "lift-json" % "2.6"
You can check the liftweb.net site to find out the latest version as time goes by.
I suggest using jerkson, it supports most basic type conversions:
scala> import com.codahale.jerkson.Json._
scala> val l = List(
Map( "id" -> 1, "name" -> "John" ),
Map( "id" -> 2, "name" -> "Dani")
)
scala> generate( l )
res1: String = [{"id":1,"name":"John"},{"id":2,"name":"Dani"}]
Number 7 on the list is Jackson, not using Jerkson. It has support for Scala objects, (case classes etc).
Below is an example of how I use it.
object MyJacksonMapper extends JacksonMapper
val jsonString = MyJacksonMapper.serializeJson(myObject)
val myNewObject = MyJacksonMapper.deserializeJson[MyCaseClass](jsonString)
This makes it very simple. In addition is the XmlSerializer and support for JAXB Annotations is very handy.
This blog post describes it's use with JAXB Annotations and the Play Framework.
http://krasserm.blogspot.co.uk/2012/02/using-jaxb-for-xml-and-json-apis-in.html
Here is my current JacksonMapper.
trait JacksonMapper {
def jsonSerializer = {
val m = new ObjectMapper()
m.registerModule(DefaultScalaModule)
m
}
def xmlSerializer = {
val m = new XmlMapper()
m.registerModule(DefaultScalaModule)
m
}
def deserializeJson[T: Manifest](value: String): T = jsonSerializer.readValue(value, typeReference[T])
def serializeJson(value: Any) = jsonSerializer.writerWithDefaultPrettyPrinter().writeValueAsString(value)
def deserializeXml[T: Manifest](value: String): T = xmlSerializer.readValue(value, typeReference[T])
def serializeXml(value: Any) = xmlSerializer.writeValueAsString(value)
private[this] def typeReference[T: Manifest] = new TypeReference[T] {
override def getType = typeFromManifest(manifest[T])
}
private[this] def typeFromManifest(m: Manifest[_]): Type = {
if (m.typeArguments.isEmpty) { m.erasure }
else new ParameterizedType {
def getRawType = m.erasure
def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
def getOwnerType = null
}
}
}
Maybe I've late a bit, but you really should try to use json library from play framework. You could look at documentation. In current 2.1.1 release you could not separately use it without whole play 2, so dependency will looks like this:
val typesaferepo = "TypeSafe Repo" at "http://repo.typesafe.com/typesafe/releases"
val play2 = "play" %% "play" % "2.1.1"
It will bring you whole play framework with all stuff on board.
But as I know guys from Typesafe have a plan to separate it in 2.2 release. So, there is standalone play-json from 2.2-snapshot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With