0Pricing
Kotlin Multiplatform Academy · درس

دمج Serialization مع Ktor

تثبيت ContentNegotiation لفك الترميز تلقائيًا

دمج Serialization مع Ktor درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «دمج Serialization مع Ktor»؟

تثبيت ContentNegotiation لفك الترميز تلقائيًا تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «دمج Serialization مع Ktor»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. نماذج البيانات @Serializable
  2. @SerialName وربط الحقول المخصص
  3. الحقول القابلة للفقد والافتراضية والاختيارية
  4. دمج Serialization مع Ktor
← العودة إلى Kotlin Multiplatform Academy