SerializationをKtorに組み込む
ContentNegotiationを導入して自動デコードします
「SerializationをKtorに組み込む」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Stop Parsing by Hand
You could decode every response manually, but Ktor can wire serialization in so requests and replies become typed automatically.
Meet ContentNegotiation
The ContentNegotiation plugin teaches the HttpClient how to convert request and response bodies to and from your models.
Add the Dependency
First add the Ktor content-negotiation artifact plus the JSON serializer module to commonMain.
implementation("io.ktor:ktor-client-content-negotiation:2.3.12")Add the JSON Bridge
You also include the ktor-serialization-kotlinx-json module, which connects Ktor to kotlinx.serialization specifically.
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")Install on the Client
Inside the HttpClient builder you install ContentNegotiation and register the json converter once.
install(ContentNegotiation) { json() }Configure the Json
The json block accepts a configured Json instance, so you can pass ignoreUnknownKeys and friends right here.
json(Json { ignoreUnknownKeys = true })Decode Automatically
Now a GET can return your model directly. Ktor reads the body and hands you a typed object with no manual decode call.
val user: User = client.get(url).body()Encode Request Bodies
The same plugin serializes outgoing data, so you can setBody with a Kotlin object and Ktor writes the JSON for you.
post(url) { setBody(user) }Set the Content Type
When sending a body, declare contentType as application/json so the server reads it correctly.
contentType(ContentType.Application.Json)One Setup, Both Apps
Because all of this lives in commonMain, Android and iOS share the identical typed networking with zero duplicate code.
Clean End to End
Ktor plus serialization gives you a tidy pipeline: call an endpoint, get a model, send a model, all type-safe across platforms.
Quick Check
Which Ktor plugin enables automatic JSON decoding of responses?
Recap
Install ContentNegotiation with json() on your HttpClient so Ktor auto-encodes request bodies and decodes responses into typed models on both apps.
よくある質問
「SerializationをKtorに組み込む」レッスンは無料ですか?
はい。「SerializationをKtorに組み込む」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「SerializationをKtorに組み込む」で何を学びますか?
ContentNegotiationを導入して自動デコードします ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「SerializationをKtorに組み込む」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- @Serializable Data Models
- @SerialNameとカスタムフィールドマッピング
- Nullable、Default、Optional Fields
- SerializationをKtorに組み込む