Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlinのdata classへJacksonでデシリアライズしようとして苦労した話 #a...

Taro Nagasawa
September 24, 2015

Kotlinのdata classへJacksonでデシリアライズしようとして苦労した話 #ashiyakt

第2回関西Kotlin勉強会( http://connpass.com/event/18102/ )の発表資料です。

Taro Nagasawa

September 24, 2015
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. {! "id": 123,! "title": "Kotlinೖ໳",! "deletedAt": null,! "author": {! "id":

    456,! "name": "ͨΖ͏",! "paying": true! }! } Article
  2. {! "id": 123,! "title": "Kotlinೖ໳",! "deletedAt": null,! "author": {! "id":

    456,! "name": "ͨΖ͏",! "paying": true! }! } User
  3. ֦ுؔ਺ + reifiedܕύϥϝʔλ inline fun <reified T> ! ObjectMapper.read(json: String):

    T =! readValue(json, javaClass<T>())! ! val article: Article = objectMapper read json
  4. Articleͷఆٛ ͦͷ1 class Article {! var id: Long? = null!

    var title: String? = null! var author: User? = null! var deletedAt: Date? = null! ! override fun toString(): String =! "Article($id, $title, $author, $deletedAt)"! }
  5. Userͷఆٛ ͦͷ1 class User {! var id: Long? = null!

    var name: String? = null! var paying: Boolean = false! ! override fun toString(): String =! "User($id, $name, $paying)"! }
  6. Userͷఆٛ ͦͷ1 class User {! var id: Long? = null!

    var name: String? = null! var paying: Boolean = false! ! override fun toString(): String =! "User($id, $name, $paying)"! } σϑΥϧτ ίϯετϥΫλ WBSϓϩύςΟ TFUUFSHFUUFS
  7. Userͷఆٛ ͦͷ1 class User {! var id: Long? = null!

    var name: String? = null! var paying: Boolean = false! ! override fun toString(): String =! "User($id, $name, $paying)"! } σϑΥϧτ ίϯετϥΫλ WBSϓϩύςΟ TFUUFSHFUUFS EBUBΫϥεʹ͍ͨ͠ͳʂ
  8. Articleͷఆٛ ͦͷ2 data class Article(! var id: Long? = null,!

    var title: String? = null,! var author: User? = null,! var deletedAt: Date? = null! )
  9. Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!

    var name: String? = null,! var paying: Boolean = false! )
  10. Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!

    var name: String? = null,! var paying: Boolean = false! ) EBUBΫϥεʹͳͬͨͧ
  11. Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!

    var name: String? = null,! var paying: Boolean = false! ) EBUBΫϥεʹͳͬͨͧ ͢΂ͯͷҾ਺ʹσϑΥϧτ஋ ͕͍ͭͯΔͷͰσϑΥϧτίϯετ ϥΫλ͕खʹೖΔͬΆ͍
  12. Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!

    var name: String? = null,! var paying: Boolean = false! ) EBUBΫϥεʹͳͬͨͧ ͢΂ͯͷҾ਺ʹσϑΥϧτ஋ ͕͍ͭͯΔͷͰσϑΥϧτίϯετ ϥΫλ͕खʹೖΔͬΆ͍ WBSμαΠΑͶ
  13. Articleͷఆٛ ͦͷ3 data class Article(! @JsonProperty(“id") val id: Long,! @JsonProperty(“title")

    val title: String,! @JsonProperty(“author") val author: User,! @JsonProperty(“deletedAt") val deletedAt: Date? = null! )
  14. Userͷఆٛ ͦͷ3 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name")

    val name: String,! @JsonProperty(“paying") val paying: Boolean = false! ) WBMʹͳͬͨ !+TPO1SPQFSUZ
  15. Userͷఆٛ ͦͷ3 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name")

    val name: String,! @JsonProperty(“paying") val paying: Boolean = false! )
  16. Articleͷఆٛ ͦͷ4 data class Article(! @JsonProperty(“id") val id: Long,! @JsonProperty(“title")

    val title: String,! @JsonProperty(“deletedAt") val deletedAt: Date? = null,! @JsonProperty(“userId") userId: Long,! @JsonProperty(“userName") userName: String,! @JsonProperty(“payingUser") payingUser: Boolean = false! ) {! val author: User = User(userId, userName, payingUser)! }
  17. Articleͷఆٛ ͦͷ4 data class Article(! @JsonProperty(“id") val id: Long,! @JsonProperty(“title")

    val title: String,! @JsonProperty(“deletedAt") val deletedAt: Date? = null,! @JsonProperty(“userId") userId: Long,! @JsonProperty(“userName") userName: String,! @JsonProperty(“payingUser") payingUser: Boolean = false! ) {! val author: User = User(userId, userName, payingUser)! } BVUIPS͕ EBUBͷԸܙΛड͚ͳ͍ ίϯετϥΫλ͕ ඒ͘͠ͳ͍
  18. Articleͷఆٛ ͦͷ5 data class Article(! val id: Long,! val title:

    String,! val author: User,! val deletedAt: Date? = null! ) {
  19. Articleͷఆٛ ͦͷ5 constructor(! @JsonProperty(“id") id: Long,! @JsonProperty(“title") title: String,! @JsonProperty(“deletedAt")

    deletedAt: Date? = null,! @JsonProperty(“userId") userId: Long,! @JsonProperty(“userName") userName: String,! @JsonProperty(“payingUser") payingUser: Boolean = false! ): this(! id = id,! title = title,! deletedAt = deletedAt,! author = User(userId, userName, payingUser)! )
  20. Userͷఆٛ ͦͷ5 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name")

    val name: String,! @JsonProperty(“paying") val paying: Boolean = false! ) {! fun isPaying(): Boolean = paying! }
  21. Userͷఆٛ ͦͷ6 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name")

    val name: String,! @JsonProperty(“paying") val paying: Boolean = false! ) {! fun isPaying(): Boolean = paying! fun withPaying(paying: Boolean) = copy(paying = paying)! }
  22. fun f(x: String) {! x.fun String.() {! println(toUpperCase())! }()! }

    https://twitter.com/vreshetnikov/status/613487198801780736 ΑΓҾ༻
  23. object: (() -> Unit) ->! Unit by {} {} {}

    https://twitter.com/vreshetnikov/status/611677438620241921 ΑΓҾ༻