ɭɘ ɖɵʊɒɼɖ 江戸 ɭɘ ɖɵʊɒɼɖ 江戸 - 5 months ago 47
Scala Question

I need advice on Play's Json and elegant Option handling in the Writes trait

I have a "style" or "effective scala" type of question here:
I have a "FeatureCheck" class which I need to serialize into a Json in Play framework.

case class FeatureCheck(val result: Option[Boolean], val missing: Option[Array[String]], val error: Option[String])

I am serializing it using my own "writes" but my code looks a lot like Java code. I would like to serialize each option in the check object only if it is defined (the final object should not have any empty values).

def writes(check: FeatureCheck): JsValue = {
val builder = Seq.newBuilder[(String, JsValue)]
if (check.error.isDefined) {
builder += "error" -> JsString(check.error.get)
if (check.missing.isDefined) {
builder += "missing" -> Json.toJson(check.missing.get)
if (check.result.isDefined) {
builder += "result" -> JsBoolean(check.result.get)

So I was wondering if there was a way to do this without those ugly if-then, or even removing the builder for the sequence.

Thanks for any help or comment given.


Let's say I want to just send result = true I want the resulting Json to be:


and NOT

"result": true,
"error": null,
"missing": []

Answer Source

Given that you can simply append an option to a seq (see Add to list if value is not null), you can do what you want rather elegantly:

type JsField = (String, JsValue)
def writes(check: FeatureCheck): JsValue = {
    Seq[JsField]() ++
    check.error.map("error" -> JsString(_)) ++
    check.missing.map("missing" -> Json.toJson(_)) ++
    check.result.map("result" -> JsBoolean(_))    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download