मौजूदा Kotlin data class पेस्ट करें और देखें कि serialize होने के बाद वह sample JSON document के रूप में कैसी दिखती है। Nested classes, List/Map properties और enums का पीछा करके उन्हें भरा जाता है, और @SerialName जैसे rename करने वाले annotations keys पर लागू होते हैं।
इसका इस्तेमाल तब करें जब server और app models साझा करते हों और आपको पक्का करना हो कि कोई class असल में कौन सा JSON भेजती और पाती है, या जब API doc या mock server को request body sample चाहिए।
import/package lines और member functions रह सकते हैं; वे छोड़ दिए जाते हैं।रूट टाइप को स्वचालित पर रहने दें: जिस पहली class को कोई दूसरी class reference नहीं करती वह root बनती है। सूची से कोई खास class भी चुन सकते हैं।Money class) null के रूप में आते हैं, और उनके नाम options के पास सूचीबद्ध होते हैं। वे classes भी पेस्ट करें और वे भर जाएंगे।@Serializable
data class Article(
val id: Long,
val title: String,
@SerialName("author_name") val authorName: String,
val status: PostStatus = PostStatus.DRAFT,
val tags: List<String> = emptyList(),
val publishedAt: Instant? = null,
@Transient val cachedHtml: String = "",
)
@Serializable
enum class PostStatus {
@SerialName("draft") DRAFT,
@SerialName("published") PUBLISHED,
}{
"id": 0,
"title": "",
"author_name": "",
"status": "draft",
"tags": [
""
],
"publishedAt": "1970-01-01T00:00:00Z"
}@SerialName की वजह से authorName author_name के रूप में निकलता है, और default PostStatus.DRAFT enum entry के अपने @SerialName से "draft" बनता है। cachedHtml पर @Transient लगा है, इसलिए वह हट जाता है। publishedAt जैसी null default वाली nullable property को भी null की बजाय typed sample (Instant → ISO timestamp) मिलता है।
@SerialName, Jackson @JsonProperty, Gson @SerializedName और Moshi @Json(name = ...)।@Transient या @JsonIgnore लगी properties, private properties, by lazy जैसी delegated properties, सिर्फ़ getter वाली computed properties, const values और companion object members।Status.PAID जैसा default उस entry की JSON वैल्यू बनता है।@SerialName का पालन करते हुए।LocalDate → 1970-01-01, LocalDateTime → 1970-01-01T00:00:00, Instant/OffsetDateTime → Z पर खत्म होने वाली ISO string, Duration → PT0S; UUID को सही रूप वाली वैल्यू मिलती है।List, Set और Array को एक sample element मिलता है; Map<Int, V> जैसे numeric key type वाला map key के रूप में "0" इस्तेमाल करता है।null या खाली array से काट दी जाती हैं।Response DTO पेस्ट करें और OpenAPI example या README के लिए JSON ढांचा पाएं। @SerialName से rename हुए fields उसी नाम से दिखते हैं जो serializer असल में लिखेगा।
MockWebServer या local JSON fixture की body उसी model से बनाएं जो आपका Retrofit interface लौटाता है। Placeholder values ("", 0) को असली जैसे data से बदलें, संरचना फिर भी model से मेल खाती है।
Server टीम को ठीक-ठीक दिखाएं कि app क्या भेजता है, या मिली spec को अपने Kotlin model से compare करें, ताकि key नामों का mismatch runtime errors बनने से पहले पकड़ा जाए।
"", 0, false) हैं, असली जैसा data नहीं; docs publish करने से पहले उन्हें बदलें।String?) को भी null की बजाय typed sample मिलता है, इसलिए अलग से चिह्नित करें कि कौन से fields सच में null हो सकते हैं।@JsonNaming या kotlinx JsonNamingStrategy जैसे global naming नियम लागू नहीं होते; बिना annotation वाली properties अपना Kotlin नाम key के रूप में रखती हैं।type discriminator) और custom KSerializer प्रतिबिंबित नहीं होते।emptyList() या listOf(...) जैसे function calls से बने defaults evaluate नहीं होते; वैल्यू declared type से आती है।@Transient property आउटपुट में नहीं है। क्या यह अपेक्षित है?हां। kotlinx.serialization @Transient और Jackson @JsonIgnore field को serialization से बाहर रखते हैं, इसलिए sample भी उसे छोड़ देता है। Class body में declare किया private val भी इसी कारण बाहर रहता है।
Class syntax एक जैसा है, इसलिए वे ठीक से parse होते हैं। Gson @SerializedName("...") और Moshi @Json(name = "...") के renames पहचाने जाते हैं।
= null declare किया, फिर field में वैल्यू क्यों है?लक्ष्य हर field का आकार दिखाना है, इसलिए null default अनदेखा करके typed sample इस्तेमाल होता है। इससे आप देख सकते हैं, उदाहरण के लिए, कि Instant? किस string format में जाता है।
उन्हें एक के बाद एक उसी इनपुट में पेस्ट करें। जो type अब भी नहीं मिला उसका नाम options के पास दिखता है, जिससे पता चलता है कि और क्या जोड़ना है।