According to json standard, keys of an object must be strings, and a String
must be wrapped with quotes.
If you want to convert a supported type, you can simply do:
case class C1(map: Map[String, Boolean]) {
val json = "map" -> map
}
val c1 = C1(Map("true" -> true, "false" -> false))
println(pretty(render(c1.json)))
The output is:
{
"map":{
"true":true,
"false":false
}
}
But if you want to convert an unsupported type, you need to make the conversion yourself. For example you can do:
case class C(map: Map[Boolean, String]) {
val json = "map" -> map.map(kvp => (kvp._1.toString, kvp._2))
}
val c = C(Map(true -> "true", false -> "false"))
println(pretty(render(c.json)))
And the output is:
{
"map":{
"true":"true",
"false":"false"
}
}
In order to do all of that you need to import:
import org.json4s.native.JsonMethods._
import org.json4s.JsonDSL._
Code run at Scastie.