Skip to content

Unable to create abstraction for Json.Format #1137

@enzeart

Description

@enzeart

I'm trying to create an abstraction to remove some repetitive code from my project. I have some nested case classes for which I am created an OFormat and JsonConfiguration in their companion objects. For my first attempt, I try this:

import play.api.libs.json.{Json, JsonConfiguration, OFormat}
import play.api.libs.json.JsonNaming.SnakeCase
 
trait VariableImplicits1[A] {

  implicit val jsonConfiguration: JsonConfiguration = JsonConfiguration(SnakeCase)

  implicit val jsonFormat: OFormat[A] = Json.format[A]
}

This results in the following error:

play.sbt.PlayExceptions$CompilationException: Compilation error[exception during macro expansion: 
scala.ScalaReflectionException: type A is not a class

I then try the following after looking at the source code for Json.format. This works, but the jsonConfiguration isn't reflected in the output. Is there a way to make this approach work, or am I trying to do something that isn't supported by the library?

import play.api.libs.json.{Json, JsonConfiguration, OFormat}
import play.api.libs.json.JsonNaming.SnakeCase
import scala.language.experimental.macros

trait TerraformVariableImplicits1[A] {

 implicit val jsonConfiguration: JsonConfiguration = JsonConfiguration(SnakeCase)

 implicit def jsonFormat: OFormat[A] = macro JsMacroImpl.implicitConfigFormatImpl[A]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions