CodexBloom - Programming Q&A Platform

Type Mismatch scenarios When Implementing a Custom Encoder with Circe in Scala 2.13

👀 Views: 42 💬 Answers: 1 📅 Created: 2025-08-30
scala circe json case-classes encoders Scala

I'm updating my dependencies and I need help solving I've searched everywhere and can't find a clear answer... I'm working with a type mismatch behavior while trying to create a custom encoder for a case class using Circe in Scala 2.13. My case class is defined as follows: ```scala case class User(name: String, age: Int) ``` I attempted to implement a custom encoder for this class like this: ```scala import io.circe.{Encoder, Json} implicit val userEncoder: Encoder[User] = new Encoder[User] { final def apply(user: User): Json = Json.obj( "name" -> Json.fromString(user.name), "age" -> Json.fromInt(user.age) ) } ``` However, when I try to encode an instance of `User`, I receive the following behavior: ``` type mismatch; found : User required: io.circe.Encoder[User] ``` I’ve checked my imports to ensure I'm including the necessary Circe libraries, and I also tried defining the encoder as a value instead of an implicit function, but the behavior continues. For example: ```scala val user = User("Alice", 30) val json = userEncoder(user) ``` This still throws the same type mismatch behavior. I suspect it might have something to do with how the implicits are resolved in the scope, but I need to pinpoint the scenario. Has anyone faced a similar question, or can anyone suggest what might be going wrong here? My development environment is macOS. Has anyone else encountered this? This is my first time working with Scala 3.10. What am I doing wrong? This is part of a larger REST API I'm building.