I'm using Argonaut to parse JSON strings. There is a requirement: If any field is not provided, or is empty or blank, give it the string "not supplied" instead.
I have a solution, but it seems very complicated:
case class User(name: String, home: String)
implicit def UserDecodeJson: DecodeJson[User] = DecodeJson(j => for {
name <- (j --\ "name").as[BlankAsNonSupplied]
home <- (j --\ "home").as[BlankAsNonSupplied]
} yield User(name.value, home.value))
case class BlankAsNonSupplied(value: String)
implicit def BlankAsNonSuppliedDecodeJson: DecodeJson[BlankAsNonSupplied] = DecodeJson.withReattempt(a => {
val v = a.success.map(_.focus).flatMap(_.string)
.filterNot(_.trim.isEmpty)
.map(BlankAsNonSupplied.apply).getOrElse(BlankAsNonSupplied("not supplied"))
DecodeResult.ok(v)
})
You can see the BlankAsNonSuppliedDecodeJson
one is very complicated, and hard to understand. Is there any way to make it(or the whole example) simpler?