모험.io:이름을 바꾸는 방법 json 속성에 대한 오른쪽/왼쪽에는 경우 등을 포함하는 중
문제
에서 모험가,어떻게 하나 쉽게 이름을 바꾸는 해당 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")
변환 FooBar
JSON 아 FooBar(Right(Bar("hello"))).asJson.spaces4
는 다음과 같습니다:
{
"e" : {
"Right" : {
"b" : "hello"
}
}
}
수 있는 가장 쉬운 방법은 무엇입니까 이름을 바꾸려면"오른쪽"뭔가 더욱 의미에서 출력을 원하십니까?(나 실제 시나리오는 많은 경우 수업으로 많은 Eithers,그래서 내가 찾는 가장 간단한 방법으로 가능합니다.)
해결책
여기에는 합리적으로 간단한 방법:
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")))
)
그리고:
val fooOrBar = namedEitherCodec[Foo, Bar]("Foo", "Bar")
implicit def FooBarCodecJson: CodecJson[FooBar] = casecodec1(
FooBar.apply, FooBar.unapply
)("e")(fooOrBar, fooOrBar)
할 수 있도 fooOrBar
암시적지만,재정의 유형은 클래스 인스턴스에서는 방법으로 되는 경향이 frowned 니다.
제휴하지 않습니다 StackOverflow