Search code examples
jsonscalaeitherargonaut

Argonaut.io: how to rename json property for Right/Left in case class containing Either


In Argonaut, how does one easily rename the corresponding JSON property name in instances where a case class contains an Either.

For example, given this definition:

  case class Foo(f: String)
  case class Bar(b: String)
  case class FooBar(e: Either[Foo, Bar])

  implicit def FooCodecJson: CodecJson[Foo] = casecodec1(Foo.apply, Foo.unapply)("f")

  implicit def BarCodecJson: CodecJson[Bar] = casecodec1(Bar.apply, Bar.unapply)("b")

  implicit def FooBarCodecJson: CodecJson[FooBar] = casecodec1(FooBar.apply, FooBar.unapply)("e")

converting a FooBar to JSON like FooBar(Right(Bar("hello"))).asJson.spaces4 results in the following:

{
    "e" : {
        "Right" : {
            "b" : "hello"
        }
    }
}

What is the easiest way to rename the "Right" to something more meaningful in the output above? (My actual scenario has many case classes with many Eithers, so I am looking for the most concise way possible.)


Solution

  • Here's a reasonably straightforward approach:

    def renameFields(replacements: (JsonField, JsonField)*)(json: Json) =
      json.withObject(obj =>
        replacements.foldLeft(obj) {
          case (acc, (before, after)) => 
            acc(before).map(v => (acc - before) + (after, v)).getOrElse(acc)
        }
      )
    
    def renamedEitherCodec[A, B](leftName: String, rightName: String)(implicit
      ee: EncodeJson[Either[A, B]],
      de: DecodeJson[Either[A, B]]
    ) = CodecJson[Either[A, B]](
      e => renameFields("Left" -> leftName, "Right" -> rightName)(ee(e)),
      c => de(c.withFocus(renameFields(leftName -> "Left", rightName -> "Right")))
    )
    

    And then:

    val fooOrBar = namedEitherCodec[Foo, Bar]("Foo", "Bar")
    
    implicit def FooBarCodecJson: CodecJson[FooBar] = casecodec1(
      FooBar.apply, FooBar.unapply
    )("e")(fooOrBar, fooOrBar)
    

    You could also make the fooOrBar implicit, but overriding type class instances in that way tends to be frowned on.