对于类型io.circe.Encoder [scala.collection.immutable.Map [Int,Any]]的分散隐式扩展

时间:2018-01-22 17:37:33

标签: json scala circe

我写了这段代码

import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
Map(1 -> 1, 2 -> "a").asJson.toString

但是我收到以下错误

cmd35.sc:1: diverging implicit expansion for type 
io.circe.Encoder[scala.collection.immutable.Map[Int,Any]]
starting with method encodeMapLike in object Encoder
val res35 = Map(1 -> 1, 2 -> "a").asJson.toString
                              ^

1 个答案:

答案 0 :(得分:1)

您无法直接使用circe序列化Map[Int, Any]。如果IntString仅允许使用值,则使用Map[Int, Either[Int, String]]代替here等自定义Encoder