0

I would like to create a custom decoder for AWS API Gateway using circe-core. I have the following code:

case class APIGatewayInput(body:Result[Body], queryParams: Map[String,String], pathParams: Map[String,String])

object ApiGatewayInput {
  implicit val decodeApiGatewayInput = Decoder.instance { c =>
    val body:Result[Body] = c.get[Body]("body")
    val queryParams = c.field("queryStringParameters").as[Map[String,String]](decoder???)
    APIGatewayInput(body, queryParams, similarForPathParams)
  }
}

I know that Result has methods to parse data to Map[K,V] and as requires a Decoder (which is a little confusing) and I can convert it to a Map[String, String]

How can I use the API to convert to an existing scala collection type with the HCursor instead of a custom type.

4

1 回答 1

1
 import io.circe._, io.circe.generic.auto._, io.circe.parser._, 
 io.circe.syntax._
 case class Test(a: Map[String,String])
 val map = Test(Map("a"->"1","2"->"2")).asJson
 val hcursor = map.hcursor

 val m = hcursor.downField("a").as[Map[String,String]]
 println(m) // Right(Map(a -> 1, 2 -> 2))
于 2018-04-04T09:07:40.947 回答