Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlinのdata classへJacksonでデシリアライズしようとして苦労した話 #a...
Search
Taro Nagasawa
September 24, 2015
Programming
2
3.8k
Kotlinのdata classへJacksonでデシリアライズしようとして苦労した話 #ashiyakt
第2回関西Kotlin勉強会(
http://connpass.com/event/18102/
)の発表資料です。
Taro Nagasawa
September 24, 2015
Tweet
Share
More Decks by Taro Nagasawa
See All by Taro Nagasawa
Android開発者のための Kotlin Multiplatform入門
ntaro
0
440
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.2k
#Ubie 狂気の認知施策と選考設計
ntaro
13
13k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
940
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.6k
Kotlin Contracts #m3kt
ntaro
4
3.9k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
440
Other Decks in Programming
See All in Programming
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
230
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
820
Jakarta EE meets AI
ivargrimstad
0
280
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
150
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
350
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
260
テストコード書いてみませんか?
onopon
2
210
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
200
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
110
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
180
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Invisible Side of Design
smashingmag
298
50k
Speed Design
sergeychernyshev
25
680
Designing Experiences People Love
moore
138
23k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
GraphQLとの向き合い方2022年版
quramy
44
13k
For a Future-Friendly Web
brad_frost
175
9.4k
GitHub's CSS Performance
jonrohan
1031
460k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Optimizing for Happiness
mojombo
376
70k
Transcript
Kotlinͷdata class JacksonͰσγϦΞϥΠζ͠Α͏ ͱͯۤ͠࿑ͨ͠ ୈճؔ,PUMJOษڧձ ! ຊ,PUMJOϢʔβάϧʔϓ ᖒଠ!OHTX@UBSP
ࣗݾհ w ᖒଠ!OHTX@UBSP w ,PUMJOΤϰΝϯδΣϦετ ࣗশ w ࡀɺֈ࠲ɺͱ͕େ͖
Α͋͘ΔJSON
{! "id": 123,! "title": "Kotlinೖ",! "deletedAt": null,! "author": {! "id":
456,! "name": "ͨΖ͏",! "paying": true! }! }
{! "id": 123,! "title": "Kotlinೖ",! "deletedAt": null,! "author": {! "id":
456,! "name": "ͨΖ͏",! "paying": true! }! } Article
{! "id": 123,! "title": "Kotlinೖ",! "deletedAt": null,! "author": {! "id":
456,! "name": "ͨΖ͏",! "paying": true! }! } User
ొΫϥε w"SUJDMF w6TFS
JacksonͰσγϦΞϥΠζ͍ͨ͠ val objectMapper = ObjectMapper()! val article: Article =! objectMapper.readValue(json,!
Article::class.java)! println(article)
֦ுؔ + reifiedܕύϥϝʔλ inline fun <reified T> ! ObjectMapper.read(json: String):
T =! readValue(json, javaClass<T>())! ! val article: Article = objectMapper read json
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)"! }
Userͷఆٛ ͦͷ1 class User {! var id: Long? = null!
var name: String? = null! var paying: Boolean = false! ! override fun toString(): String =! "User($id, $name, $paying)"! }
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
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Ϋϥεʹ͍ͨ͠ͳʂ
Articleͷఆٛ ͦͷ2 data class Article(! var id: Long? = null,!
var title: String? = null,! var author: User? = null,! var deletedAt: Date? = null! )
Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!
var name: String? = null,! var paying: Boolean = false! )
Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!
var name: String? = null,! var paying: Boolean = false! ) EBUBΫϥεʹͳͬͨͧ
Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!
var name: String? = null,! var paying: Boolean = false! ) EBUBΫϥεʹͳͬͨͧ ͯ͢ͷҾʹσϑΥϧτ ͕͍ͭͯΔͷͰσϑΥϧτίϯετ ϥΫλ͕खʹೖΔͬΆ͍
Userͷఆٛ ͦͷ2 data class User(! var id: Long? = null,!
var name: String? = null,! var paying: Boolean = false! ) EBUBΫϥεʹͳͬͨͧ ͯ͢ͷҾʹσϑΥϧτ ͕͍ͭͯΔͷͰσϑΥϧτίϯετ ϥΫλ͕खʹೖΔͬΆ͍ WBSμαΠΑͶ
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! )
Userͷఆٛ ͦͷ3 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name")
val name: String,! @JsonProperty(“paying") val paying: Boolean = false! ) WBMʹͳͬͨ !+TPO1SPQFSUZ
Userͷఆٛ ͦͷ3 data class User(! @JsonProperty(“id") val id: Long,! @JsonProperty(“name")
val name: String,! @JsonProperty(“paying") val paying: Boolean = false! )
Α͋͘Δʁ Πέͯͳ͍JSON
{! "id": 123,! "title": "Kotlinೖ",! "deletedAt": null,! "userId": 456,! "userName":
"ͨΖ͏",! "payingUser": true! }
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)! }
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ͷԸܙΛड͚ͳ͍ ίϯετϥΫλ͕ ඒ͘͠ͳ͍
Articleͷఆٛ ͦͷ5 data class Article(! val id: Long,! val title:
String,! val author: User,! val deletedAt: Date? = null! ) {
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)! )
Java͔Β͏
kotlin: user.paying java : user.getPaying()
kotlin: user.paying java : user.getPaying() μα͍
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! }
kotlin: user.copy(paying=false) ! java: user.copy(user.getId(), user.getName(), false)
kotlin: user.copy(paying=false) ! java: user.copy(user.getId(), user.getName(), false) ΊΜͲ͍
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)! }
Ҏ্
͓·͚ Kotlinมଶจ๏
fun f(x: String) {! x.fun String.() {! println(toUpperCase())! }()! }
https://twitter.com/vreshetnikov/status/613487198801780736 ΑΓҾ༻
x.(y())()! ! ϥΠϒίʔσΟϯά! Ͱ͖ͳ͔ͬͨΒΰϝϯω https://twitter.com/vreshetnikov/status/613464247784214529 ΑΓҾ༻
object: (() -> Unit) ->! Unit by {} {} {}
https://twitter.com/vreshetnikov/status/611677438620241921 ΑΓҾ༻
fun(String.()->Unit).f() =! "A"() https://twitter.com/vreshetnikov/status/610624254996951040 ΑΓҾ༻
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠