API response या config JSON पेस्ट करें और उसकी संरचना को दर्शाने वाला Kotlin data class कोड पाएं। Nested objects और objects के arrays हर एक अपनी class बनते हैं, और आप kotlinx.serialization या Jackson में से कोई भी target चुन सकते हैं।
यह उस पल के लिए बना है जब आप Android app या Ktor/Spring service में बाहरी API जोड़ रहे हों और model का ढांचा टाइप करने की बजाय एक sample response से पाना चाहते हों। Conversion पूरी तरह आपके browser में चलता है, इसलिए internal API response पेस्ट करने पर वह कहीं नहीं भेजा जाता।
टाइप का नाम में top-level class का नाम सेट करें (default Root), उदाहरण के लिए UserResponse।सीरियलाइज़र में kotlinx.serialization या Jackson चुनें, जो आपका project पहले से इस्तेमाल करता हो।{
"orderId": "A-1024",
"paid": true,
"total": 39.9,
"coupon": null,
"items": [
{ "sku": "TSHIRT-M", "qty": 2 },
{ "sku": "CAP", "qty": 1, "gift_wrap": true }
]
}import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonElement
@Serializable
data class Order(
val orderId: String,
val paid: Boolean,
val total: Double,
val coupon: JsonElement? = null,
val items: List<Item>
)
@Serializable
data class Item(
val sku: String,
val qty: Int,
@SerialName("gift_wrap") val giftWrap: Boolean? = null
)
items array के अंदर के objects एकवचन Item class बनते हैं, और सिर्फ़ दूसरे item में मौजूद gift_wrap Boolean? = null बनता है। coupon हमेशा null ही है, इसलिए उसका type अनुमानित नहीं हो सकता और वह JsonElement? के रूप में छोड़ा जाता है, ताकि आप उसे असली type से बदलें।
@Serializable, नाम बदले fields पर @SerialName, साथ में ज़रूरी imports।@JsonProperty।user_name या home-url जैसी keys camelCase properties (userName, homeUrl) बनती हैं, जो annotation से मूल key से map होती हैं।class या val जैसे Kotlin keywords backticks में लपेटे जाते हैं; अंक से शुरू होने वाली keys को n prefix मिलता है।Int बनते हैं, 32-bit से बड़ी values Long, और कोई भी decimal field को Double बना देता है।? = null बनते हैं।typealias बनाता है, जैसे typealias Root = List<RootItem>।Postman या browser DevTools से कॉपी किया response पेस्ट करें और Call<Order> या client.get<Order>() के लिए तैयार classes पाएं। दर्जनों fields वाले responses में यह हाथ से कॉपी करने की typos और गलत types से बचाता है।
जब backend created_at भेजता है, तो Kotlin की तरफ़ createdAt पढ़ा जाता है जबकि serial name मूल key रखता है। Global naming strategy पर निर्भर रहने की बजाय mapping हर class में दिखती रहती है।
अगर आप Gson या Jackson models को kotlinx.serialization पर ले जा रहे हैं, तो ठीक-ठीक कौन से annotations बदलते हैं यह देखने के लिए उसी JSON से दोनों आउटपुट बनाएं।
? खुद जोड़ें।2024-01-01T00:00:00Z जैसी date strings String रहती हैं; Instant पर जाने के लिए अपना serializer चाहिए।Any को serialize नहीं कर सकता, इसलिए जिन values का type अनुमानित नहीं होता वे वहां JsonElement और Jackson के साथ Any बनती हैं।String? की बजाय String? = null क्यों है?जब key मौजूद न हो और property का कोई default न हो, तो kotlinx.serialization MissingFieldException देता है। कुछ items में न मौजूद या null वाले fields पर = null जोड़ने से उस key के बिना वाले responses भी ठीक से decode होते हैं।
@SerialName के बिना JSON keys को property नाम रख सकते हैं?टूल हमेशा camelCase में बदलता है और annotation सिर्फ़ तब जोड़ता है जब नाम अलग हो, इसलिए पहले से camelCase वाली keys बिना annotations के आती हैं। snake_case नाम रखने के लिए बनाने के बाद properties rename करें और annotations हटा दें।
हां। बने हुए data class में no-arg constructor नहीं होता, इसलिए constructor parameters से values bind करने के लिए Jackson को jackson-module-kotlin register होना चाहिए (dependency मौजूद होने पर Spring Boot इसे अपने-आप register करता है)।
Serializer के विकल्प kotlinx.serialization और Jackson हैं। Gson के लिए Jackson आउटपुट लें और @JsonProperty को @SerializedName से बदलें; Moshi के लिए @Json(name = ...) इस्तेमाल करें। Class की संरचना वही रहती है।