//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.