Slide 1

Slide 1 text

Kotlinͷdata class΁ JacksonͰσγϦΞϥΠζ͠Α͏ ͱͯۤ͠࿑ͨ͠࿩ ୈճؔ੢,PUMJOษڧձ ! ೔ຊ,PUMJOϢʔβάϧʔϓ
 ௕ᖒଠ࿠!OHTX@UBSP

Slide 2

Slide 2 text

ࣗݾ঺հ w ௕ᖒଠ࿠!OHTX@UBSP w ,PUMJOΤϰΝϯδΣϦετ ࣗশ w ࡀɺֈ࠲ɺͱ͕େ޷͖

Slide 3

Slide 3 text

Α͋͘ΔJSON

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

ొ৔Ϋϥε w"SUJDMF w6TFS

Slide 8

Slide 8 text

JacksonͰσγϦΞϥΠζ͍ͨ͠ val objectMapper = ObjectMapper()! val article: Article =! objectMapper.readValue(json,! Article::class.java)! println(article)

Slide 9

Slide 9 text

֦ுؔ਺ + reifiedܕύϥϝʔλ inline fun ! ObjectMapper.read(json: String): T =! readValue(json, javaClass())! ! val article: Article = objectMapper read json

Slide 10

Slide 10 text

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)"! }

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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Ϋϥεʹ͍ͨ͠ͳʂ

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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! )

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Userͷఆٛ ͦͷ3 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name") val name: String,! @JsonProperty(“paying") val paying: Boolean = false! )

Slide 22

Slide 22 text

Α͋͘Δʁ Πέͯͳ͍JSON

Slide 23

Slide 23 text

{! "id": 123,! "title": "Kotlinೖ໳",! "deletedAt": null,! "userId": 456,! "userName": "ͨΖ͏",! "payingUser": true! }

Slide 24

Slide 24 text

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)! }

Slide 25

Slide 25 text

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ͷԸܙΛड͚ͳ͍ ίϯετϥΫλ͕ ඒ͘͠ͳ͍

Slide 26

Slide 26 text

Articleͷఆٛ ͦͷ5 data class Article(! val id: Long,! val title: String,! val author: User,! val deletedAt: Date? = null! ) {

Slide 27

Slide 27 text

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)! )

Slide 28

Slide 28 text

Java͔Β࢖͏

Slide 29

Slide 29 text

kotlin: user.paying java : user.getPaying()

Slide 30

Slide 30 text

kotlin: user.paying java : user.getPaying() μα͍

Slide 31

Slide 31 text

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! }

Slide 32

Slide 32 text

kotlin: user.copy(paying=false) ! java: user.copy(user.getId(), user.getName(), false)

Slide 33

Slide 33 text

kotlin: user.copy(paying=false) ! java: user.copy(user.getId(), user.getName(), false) ΊΜͲ͍

Slide 34

Slide 34 text

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)! }

Slide 35

Slide 35 text

Ҏ্

Slide 36

Slide 36 text

͓·͚ Kotlinมଶจ๏

Slide 37

Slide 37 text

fun f(x: String) {! x.fun String.() {! println(toUpperCase())! }()! } https://twitter.com/vreshetnikov/status/613487198801780736 ΑΓҾ༻

Slide 38

Slide 38 text

x.(y())()! ! ϥΠϒίʔσΟϯά! Ͱ͖ͳ͔ͬͨΒΰϝϯω https://twitter.com/vreshetnikov/status/613464247784214529 ΑΓҾ༻

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

fun(String.()->Unit).f() =! "A"() https://twitter.com/vreshetnikov/status/610624254996951040 ΑΓҾ༻

Slide 41

Slide 41 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠