scala could not find implicit value for parameter encoder: io.circe.Encoder[scala.collection.immutable.Map[String,java.io.Serializable]] ).asJson)

Viewed 35

I am trying to encode a Map to Json using io.cierce

My Json data is like

{
   "key" : "123-456" ,
   "message" : "test msg",
   "payload" : { "hello" : "world" }
  }

And Json conversion

Map(
      "key" -> root.key.string.getOption(data).getOrElse(""),,
      "message" -> root.message.string.getOption(data).getOrElse(""),
      "payload" -> root.payload.json.getOption(data).getOrElse(Json.Null)
    ) asJson

But this is resulting could not find implicit value for parameter encoder: io.circe.Encoder[scala.collection.immutable.Map[String,java.io.Serializable]] ) asJson)

I was under the impression import io.circe.generic.auto._ should take care of most of the encoding

1 Answers

This works:

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

scala> Map("key" -> "hello".asJson, "boolean" -> true.asJson, "number" -> 123.asJson).asJson.spaces4 
res1: String =
{
    "key" : "hello",
    "boolean" : true,
    "number" : 123
}

You need to avoid Map[String, Any] and use the appropriate implicits to get the automatic conversion you need.

Related