Search code examples
scalacirce

How to decode null to empty string in Circe


I have the following case class

case class Response(attributes: CsvAttributes, rows: Seq[Array[String]])

The rows are obtained from a Java library which can have null elements in the array as shown below:

[
  ["a", "b", null],
  ["c", null, "d"]
]

Response(attributes, rows).asJson.noSpaces throws error.

How to encode the null elements into empty string("")?


Solution

  • You can use a custom decoder for Response:

    implicit val decodeResponse: Decoder[Response] = (c: HCursor) =>
      for {
        attributes <- c.downField("attributes").as[CsvAttributes]
        rows       <- c.downField("rows").as[Seq[Array[Option[String]]]]
      } yield {
        Response(attributes, rows.map(_.map {
          case Some(v) => v
          case None    => ""
        }))
      }
    

    I would try to translate the response from your Java library to idiomatic Scala (e.g. translate null to Option.None).

    But in case you want to avoid this, you can overwrite the default behaviour of how circe encodes a String. This declaration would change it to represent null with an empty string:

    implicit val encodeFoo: Encoder[String] = {
      case null => Json.fromString("")
      case s    => Json.fromString(s)
    }