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
340
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.5k
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
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4.1k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
430
Synchronizationを支える技術
s_shimotori
1
150
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
210
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
590
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
920
色々なIaCツールを実際に触って比較してみる
iriikeita
0
270
C#/.NETのこれまでのふりかえり
tomokusaba
1
160
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
850
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
1.6k
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
360
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
88
5.7k
Making the Leap to Tech Lead
cromwellryan
132
8.9k
Writing Fast Ruby
sferik
626
61k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Side Projects
sachag
452
42k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Embracing the Ebb and Flow
colly
84
4.4k
A Tale of Four Properties
chriscoyier
156
23k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
How to Think Like a Performance Engineer
csswizardry
19
1.1k
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 ΑΓҾ༻
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠