1

I'm looking for a good JSON library for Scala that can serialize an arbitrary object graph to JSON, without requiring me to write mapping code.

Java's Jackson does this, but it isn't native to Scala and I'm having some trouble getting it to deserialize nested generics (which I attribute to the Java type system). There's Jerkson, a Scala wrapper for Jackson, but it's been abandoned for a few years.

What do you recommend?

larspars
  • 1,640
  • 1
  • 15
  • 30
  • I think ScalaJack would help you here, https://github.com/gzoller/ScalaJack, alternatively there are a few others maintained in the list from awesome Scala repo - https://github.com/lauris/awesome-scala#json – Faktor 10 Dec 14 '15 at 23:00
  • Thanks. I just tried ScalaJack, but it doesn't seem to have automatic serialization of arbitrary objects. – larspars Dec 15 '15 at 10:53

2 Answers2

0

play-json, http://mvnrepository.com/artifact/com.typesafe.play/play-json_2.11, is a good choice. It is actually a wrapper around Jackson, but provides a lot of utilities that mitigate the serilization and deserilization. For more details, https://www.playframework.com/documentation/2.4.x/ScalaJson

In addition, it already provides reads and writes for basic data type in Scala. All you need to do is to define a format for the model in the companion object.

import play.api.libs.json.Json
implicit val format = Json.format[YourObject]

This is a detailed example, https://github.com/luongbalinh/play-mongo/blob/master/app/models/User.scala

Luong Ba Linh
  • 802
  • 5
  • 20
  • Although a fine library, play-json does *not* provide the "automatic serialization through introspection" that the OP asked for – Alvaro Carrasco Dec 14 '15 at 17:02
  • Thanks. I've tried play-json, but as Alvara pointed out it doesn't have the automatic serialization feature I was looking for. – larspars Dec 15 '15 at 10:40
0

Core Jackson does not have explicit Scala support, but Jackson Scala module:

https://github.com/FasterXML/jackson-module-scala

does support Scala datatypes.

StaxMan
  • 113,358
  • 34
  • 211
  • 239