How to skip over empty arrays in the generated Json with Circe?

纵饮孤独 提交于 2019-12-11 12:43:58

问题


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



来源:https://stackoverflow.com/questions/56029870/how-to-skip-over-empty-arrays-in-the-generated-json-with-circe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!