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
370
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
2.1k
#Ubie 狂気の認知施策と選考設計
ntaro
13
12k
UbieにおけるサーバサイドKotlin活用事例
ntaro
1
1.1k
KotlinでSpring 完全理解ガイド #jsug
ntaro
6
3.3k
Kotlinでサーバサイドを始めよう!
ntaro
1
920
Androidからサーバーサイドまで!プログラミング言語 Kotlinの魅力 #devboost
ntaro
5
2.6k
Kotlin Contracts #m3kt
ntaro
4
3.8k
How_to_Test_Server-side_Kotlin.pdf
ntaro
1
430
Other Decks in Programming
See All in Programming
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
870
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
920
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
CSC509 Lecture 11
javiergs
PRO
0
180
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
cmp.Or に感動した
otakakot
3
170
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
100
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
220
Featured
See All Featured
What's new in Ruby 2.0
geeforr
343
31k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
How to Ace a Technical Interview
jacobian
276
23k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Rails Girls Zürich Keynote
gr2m
94
13k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
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 ΑΓҾ༻
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠