पेजिंग कोडलैब में कोड का यह टुकड़ा है:
/**
* Immutable model class for a Github repo that holds all the information about a repository.
* Objects of this type are received from the Github API, therefore all the fields are annotated
* with the serialized name.
* This class also defines the Room repos table, where the repo [id] is the primary key.
*/
@Entity(tableName = "repos")
data class Repo(
@PrimaryKey @field:SerializedName("id") val id: Long,
@field:SerializedName("name") val name: String,
@field:SerializedName("full_name") val fullName: String,
@field:SerializedName("description") val description: String?,
@field:SerializedName("html_url") val url: String,
@field:SerializedName("stargazers_count") val stars: Int,
@field:SerializedName("forks_count") val forks: Int,
@field:SerializedName("language") val language: String?
)
उन सभी टिप्पणियों की आवश्यकता क्यों है? वे करते क्या हैं?
2 जवाब
इस डेटाबेस रिकॉर्ड की कल्पना करें:
id: 21434366,
name: "John",
full_name: "John Doe",
number_of_github_repository: 4
प्रत्येक फ़ील्ड की कुंजियाँ लोअर केस में होती हैं, अंडरस्कोर सेपरेटेड फॉर्मेट। हालांकि, खेतों/चरों की सामान्य नामकरण परंपरा ऊंट के मामले पर आधारित होती है।
फ़ील्ड नाम को स्वयं चर नाम के रूप में उपयोग करने के बजाय
val number_of_github_repository: Int
हम सभी इसे पसंद करते हैं
val numOfGithubRepos: Int
यहीं पर @field:SerializedName एनोटेशन चलन में आता है। यदि आप वास्तविक डेटाबेस फ़ील्ड नाम के साथ चर नाम को एनोटेट करते हैं, तो प्रोग्राम एनोटेट नाम से मान ढूंढेगा और इसे आपके अनुकूलित चर नाम पर असाइन करेगा।
उदाहरण के लिए,
@field:SerializedName("number_of_github_repositories") val numOfGithubRepos: Int
यह आपके डेटाबेस फ़ील्ड, "number_of_github_repositories" से मान की तलाश करेगा और इसे वेरिएबल numOfGithubRepos पर असाइन करेगा।
वही GSON लाइब्रेरी के साथ जाता है। यह अवांछित/असंगठित फ़ील्ड नामों को आपके पसंदीदा चर नामों में परिवर्तित करता है। डिफ़ॉल्ट रूप से, GSON लाइब्रेरी Json प्रतिक्रिया से फ़ील्ड को खोजने का प्रयास करती है जो घोषित चर नाम से मेल खाती है। तो अगर आपने बिना एनोटेशन के फ़ील्ड को इस तरह घोषित किया है,
val numOfGithubRepos: Int
और अगर वास्तविक Json वस्तु थी
{num_of_github_repositories: 4}
यह अपवाद फेंक देगा क्योंकि जेसन प्रतिक्रिया में numOfGithubRepos
नाम के साथ ऐसा कोई फ़ील्ड नहीं है।
वे एक एपीआई से एक जेसन के रूप में आते हैं। एक जेसन में एक कुंजी-मूल्य संरचना होती है, जहां आपके पास नाम और मूल्य होता है, ताकि एनोटेशन इंगित करे कि आप किस कुंजी की तलाश में हैं और फिर मान वापस कर दिया गया है, इस मामले में एक लंबा, तार और स्याही। मुझे आशा है कि मैं काफी स्पष्ट था। =)
संबंधित सवाल
नए सवाल
android
एंड्रॉइड Google का मोबाइल ऑपरेटिंग सिस्टम है, जिसका उपयोग प्रोग्रामिंग या डिजिटल डिवाइस (स्मार्टफोन, टैबलेट, ऑटोमोबाइल्स, टीवी, वियर, ग्लास, IoT) को विकसित करने के लिए किया जाता है। एंड्रॉइड से संबंधित विषयों के लिए, एंड्रॉइड-विशिष्ट टैग जैसे कि एंड्रॉइड-इरादे, एंड्रॉइड-गतिविधि, एंड्रॉइड-एडॉप्टर आदि का उपयोग करें। विकास या प्रोग्रामिंग के अलावा अन्य प्रश्नों के लिए, लेकिन एंड्रॉइड फ्रेमवर्क से संबंधित हैं, इस लिंक का उपयोग करें: https: // android.stackexchange.com।