akka http中json响应的circe序列化问题

toe95027  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(121)

在可以是类型***int***或类型***List[String]***的字段中出现序列化问题,该字段的每个元素都是始终具有2个元素的列表,其中第一个元素的值字段始终是列表[ String],而第二个元素的值字段始终是int类型;问题出在值字段中,并且由于此行为而无法推断它。
身体:

json响应:

{
  "custom_fields": [
    {
      "id": "d0c016df-e09a-492e-a7a2-cc92e1993627",
      "name": "Sprint",
      "type": "labels",
      "date_created": "1586927852599",
      "hide_from_guests": false,
      "value": [
        "64e19188-8440-4b35-8459-d49348c92e55"
      ],
      "required": false
    },
    {
      "id": "4513e77f-9866-4139-bcc5-458945db0deb",
      "name": "Story Points",
      "type": "drop_down",
      "date_created": "1579556449403",
      "hide_from_guests": false,
      "value": 1,
      "required": false
    }
  ]
}

关于外地问题:
价值
Scala Cire代码串行器:

implicit val decodeCustomFields: Decoder[CustomFieldsItem] = new Decoder[CustomFieldsItem] {
    final def apply(c: HCursor): Decoder.Result[CustomFieldsItem] =
      for {
        id <- c.downField("id").as[Option[String]]
        name <- c.downField("name").as[Option[String]]
        typeF <- c.downField("type").as[Option[String]]
        date_created <- c.downField("date_created").as[Option[String]]
        hide_from_guests <- c.downField("hide_from_guests").as[Option[Boolean]]
        value <- c.downField("value").as[Option[List[String]]] // here it fails !!
        required <- c.downField("required").as[Option[Boolean]]
      } yield {
        CustomFieldsItem( id, name, typeF, date_created, hide_from_guests, value , required )
      }
  }

scala***unmarshal***http响应的代码,它采用circe序列化器:

private def runRequest(req: HttpRequest): Future[CustomFieldsItem] =
    Http()
      .singleRequest(req)
      .flatMap {
        res =>
          val ser = Unmarshal(res).to[CustomFieldsItem]
          ser
      }
  • 解组时出现消息错误:

发生错误:C[A]:向下字段(值)、右移、向下数组、向下字段(自订字段)、向下数组、向下字段(工作)
如何改进或修复此序列化?

ctehm74n

ctehm74n1#

是否使CustomField的类型为(CustomField[List[String]],CustomField[Int]),并具有泛型类型的CustomField case类?
问题是你的模型化并没有反映出你所接收到的内容的真实性,而你试图在事后修正这个问题。如果它总是有两个元素,那么它就不是一个列表,而是一个元组。

相关问题