0

I have a case class like this:

case class TotalTest(f1: Option[String], f2: Option[String], f3: String)

Using Gson, I am trying to write the contents into a Json file, here's what I get:


val test1 = (new Gson).toJson(TotalTest(Some("a"), Some("b"), "c"))
val pw1 = new PrintWriter(new File("test1.json" ))
pw1.write(test1)
pw1.close

The contents would be:

{"f1":{},"f2":{},"f3":"c"}

Why would I lose the first two fields? Basically I have a highly nested object of a case class with a lots of Options and am wondering an easy way to extract the results.

Related question

ahoosh
  • 1,118
  • 3
  • 15
  • 28

1 Answers1

0

Thanks to this answer we can use Circe:

import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._

val totalTest = TotalTest(Some("a"), Some("b"), "c")
val pw1 = new PrintWriter(new File("test1.json" ))
pw1.write(totalTest.asJson.noSpaces)
pw1.close

ahoosh
  • 1,118
  • 3
  • 15
  • 28