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
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1k
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
350
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
ヤプリ新卒SREの オンボーディング
masaki12
0
130
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
210
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.2k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
6
1.5k
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.8k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
subpath importsで始めるモック生活
10tera
0
320
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Become a Pro
speakerdeck
PRO
25
5k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
A better future with KSS
kneath
238
17k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
4 Signs Your Business is Dying
shpigford
180
21k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
380
Optimizing for Happiness
mojombo
376
70k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
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 ΑΓҾ༻
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠