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
540
kotlinx.serialization
Takuji Nishibayashi
October 04, 2020
Tweet
Share
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
CameraX使ってみた
takuji31
0
66
kotlinx.datetime 使ってみた
takuji31
0
260
HiltのCustom Componentについて
takuji31
0
150
java.timeをAndroidで使う
takuji31
0
60
KSPを使ってコード生成
takuji31
0
290
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
960
kanmoba-returns-02.pdf
takuji31
0
180
AndroidXとKotlin Coroutines
takuji31
0
330
AndroidXに潜む便利なヤツら
takuji31
0
160
Other Decks in Technology
See All in Technology
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
AIチャットボット開発への生成AI活用
ryomrt
0
170
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
500
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
9
1.1k
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
Flutterによる 効率的なAndroid・iOS・Webアプリケーション開発の事例
recruitengineers
PRO
0
120
Engineer Career Talk
lycorp_recruit_jp
0
190
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
73
9.1k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
BBQ
matthewcrist
85
9.3k
Building an army of robots
kneath
302
43k
Git: the NoSQL Database
bkeepers
PRO
427
64k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Thoughts on Productivity
jonyablonski
67
4.3k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Visualization
eitanlees
145
15k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
Happy Clients
brianwarren
98
6.7k
KATA
mclloyd
29
14k
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αʔόʔαΠυͰ͑Δ • ϚϧνϓϥοτϑΥʔϜରԠ͍ͯ͠ͳ͍ϑΥʔϚοτ͋ΔͷͰҙ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠