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 serializationの使い方を詳しく調べてみた
Search
Wataru Mizukami
January 21, 2019
Programming
1.7k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Kotlin serializationの使い方を詳しく調べてみた
Wataru Mizukami
January 21, 2019
More Decks by Wataru Mizukami
See All by Wataru Mizukami
GitHub Actions活用術
tarumzu
1
820
CIサービス「Bitrise」を使って 最小限の労力でDanger + ktlintをGithubと連携させる
tarumzu
1
1.1k
JavaScriptでWebViewをハックする
tarumzu
2
1.5k
Realm Cloudを使ったオフラインファーストなアプリ開発
tarumzu
2
1.1k
ViewFlipperで手軽にリッチアニメーション
tarumzu
0
610
Other Decks in Programming
See All in Programming
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
210
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
230
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
330
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
250
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
280
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
430
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
140
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
460
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
1k
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
230
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
190
Featured
See All Featured
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
750
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.6k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Code Review Best Practice
trishagee
74
20k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
30 Presentation Tips
portentint
PRO
1
340
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.7k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Leo the Paperboy
mayatellez
7
1.9k
Utilizing Notion as your number one productivity tool
mfonobong
4
340
Transcript
Kotlin serializationの使い方を詳しく 調べてみた Wataru Mizukami(水上 亘)
自己紹介 Wataru Mizukami/水上 亘 Twitter/Github/Qiita: @tarumzu (たる) Organization/所属: sikmi, inc.
Kotlin serializationとは - シリアライザ・デシリアライザである(コード自動生成用のコンパイラプラグインとシリ アライズ用のランタイムライブラリの構成) - JSON, CBOR, Protobufの3つのフォーマットをサポートし、 さらに有志が作成したアドオンを使えばHOCON,
YAMLも使える https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md - Kotlin/JVM, Kotlin/JS, Kotlin/Nativeプラットフォームをサポート - リフレクションを使用しない
Kotlin serializationとは - シリアライザ・デシリアライザである(コード自動生成用のコンパイラプラグインとシリ アライズ用のランタイムライブラリの構成) - JSON, CBOR, Protobufの3つのフォーマットをサポートし、 さらに有志が作成したアドオン使えばHOCON,
YAMLも使える https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md - Kotlin/JVM, Kotlin/JS, Kotlin/Nativeプラットフォームをサポート - リフレクションを使用しない ー> リフレクション使用しない分速い!
build.gradle Androidでの設定方法について記述する。その他のプラットフォーム用の設定は公式参照のこと buildscript { ext.kotlin_version = '1.3.11' repositories { jcenter()
} dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" } }
app/build.gradle apply plugin: 'kotlin' apply plugin: 'kotlinx-serialization' // 略 repositories
{ jcenter() // artifacts are published to this repository maven { url "https://kotlin.bintray.com/kotlinx" } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1" }
使い方 import kotlinx.serialization.* import kotlinx.serialization.json.JSON @Serializable data class Data(val a:
Int, @Optional val b: String = "42") fun main(args: Array<String>) { // Serializableアノテーションを付けた場合、オブジェクトを渡すだけ val jsonData = JSON.stringify(Data(42)) // コレクション等も拡張関数 serializerを使うことで利用可能 val jsonList = JSON.stringify(Data.serializer().list, listOf(Data(42))) println(jsonData) // {"a": 42, "b": "42"} println(jsonList) // [{"a": 42, "b": "42"}] // 文字列をパースするときも簡単! val obj = JSON.parse<Data>("""{"a":42}""") println(obj) // Data(a=42, b="42") } 引用元: https://github.com/Kotlin/kotlinx.serialization#quick-example
使い方2 @Serializable class Data(val a: Int) { private val b:
String = "42" override fun equals(other: Any?) = /*...*/ } // unquotedはフォーマットからダブルクォーテーションを外す assertEquals("{a:1, b:42}", JSON.unquoted.stringify(Data(1))) assertEquals(Data(1), JSON.unquoted.parse<Data>("{a:1, b:42}")) 引用元: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/examples.md#supported-properties
アノテーションについて - Serializable : シリアライズしたいカスタムクラスに付けることでシリアライゼーションできる - Optional : 必須ではないパラメータの場合はオプショナルをつけないとエラーになる -
SerialName : JSONキー名が異なる場合に付ける - - Transient : JSONで扱わないプロパティを定義したい場合に使用する。 JSONにこのプロパティが存在していた場合エラーになる - SerialInfo : アノテーションクラスを定義するためのアノテーション。 プロパティにタグなどの情報をもたせることが出来て、 エンコーダー内でタグをもとに条件分岐などに利用できる。 Protobufなどで利用する
アノテーションの使い方 // シリアライズしたいクラスに付与 @Serializable data class Data( val a: Int,
// 必須パラメーターではない場合に付ける @Optional val b: String = “42”, // jsonキー名がcでプロパティ名とことなる場合 @SerialName("c") val d: String ) - SerialInfoの例は下記参照 https://github.com/Kotlin/kotlinx.serialization/blob/bb6e21e2c05cf6fa7d9a1b7f0c401d0afe726eda/runtime/com mon/src/test/kotlin/kotlinx/serialization/protobuf/SampleClasses.kt
カスタムシリアライザ シリアライザを自作することも可能。このサンプルは日付を扱う例 @Serializer(forClass = Date::class) object DateSerializer: KSerializer<Date> { private
val df: DateFormat = SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS") override val descriptor: SerialDescriptor = StringDescriptor.withName("WithCustomDefault") override fun serialize(output: Encoder, obj: Date) { output.encode(df.format(obj)) } override fun deserialize(input: Decoder): Date { return df.parse(input.decode()) } } 引用元: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md#external-serializers-for-library-classes
ご清聴、ありがとうございました!