How to skip over empty arrays in the generated Jso

2019-07-24 21:25发布

问题:

//Domain

case class Item(price: Int)
case class Data(name: String, items: Vector[Item])

Data("stackoverflow", Vector(Item(100))).asJson

//ouput:

{
 "name": "stackoverflow",
 "items": [
  {
    "price": 100
  }
 ]
}

// With Empty items:
Data("stackoverflow", Vector()).asJson

// expected output:
{
 "name": "stackoverflow",
 "items": null // Can be removed with Printer.noSpaces.copy(dropNullValues = true)
}

I tried doing something like:

  implicit val itemsEncoder: Encoder[Vector[Item]] = (items: Vector[Item]) => {
    if (items.nonEmpty) items.asJson else Json.Null
  }

And this results in a StackoverflowError.

Goal: If there are no elements in the array, there shouldn't be any empty array "items": [] in the output. How can I achieve this?

A possible alternative is to encode Option[Vector[Item]] like so:

implicit val optionalVector: Encoder[Option[Vector[Item]]] = {
 case Some(v) =>
  if(v.isEmpty) Json.Null else v.asJson
 case None => Json.Null
}

I don't like this solution since it forces an Option type on the domain objects just to generate the Json.

回答1:

Try this instead,

  implicit val itemsEncoder: Encoder[Vector[Item]] = (items: Vector[Item]) => {
    if (items.nonEmpty) items.asJson(Encoder.encodeVector) else Json.Null
  }

where you are telling it to use the standard Vector encoder instead of itemsEncoder again.



标签: scala circe