I recently started to learn Scala. I need an EASY way to SERIALIZE my data in Json. Below I will give an example of data. I tried several popular libraries: GSON, Play JSON, Circe, but did not get the result! GSON does not know how to work with Option (eg Option [String]), in Play JSON and Circe they need to describe serialization and deserialization of an abstract class (I don’t know how to get around this).
// Test data
abstract class Base(id: Long, descr: Option[String])
case class A(id: Long, descr: Option[String], a: Option[Long]) extends Base(id, descr)
case class B(id: Long, descr: Option[String], b: Option[Double]) extends Base(id, descr)
case class Data(id: Long, user: Option[String], data: Option[Array[Base]])
val test: Array[Data] = Array(
Data(1, Some("Qqq"), None),
Data(2, None, None),
Data(3, Some("Zzz"), Some(
Array(
A(1, Some("a1"), Some(111)),
A(2, Some("a2"), None),
A(3, None, Some(333)),
B(4, Some("b1"), Some(444.444)),
B(5, Some("b2"), None),
B(6, None, Some(666.666))
)
)) )
Using circe:
import cats.syntax.functor._
import io.circe.{Decoder, Encoder}, io.circe.generic.auto._
import io.circe.syntax._
implicit val encodeBase: Encoder[Base] = Encoder.instance {
case foo @ A(_, _, _) => foo.asJson
case bar @ B(_, _, _) => bar.asJson
}
And then you can just do
test.asJson