A quick tour of JSON libraries in Scala A quick tour of JSON libraries in Scala Scala JSON libraries Great, now which one to pick?

Update (18.11.2015): added spray-json-shapeless library
Update (06.11.15): added circe library

Some time ago I wrote a post on relational database access in Scala since I was looking for a library and there were many of them available, making it hard to make a choice. It turns out that the situation is similar if not worse when it comes to JSON libraries in Scala.

There are just plenty of them. You have no idea. (me neither until I wrote this post)

The following is an attempt to provide a quick overview at how a subset of the libraries I found does a few of the most common things one would likely need to do in regard to JSON:

  • parsing it from a raw string
  • browsing the AST
  • building an AST
  • mapping to a case class

There are of course plenty more valid use-cases but this post is not going to cover those.

Let’s get started!

Scala JSON libraries

play-json

The Play Framework comes with a JSON library that covers most of the common use-cases one would encounter when building web applications:

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
_
_
 
}
 
)
}
 

Browsing the AST

 
 
1
2
3
]
world
 

Building a JSON AST

 
 
1
2
3
)
}
 

Mapping to a case class

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
)
Model
 
]
81116d
 
)
)
 
get
)
 
)
 
}
 

lift-json

lift-json is the JSON library of the Lift framework. It is one of the oldest one out there if I’m not mistaken.

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
_
_
 
}
 
)
)
 

Browsing the AST

 
 
1
2
3
)
 

Building a JSON AST

 
 
1
2
3
4
5
6
_
_
 
)
)
 

Mapping to a case class

 
 
1
2
3
4
5
6
7
8
9
10
11
12
{
{
_
DefaultFormats
 
)
 
)
}
}
 

spray-json

spray rols its own JSON library that focuses on working with case classes:

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
10
11
12
_
_
 
_
_
 
}
 
parseJson
}
 

Browsing the AST

No can do?

Building a JSON AST

No can do?

Mapping to a case class

 
 
1
2
3
4
5
6
7
8
9
)
Model
 
)
7bc880f8
 
]
)
 

spray-json-shapeless

spray-json-shapeless derives spray-json’s JsonFormat-s automatically using shapeless (no need to define an implicit JsonFormat

Mapping to a case class

 
 
1
2
3
4
5
6
7
8
9
10
11
12
_
_
 
_
_
 
)
Model
 
toJson
}
 

This is quite useful, it removes the boilerplate formats hanging around

json4s

json4s is a bit like slf4j in the sense that it tries to unite all kind of rogue libraries serving the same purpose by providing a common interface. But not every library uses it, which means that chances are high that your project will contain json4s in addition to another (few) Scala JSON libraries. Hopefully it will one day succeed with its slogan “One AST to rule them all”.

json4s has its roots in lift-json so this will look familiar:

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
10
11
12
_
_
 
_
_
 
}
 
)
)
 

Browsing the AST

 
 
1
2
3
)
 

Building a JSON AST tree

 
 
1
2
3
)
)
 

Mapping to a case class

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
{
{
_
_
DefaultFormats
 
)
 
)
}
}
 

argonaut

Argonaut promotes “purely functional JSON in Scala”. It uses scalaz under the hood and

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
_
_
_
 
_
_
_
 
}
 
parseOption
)
 

Browsing the AST

There are several mechanisms available, let’s use a lense. Those are funky:

 
 
1
2
3
4
5
6
jBoolPL
8c894ab
 
)
)
 

Building a JSON AST tree

 
 
1
2
3
jEmptyObject
}
 

Mapping to a case class

 
 
1
2
3
4
5
6
7
8
9
)
Model
 
)
]
 
]
)
 

circe

Circe is a fork of Argonaut that uses cats instead of scalaz and uses shapeless to generate codecs.

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
      
        
      
      
        
        
        
      
        
        
      
        
        
        
      
      
      
}
 

Browsing the AST

So far there’s only support for cursors which let you move around the JSON tree and do changes if you would like, not for Lenses yet:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cursor      
        
      
      
        
        
        
      
      
      
      
        
      
      
      
      
)
 

Mapping to a case class hierarchy

I didn’t get this to work with the example below. The example in the documentation works though so here is that one:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
Foo      
Foo        
Foo      
Bar        
Foo        
Qux        
        
        
noSpaces      
        
      
)
 

sphere-json

The sphere-json library focuses on providing an easy way to get de/serializers for entire families of case classes. This is really useful when working with any kind of protocol-related system. I am using it in a CQRS system where commands and events are travelling back and forth between the server and a Javascript UI. Instead of having to define a codec for each one of my case classes, I simply have them extend a common abstract class and let the library take care of the rest. Watch for yourselves:

Mapping to a case class hierarchy

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
_
_
 
{
 
Message
Message
Message
 
}
{
_
 
 
]
 
(
,
)
)
 
{
)
)
}
 
}
 

jawn

jawn is a library that focuses on speed. It defines a lightweight AST and is compatible with all kind of other ASTs that we have seen here.

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
10
11
12
_
_
 
_
_
 
}
 
get
}
 

rapture

rapture’s json library is the ultimate Scala JSON library. It doesn’t really do anything with JSON itself, instead, it abstracts over the following JSON libraries (which it callsbackends):

  • Argonaut
  • Jackson
  • Jawn
  • JSON4S
  • Lift
  • Play
  • Scala standard library JSON
  • Spray

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
_
 
_
_
 
)
}
 

Browsing the AST

Rapture is using Scala’s Dynamic trait, which makes this fun:

 
 
1
2
3
]
world
 

Building a JSON AST tree

 
 
1
2
3
}
 

The Scala standard library

Up until recently I did not know that Scala had a JSON utility in its standard library. But here it is!

Parsing raw JSON

 
 
1
2
3
4
5
6
7
8
9
10
_
_
 
}
 
)
details
)
 

Browsing the “AST”

 
 
1
2
3
]
world
 

Even more!

I am forgetting a ton of libraries here I am sure. That, and I am tired and my glass ofUigeadail is getting empty.

So let me mention a few more:

Great, now which one to pick?

A quick tour of JSON libraries in Scala
A quick tour of JSON libraries in Scala
Scala JSON libraries
Great, now which one to pick?

Honestly I don’t have any good advice here. These days I am sticking to play-json and sphere-json, but that’s for no particular reason other than these libraries being there and doing what I need to do (parse and write JSON, traverse and some rare times transform the AST, bind to case classes, make it possible to support custom Java types). If play-json had support for hierarchies out of the box I would probably not have even looked for anything else.

Because for all the joy there seems to be in implementing JSON libraries in Scala, one thing has to be said: JSON de/serialization is boring. It’s this annoying thing that you have to do in order to get your application to talk to another computerized system, period.

I have never met a developer who told me how much pleasure they derived from turning classes into JSON strings and back. That’s just not a thing.

I have, however, met more than one developer that has run into trouble getting library X to cover one of the simple use-cases outlined above. Believe me, there is nothing more frustrating than having to spend time on the annoying task of setting up JSON de/serialization in order to do the boring thing of tossing strings back and forth the network. That’s time you will never get back.

Good night, and good luck.

A quick tour of JSON libraries in Scala
A quick tour of JSON libraries in Scala
Scala JSON libraries
Great, now which one to pick?
原文