Home > Software engineering >  No Json serializer found for type Seq[Foo]
No Json serializer found for type Seq[Foo]

Time:07-28

I have a class and a companion object defined as follows:

case class Foo(
  bar: String,
  baz: String,
)

object Foo {
  implicit val format = Json.format[Foo]
}

And when trying to compile this code, I get an error:

val example: Seq[Foo] = Seq()
val json = Json.toJson(example)
No Json serializer found for type Seq[Foo]. Try to implement an implicit Writes or Format for this type.

As far as I can tell, I have an implicit Format. What am I doing wrong?

CodePudding user response:

Your implicit val format = Json.format[Foo] is not a Format[Foo].

Change that line in the companion to: implicit val format: Format[Foo] = Json.format[Foo] - then the compiler should tell you what is wrong.

  • Related