Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diverging implicit expansion for type io.circe.Encoder[scala.collection.immutable.Map[Int,Any]]

Tags:

json

scala

circe

I wrote this code

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

but I get the following error

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
                              ^
like image 703
Knows Not Much Avatar asked Jan 27 '26 12:01

Knows Not Much


1 Answers

You cannot serialize Map[Int, Any] directly with circe. If Int or String are only allowed for values then use Map[Int, Either[Int, String]] instead with a custom Encoder like here.

like image 187
Andriy Plokhotnyuk Avatar answered Jan 29 '26 02:01

Andriy Plokhotnyuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!