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
kotlinx.serialization
Search
Takuji Nishibayashi
October 04, 2020
Technology
0
530
kotlinx.serialization
Takuji Nishibayashi
October 04, 2020
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
58
kotlinx.datetime 使ってみた
takuji31
0
220
HiltのCustom Componentについて
takuji31
0
140
java.timeをAndroidで使う
takuji31
0
53
KSPを使ってコード生成
takuji31
0
280
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
940
kanmoba-returns-02.pdf
takuji31
0
170
AndroidXとKotlin Coroutines
takuji31
0
330
AndroidXに潜む便利なヤツら
takuji31
0
150
Other Decks in Technology
See All in Technology
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.5k
ABEMA のコンテンツ制作を最適化!生成 AI x クラウド映像編集システム / abema-ai-editor
cyberagentdevelopers
PRO
1
180
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
470
GitHub Universe: Evaluating RAG apps in GitHub Actions
pamelafox
0
180
分布で見る効果検証入門 / ai-distributional-effect
cyberagentdevelopers
PRO
4
700
日経電子版におけるリアルタイムレコメンドシステム開発の事例紹介/nikkei-realtime-recommender-system
yng87
1
510
Forget efficiency – Become more productive without the stress
ufried
0
150
グローバル展開を見据えたサービスにおける機械翻訳プラクティス / dp-ai-translating
cyberagentdevelopers
PRO
1
150
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
740
サイバーエージェントにおける生成AIのリスキリング施策の取り組み / cyber-ai-reskilling
cyberagentdevelopers
PRO
2
200
ガバメントクラウド先行事業中間報告を読み解く
sugiim
1
1.4k
物価高なラスベガスでの過ごし方
zakky
0
380
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
14
1.9k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
107
49k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Designing Experiences People Love
moore
138
23k
KATA
mclloyd
29
13k
Thoughts on Productivity
jonyablonski
67
4.3k
Rails Girls Zürich Keynote
gr2m
93
13k
Code Review Best Practice
trishagee
64
17k
Why Our Code Smells
bkeepers
PRO
334
57k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Gamification - CAS2011
davidbonilla
80
5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Transcript
kotlinx .serialization @takuji31 J Lang Fest Kansai Online #1
ࣗݾհ • @takuji31 • Takuji Nishibayashi • Hatena Co., Ltd.
• App Team • ίϛοΫDAYS • ΧΫϤϜ • δϟϯϓϧʔΩʔʂ
ࣗݾհ • Android • Kotlin • (Flutter / iOS /
Material Design etc.) • ! https://blog.takuji31.jp • " https://nazuna.takuji31.jp • # https://photo.takuji31.jp
ΞϓϦέʔγϣϯͰJSONYAML XMLΛಡΜͩΓॻ͍ͨΓͯ͠·͢ ͔ʁ
AGENDA • kotlinx.serializationʹ͍ͭͯ • kotlinx.serializationͷ͍ํ • ڝ߹ϥΠϒϥϦʔͱͷൺֱ
kotlinx. serializationʹ͍ͭͯ
KotlinͷγϦΞϥΠθʔγϣϯϥ ΠϒϥϦʔ
Kotlinެࣜ
ϚϧνϓϥοτϑΥʔϜରԠ
JSONҎ֎ʹෳͷϑΥʔϚοτ ʹରԠ
ϦϑϨΫγϣϯͳ͠
kaptෆཁ
Null SafetyରԠ
github.com/Kotlin/ kotlinx.serialization
kotlinx. serializationͷ͍ํ
ηοτΞοϓखॱ
plugins { kotlin("multiplatform") // or kotlin("jvm") or kotlin("android") etc. kotlin("plugin.serialization")
version "1.4.10" // Kotlin version } kotlin { sourceSets { val commonMain by getting { dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0-RC2") } } } }
or github.com/Kotlin/ kotlinx.serialization#setup
JSON
{ "id": 1, "title": "సੜͨ͠ΒKotlinͩͬͨ݅", "author": "@takuji31", "stories": [ {
"id": 1001, "number": 1, "title": "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠" } ] }
Ϋϥεఆٛ
@Serializable data class Comic( val id: Long, val title: String,
val author: String, val stories: List<Story>, ) @Serializable data class Story( val id: Long, val number: Int, val title: String, )
γϦΞϥΠζ
val comic = Comic( id = 1L, title = "సੜͨ͠ΒKotlinͩͬͨ",
author = "@takuji31", stories = listOf( Story( id = 1001L, number = 1, title = "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠", ), ), ) Json.encodeToString(Comic.serializer(), comic)
val comic = Comic( id = 1L, title = "సੜͨ͠ΒKotlinͩͬͨ",
author = "@takuji31", stories = listOf( Story( id = 1001L, number = 1, title = "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠", ), ), ) Json.encodeToString(comic)
{"id":1,"title":"సੜͨ͠ΒKotlinͩͬͨ","author":"@takuji31","stories":[{"id":1001,"number":1,"title":"ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠"}]}
!
val comic = Comic( ... ) val format = Json
{ prettyPrint = true } format.encodeToString(comic)
{ "id": 1, "title": "సੜͨ͠ΒKotlinͩͬͨ", "author": "@takuji31", "stories": [ {
"id": 1001, "number": 1, "title": "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠" } ] }
prettyPrintҎ֎ͷΦϓγϣϯ docs/json.md#json-configuration
σγϦΞϥΠζ
val json = //get json val comic: Comic = Json.decodeFromString(json)
val format = Json { ignoreUnknownKeys = true } val
json = //get json val comic: Comic = format.decodeFromString(json)
ڝ߹ϥΠϒϥϦʔ ͱͷൺֱ
ൺֱ͢ΔϥΠϒϥϦʔ • Gson • Jackson • Moshi
σγϦΞϥΠζ࣌ͷܗࣜ
ܗࣜ • ϦϑϨΫγϣϯ • kapt • GradleϓϥάΠϯ
֤ϥΠϒϥϦʔͷσγϦΞϥΠζܗࣜ ϥΠϒϥϦʔ ܗࣜ Gson ϦϑϨΫγϣϯ Jackson ϦϑϨΫγϣϯ Moshi ϦϑϨΫγϣϯ/kapt kotlinx.serialization
GradleϓϥάΠϯ
KotlinͷNull SafetyରԠ
֤ϥΠϒϥϦʔͷNull SafetyରԠ ϥΠϒϥϦʔ ରԠ/ඇରԠ Gson ඇରԠ Jackson ରԠ(jackson-module-kotlin) 1 Moshi
ରԠ kotlinx.serialization ରԠ 1 https://github.com/FasterXML/jackson-dataformats-text
JSONҎ֎ͷϑΥʔϚοτͷରԠ
֤ϥΠϒϥϦʔͷରԠϑΥʔϚοτ(Ұ෦) ϥΠϒϥϦʔ ରԠϑΥʔϚοτ Gson ͳ͠ Jackson YAML/CSV/XML/Properties/Protobuf 1 2 Moshi
ͳ͠ kotlinx.serialization YAML/XML/Properties/Protobuf 3 3 https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md 2 https://github.com/FasterXML/jackson-dataformats-binary 1 https://github.com/FasterXML/jackson-dataformats-text
Kotlin Multiplatform Project(KMP/ KMM)ରԠ
֤ϥΠϒϥϦʔͷରԠϑΥʔϚοτ(Ұ෦) ϥΠϒϥϦʔ ରԠ/ඇରԠ Gson ඇରԠ Jackson ඇରԠ Moshi ඇରԠ kotlinx.serialization
ରԠ
·ͱΊ
·ͱΊ • kotlinx.serializationKotlinެࣜγϦΞϥΠθʔγϣϯϥΠϒϥϦʔ • ηοτΞοϓͨ͠Β؆୯ʹ͑Δ • GradleϓϥάΠϯʹΑΔίʔυੜͳͷͰ࣮ߦ࣌/Ϗϧυ࣌ͷύϑΥʔϚϯ ε͕ྑ͍ • JSONҎ֎ʹଟͷϑΥʔϚοτʹରԠ
• ϚϧνϓϥοτϑΥʔϜରԠͰiOSαʔόʔαΠυͰ͑Δ • ϚϧνϓϥοτϑΥʔϜରԠ͍ͯ͠ͳ͍ϑΥʔϚοτ͋ΔͷͰҙ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠