0Pricing
Kotlin Multiplatform Academy · บทเรียน

โมเดลข้อมูล @Serializable

ใส่คำกำกับให้โมเดลเพื่อการแยกวิเคราะห์ที่ปลอดภัยตั้งแต่เวลาคอมไพล์

โมเดลข้อมูล @Serializable เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โมเดลข้อมูล @Serializable” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โมเดลข้อมูล @Serializable”

ใส่คำกำกับให้โมเดลเพื่อการแยกวิเคราะห์ที่ปลอดภัยตั้งแต่เวลาคอมไพล์ คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “โมเดลข้อมูล @Serializable” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โมเดลข้อมูล @Serializable
  2. @SerialName และการจับคู่ฟิลด์แบบกำหนดเอง
  3. ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม
  4. เชื่อม Serialization เข้ากับ Ktor
← กลับไปที่ Kotlin Multiplatform Academy