JSON को Kotlin data class में बदलें!

JSON डालें और nested object समेत Kotlin data class कोड बनाएं! kotlinx.serialization @Serializable, @SerialName, Jackson समर्थित।
JSON
परिणाम
 

JSON को Kotlin data class में बदलें!

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 पेस्ट करने पर वह कहीं नहीं भेजा जाता।

उपयोग कैसे करें

  1. इनपुट बॉक्स में JSON पेस्ट करें। काटे-छांटे response की तुलना में पूरा असली response बेहतर type inference देता है।
  2. टाइप का नाम में top-level class का नाम सेट करें (default Root), उदाहरण के लिए UserResponse।
  3. सीरियलाइज़र में kotlinx.serialization या Jackson चुनें, जो आपका project पहले से इस्तेमाल करता हो।
  4. बना हुआ कोड अपनी model फ़ाइल में कॉपी करें। Type का नाम और serializer URL में रहते हैं, इसलिए bookmark वही setup दोबारा खोलता है।

उदाहरण

इनपुट (JSON)
{
  "orderId": "A-1024",
  "paid": true,
  "total": 39.9,
  "coupon": null,
  "items": [
    { "sku": "TSHIRT-M", "qty": 2 },
    { "sku": "CAP", "qty": 1, "gift_wrap": true }
  ]
}
आउटपुट (Kotlin · kotlinx.serialization)
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 से बदलें।

समर्थित सुविधाएँ

  • kotlinx.serialization: हर class पर @Serializable, नाम बदले fields पर @SerialName, साथ में ज़रूरी imports।
  • Jackson: सिर्फ़ नाम बदले fields पर @JsonProperty।
  • user_name या home-url जैसी keys camelCase properties (userName, homeUrl) बनती हैं, जो annotation से मूल key से map होती हैं।
  • class या val जैसे Kotlin keywords backticks में लपेटे जाते हैं; अंक से शुरू होने वाली keys को n prefix मिलता है।
  • संख्याएं: integers Int बनते हैं, 32-bit से बड़ी values Long, और कोई भी decimal field को Double बना देता है।
  • Objects के arrays सभी items से मिलाकर एक class बनते हैं; सिर्फ़ कुछ items में मौजूद fields ? = null बनते हैं।
  • Top-level array या primitive typealias बनाता है, जैसे typealias Root = List<RootItem>।

उपयोग के मामले

Retrofit या Ktor clients के response models

Postman या browser DevTools से कॉपी किया response पेस्ट करें और Call<Order> या client.get<Order>() के लिए तैयार classes पाएं। दर्जनों fields वाले responses में यह हाथ से कॉपी करने की typos और गलत types से बचाता है।

snake_case API को Kotlin naming से map करना

जब backend created_at भेजता है, तो Kotlin की तरफ़ createdAt पढ़ा जाता है जबकि serial name मूल key रखता है। Global naming strategy पर निर्भर रहने की बजाय mapping हर class में दिखती रहती है।

kotlinx.serialization पर migrate करना

अगर आप Gson या Jackson models को kotlinx.serialization पर ले जा रहे हैं, तो ठीक-ठीक कौन से annotations बदलते हैं यह देखने के लिए उसी JSON से दोनों आउटपुट बनाएं।

नोट्स और सीमाएँ

  • Types सिर्फ़ आपके पेस्ट किए sample से अनुमानित होते हैं। हर sample item में मौजूद field non-null आता है, इसलिए जिन fields को API docs छोड़े जा सकने वाला बताते हैं उन पर ? खुद जोड़ें।
  • 2024-01-01T00:00:00Z जैसी date strings String रहती हैं; Instant पर जाने के लिए अपना serializer चाहिए।
  • kotlinx.serialization Any को serialize नहीं कर सकता, इसलिए जिन values का type अनुमानित नहीं होता वे वहां JsonElement और Jackson के साथ Any बनती हैं।
  • Enums, sealed classes या default values (जैसे खाली lists) नहीं बनते। तय values वाले fields को बाद में enums में बदलना फ़ायदेमंद है।
  • सभी classes एक block में आती हैं; package declarations और फ़ाइलों में बांटना आप पर है।

अक्सर पूछे जाने वाले प्रश्न

Field सिर्फ़ 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 हटा दें।

Jackson चुनने पर क्या jackson-module-kotlin ज़रूरी है?

हां। बने हुए data class में no-arg constructor नहीं होता, इसलिए constructor parameters से values bind करने के लिए Jackson को jackson-module-kotlin register होना चाहिए (dependency मौजूद होने पर Spring Boot इसे अपने-आप register करता है)।

क्या यह Moshi या Gson models बना सकता है?

Serializer के विकल्प kotlinx.serialization और Jackson हैं। Gson के लिए Jackson आउटपुट लें और @JsonProperty को @SerializedName से बदलें; Moshi के लिए @Json(name = ...) इस्तेमाल करें। Class की संरचना वही रहती है।