نماذج البيانات @Serializable
إضافة annotations إلى النماذج لتحليل آمن وقت التجميع
نماذج البيانات @Serializable درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «نماذج البيانات @Serializable»؟
إضافة annotations إلى النماذج لتحليل آمن وقت التجميع تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «نماذج البيانات @Serializable»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نماذج البيانات @Serializable
- @SerialName وربط الحقول المخصص
- الحقول القابلة للفقد والافتراضية والاختيارية
- دمج Serialization مع Ktor