简体   繁体   中英

Automatic derivation of codecs for structural types

Given the following JSON structure

  val rawJson =
    """
      |{
      |  "users": [
      |    {
      |      "name": "Mario",
      |      "age": 10,
      |      "address": {
      |        "street": "13 Blvd",
      |        "postcode": "ABC 123"
      |      }
      |    },
      |    {
      |      "name": "Wario",
      |      "age": 70,
      |      "address": {
      |        "street": "22 Blvd",
      |        "postcode": "CBA 321"
      |      }
      |    }
      |  ]
      |}
    """.stripMargin

and the corresponding model

  case class Address(street: String, postcode: String)
  case class User(name: String, age: Int, address: Address)
  case class RequestBody(users: List[User])

circe is able to automatically deserialise with

decode[RequestBody](rawJson))

Is automatic deserialisation possible when the model is defined via structural type like so

  case class RequestBody(users: List[ {
    val name: String
    val age: Int
    val address: {
      val street: String
      val postcode: String
    }
  }])

I guess no. Circe uses Shapeless under the hood for deriving codecs. And Shapeless works for ADT ie sealed trait + case classes. Structural types are not ADT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM