'Implicit Json Formatter for value classes in Scala

I have many value classes that make up a larger object case class.

final case class TopLevel(
  foo: Foo,
  bar: Bar
)

final case class Foo(foo: String) extends AnyVal

final case class Bar(bar: String) extends AnyVal

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

object Bar {
  implicit val format = Json.valueFormat[Bar]
}

object TopLevel {
  implicit val TopLevelFormat: OFormat[TopLevel] = Json.format[TopLevel]
}

This works fine, but when the top level case class has many arguments the construction objects soon pile up. I tried to do this implicitly, e.g.

object Format {
  implicit def jsonFormatter[A](): Format[A] = Json.valueFormat[A]
}

But I get

scala.ScalaReflectionException: <none> is not a method


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source