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
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
110
선언형 UI에서의 상태관리
l2hyunwoo
0
190
命名をリントする
chiroruxx
1
450
MCP with Cloudflare Workers
yusukebe
2
230
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
260
CloudflareStack でRAGに入門
asahiiwm
0
100
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
200
iOS開発におけるCopilot For XcodeとCode Completion / copilot for xcode
fuyan777
1
110
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
360
Recoilを剥がしている話
kirik
5
7.2k
良いユニットテストを書こう
mototakatsu
8
3.1k
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
160
Featured
See All Featured
Become a Pro
speakerdeck
PRO
26
5k
Documentation Writing (for coders)
carmenintech
67
4.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
The Cult of Friendly URLs
andyhume
78
6.1k
Agile that works and the tools we love
rasmusluckow
328
21k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Site-Speed That Sticks
csswizardry
2
190
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Making Projects Easy
brettharned
116
6k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Producing Creativity
orderedlist
PRO
342
39k
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 ΑΓҾ༻
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠