クラスがあります
/**
* If stub == true data == length of the binary data
* If stub == false data == binary data
*/
case class Attachment(contentType: String,
stub: Boolean,
data: Either[Int, Array[Byte]])
そして、私はそれを書き込もうとしてFormat
います:
implicit val attachmentFormat = new Format[Attachment] {
def reads(json: JsValue): JsResult[Attachment] = (
"content_type".read[String] ~
"stub".read[Boolean] ~
/// ??? How do i validate data based on the value of stub
)(Attachment.apply)
def writes(o: Attachment): JsValue = obj(
"content_type" -> toJson(o.contentType),
"stub" -> toJson(o.stub),
"data" -> o.data.fold(
length => toJson(length),
bytes => toJson(new String(Base64.encode(bytes)))
)
)
}
の条件付き書き込みはできますが、 の値に基づいて読み取り時data
に条件付きで検証する方法がわかりません。data
stub