在 Argonaut 中,如何在案例类包含 Either 的情况下轻松重命名相应的 JSON 属性名称。
例如,给定这个定义:
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")
将 a 转换FooBar
为类似 JSON 的FooBar(Right(Bar("hello"))).asJson.spaces4
结果如下:
{
"e" : {
"Right" : {
"b" : "hello"
}
}
}
在上面的输出中将“Right”重命名为更有意义的最简单方法是什么?(我的实际场景有很多案例类,其中有很多,所以我正在寻找最简洁的方法。)