0Pricing
Kotlin Multiplatform Academy · レッスン

@Serializable Data Models

コンパイル時に安全にパースできるようモデルにアノテーションを付けます

「@Serializable Data Models」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

JSON Needs a Map

APIs speak JSON, but your shared code wants Kotlin objects. kotlinx.serialization bridges that gap on every KMP target.

Compile-Time Safety

Unlike reflection-based parsers, this library generates code at compile time, so it works fast and predictably on Android and iOS.

Mark a Class @Serializable

You tag a data class with @Serializable and the compiler writes a serializer for it automatically.

@Serializable
data class User(val id: Int, val name: String)

Add the Plugin

The annotation only works once you apply the kotlin-serialization Gradle plugin to your shared module.

plugins { kotlin("plugin.serialization") }

Add the Runtime

You also need the JSON runtime dependency in commonMain so the generated code has something to call.

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

Encode to a String

Turn a model into JSON text with Json.encodeToString, ready to send over the network.

val text = Json.encodeToString(user)

Decode From a String

Going the other way, decodeToString's partner reads JSON text back into a typed Kotlin object.

val user = Json.decodeFromString<User>(text)

Properties Become Keys

By default each property name becomes the matching JSON key, so id and name map straight across with no extra config.

Nested Models Just Work

If a serializable class holds another serializable class, the whole nested tree serializes together with no manual wiring.

@Serializable
data class Order(val buyer: User)

Supported Types

Primitives, strings, lists, maps and other serializable classes are all supported, which covers most real API payloads you will meet.

Truly Multiplatform

Because the serializer is generated Kotlin, the exact same parsing logic runs on every target with no platform-specific code.

Quick Check

What does the @Serializable annotation actually trigger?

Recap

Tag models with @Serializable, add the plugin and runtime, then use Json.encodeToString and decodeFromString to move between objects and JSON.

よくある質問

「@Serializable Data Models」レッスンは無料ですか?

はい。「@Serializable Data Models」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「@Serializable Data Models」で何を学びますか?

コンパイル時に安全にパースできるようモデルにアノテーションを付けます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「@Serializable Data Models」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. @Serializable Data Models
  2. @SerialNameとカスタムフィールドマッピング
  3. Nullable、Default、Optional Fields
  4. SerializationをKtorに組み込む
← Kotlin Multiplatform Academyに戻る