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
550
kotlinx.serialization
Takuji Nishibayashi
October 04, 2020
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
86
kotlinx.datetime 使ってみた
takuji31
0
330
HiltのCustom Componentについて
takuji31
0
190
java.timeをAndroidで使う
takuji31
0
73
KSPを使ってコード生成
takuji31
0
310
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
990
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
340
AndroidXに潜む便利なヤツら
takuji31
0
160
Other Decks in Technology
See All in Technology
ハイテク休憩
sat
PRO
2
140
日本版とグローバル版のモバイルアプリ統合の開発の裏側と今後の展望
miichan
1
130
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
520
KubeCon NA 2024 Recap: How to Move from Ingress to Gateway API with Minimal Hassle
ysakotch
0
200
生成AIのガバナンスの全体像と現実解
fnifni
1
180
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
260
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
Wvlet: A New Flow-Style Query Language For Functional Data Modeling and Interactive Data Analysis - Trino Summit 2024
xerial
1
110
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
280
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
160
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
210
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
100
Featured
See All Featured
Bash Introduction
62gerente
608
210k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Site-Speed That Sticks
csswizardry
2
190
YesSQL, Process and Tooling at Scale
rocio
169
14k
Practical Orchestrator
shlominoach
186
10k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Building Applications with DynamoDB
mza
91
6.1k
Gamification - CAS2011
davidbonilla
80
5.1k
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αʔόʔαΠυͰ͑Δ • ϚϧνϓϥοτϑΥʔϜରԠ͍ͯ͠ͳ͍ϑΥʔϚοτ͋ΔͷͰҙ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠