Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

14 Jan 2016

Using Gson to serialize Scala objects

gson is a pretty nice library that converts Java objects into JSON and back. When using this library from Scala things become a bit harder (eg: Plenty of people have difficulties when their scala object has an (im)mutable Map or List).

Here is an example to convert a JSON object to a Map[String,String]:

import com.google.gson.Gson
import scala.collection.JavaConversions._

val mapJson = "{ 'a': 'b', 'c': 'd' }"
val map = new Gson().fromJson(mapJson, classOf[java.util.Map[String, String]])

Now that we know that this works, we hide the java types in the constructor and expose a nicer scala type via a method:

case class Dummy(private val settings: java.util.Map[String, String]) {
	def getSettings = settings.toMap
}

val dummyJson = "{ 'settings' : { 'a': 'b', 'c': 'd' } }"
val dummy = new Gson().fromJson(dummyJson, classOf[Dummy])

case class Dummy2(private val options: java.util.List[String]) {
	def getOptions = options.toList
}

val dummy2Json = "{ 'options' : [ 'a', 'b', 'c', 'd' ] }"
val dummy2 = new Gson().fromJson(dummy2Json, classOf[Dummy2])

Edit: One could simply use lift-json instead and get pretty good scala support for free.