How to ignore an item when generating the json string if the value is None?

1.3k Views Asked by At

I'm trying to use Argonaut to generate JSON string from a Scala instance.

import argonaut._, Argonaut._

case class Person(name: Option[String], age: Int, things: List[String])

implicit def PersonCodecJson =
  casecodec3(Person.apply, Person.unapply)("name", "age", "things")

val person = Person(Some("Freewind"), 2, List("club"))

val json: Json = person.asJson

val prettyprinted: String = json.spaces2

It will generate:

{
  "name" : "Freewind",
  "age" : 2,
  "things" : [
    "club"
  ]
}

And when the name is None:

val person = Person(None, 2, List("club"))

It will generate:

{
  "name" : null,
  "age" : 2,
  "things" : [
    "club"
  ]
}

But actually I want it to be:

{
  "age" : 2,
  "things" : [
    "club"
  ]
}

How to do it?

1

There are 1 best solutions below

0
On

Resolved, the key is to define custom EncodeJson rule and use ->?: and field.map:

implicit def PersonCodecJson: EncodeJson[Person] = EncodeJson((p: Person) =>
  p.name.map("name" := _) ->?: ("age" := p.age) ->: ("things" := p.things) ->: jEmptyObject)