Last active
September 12, 2018 08:28
-
-
Save jeroenr/6230685 to your computer and use it in GitHub Desktop.
Easy JSON (un)marshalling in Scala with Jackson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper} | |
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper | |
import com.fasterxml.jackson.module.scala.DefaultScalaModule | |
object JsonUtil { | |
val mapper = new ObjectMapper with ScalaObjectMapper | |
mapper.registerModule(DefaultScalaModule) | |
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | |
def toJson(value: Map[Symbol, Any]): String = { | |
toJson(value map { case (k,v) => k.name -> v}) | |
} | |
def toJson(value: Any): String = { | |
mapper.writeValueAsString(value) | |
} | |
def toMap[V](json:String)(implicit m: Manifest[V]) = fromJson[Map[String,V]](json) | |
def fromJson[T](json: String)(implicit m : Manifest[T]): T = { | |
mapper.readValue[T](json) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MarshallableImplicits { | |
implicit class Unmarshallable(unMarshallMe: String) { | |
def toMap: Map[String,Any] = JsonUtil.toMap(unMarshallMe) | |
def toMapOf[V]()(implicit m: Manifest[V]): Map[String,V] = JsonUtil.toMapOf[V](unMarshallMe) | |
def fromJson[T]()(implicit m: Manifest[T]): T = JsonUtil.fromJson[T](unMarshallMe) | |
} | |
implicit class Marshallable[T](marshallMe: T) { | |
def toJson: String = JsonUtil.toJson(marshallMe) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import utils.MarshallableImplicits._ | |
case class Person(name:String, age: Int) | |
val jeroen = Person("Jeroen", 26) | |
val jeroenJson = jeroen.toJson | |
// jeroenJson: String = {"name":"Jeroen","age":26} | |
val jeroenMap = jeroenJson.toMap | |
// jeroenMap: Map[String,Any] = Map(name -> Jeroen, age -> 26) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* (Un)marshalling a simple map | |
*/ | |
val originalMap = Map("a" -> List(1,2), "b" -> List(3,4,5), "c" -> List()) | |
val json = JsonUtil.toJson(originalMap) | |
// json: String = {"a":[1,2],"b":[3,4,5],"c":[]} | |
val map = JsonUtil.toMap[Seq[Int]](json) | |
// map: Map[String,Seq[Int]] = Map(a -> List(1, 2), b -> List(3, 4, 5), c -> List()) | |
/* | |
* Unmarshalling to a specific type of Map | |
*/ | |
val mutableSymbolMap = JsonUtil.fromJson[collection.mutable.Map[Symbol,Seq[Int]]](json) | |
// mutableSymbolMap: scala.collection.mutable.Map[Symbol,Seq[Int]] = Map('b -> List(3, 4, 5), 'a -> List(1, 2), 'c -> List()) | |
/* | |
* (Un)marshalling nested case classes | |
*/ | |
case class Person(name: String, age: Int) | |
case class Group(name: String, persons: Seq[Person], leader: Person) | |
val jeroen = Person("Jeroen", 26) | |
val martin = Person("Martin", 54) | |
val originalGroup = Group("Scala ppl", Seq(jeroen,martin), martin) | |
// originalGroup: Group = Group(Scala ppl,List(Person(Jeroen,26), Person(Martin,54)),Person(Martin,54)) | |
val groupJson = JsonUtil.toJson(originalGroup) | |
// groupJson: String = {"name":"Scala ppl","persons":[{"name":"Jeroen","age":26},{"name":"Martin","age":54}],"leader":{"name":"Martin","age":54}} | |
val group = JsonUtil.fromJson[Group](groupJson) | |
// group: Group = Group(Scala ppl,List(Person(Jeroen,26), Person(Martin,54)),Person(Martin,54)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to do this:
def deSymbolizeKeys(map: Map[Symbol,Any]):Map[String,Any] = map mapKeys(_.name)