0Pricing
Kotlin Multiplatform Academy · レッスン

@SerialNameとカスタムフィールドマッピング

一致しないJSONキーをKotlinの名前に対応付けます

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

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

Names Do Not Always Match

Real APIs love keys like user_name, but Kotlin prefers userName. @SerialName lets the two disagree peacefully.

Rename a Single Field

Put @SerialName above a property to tell the serializer which JSON key it really maps to.

@SerialName("user_name") val userName: String

Kotlin Stays Idiomatic

Your code keeps clean camelCase names while the wire format keeps whatever the backend chose. Both sides win.

Snake Case Is Common

Many JSON APIs use snake_case, so mapping created_at to a createdAt property is one of the most frequent uses.

@SerialName("created_at") val createdAt: String

Map Reserved Words

If JSON has a key like type that clashes with your style, @SerialName frees you to name the property whatever reads best.

@SerialName("type") val kind: String

Works on Every Field

You can annotate as many properties as you like, mixing renamed and default-named fields inside the same data class.

Rename Enum Entries Too

@SerialName also works on enum constants, so an ACTIVE entry can serialize as the lowercase string the API expects.

@SerialName("active") ACTIVE

Global Naming Strategy

If every key is snake_case you can set a JsonNamingStrategy once instead of annotating each field by hand.

Json { namingStrategy = JsonNamingStrategy.SnakeCase }

Annotation Wins

When both a global strategy and an explicit @SerialName apply, the annotation takes priority for that one field.

Same Class, Both Ways

The custom names apply to both encoding and decoding, so one annotated model handles reading and writing JSON.

Decoupled From the Backend

Mapping keys explicitly means a backend rename only touches one annotation, not your whole codebase.

Quick Check

How do you map a JSON key to a differently named Kotlin property?

Recap

Use @SerialName to map mismatched JSON keys to idiomatic Kotlin names on fields and enums, or set a global naming strategy for whole models.

よくある質問

「@SerialNameとカスタムフィールドマッピング」レッスンは無料ですか?

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

「@SerialNameとカスタムフィールドマッピング」で何を学びますか?

一致しないJSONキーをKotlinの名前に対応付けます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「@SerialNameとカスタムフィールドマッピング」レッスンにはどのくらい時間がかかりますか?

ほとんどの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に戻る