सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Typed_JSON एक Dart लाइब्रेरी है जिसका उद्देश्य किसी भी पैमाने के प्रोजेक्ट में हस्तचालित JSON सीरियलाइज़ेशन को सरल बनाना है। यह रनटाइम टाइप जानकारी या रिफ्लेक्शन के बिना Dart ऑब्जेक्ट्स को JSON में और JSON से Dart ऑब्जेक्ट्स में बदलने का एक साफ, टाइप-सुरक्षित तरीका प्रदान करता है। इसे कंपाइल-समय पर कुशल सीरियलाइज़ेशन कोड जनरेट करके प्रदर्शन सुनिश्चित किया जाता है और रनटाइम त्रुटियों के जोखिम को कम किया जाता है। लाइब्रेरी Flutter और अन्य Dart एप्लिकेशन में बिना किसी दिक्कत के एकीकृत होती है, और सभी प्रमुख प्लेटफॉर्म जैसे Android, iOS, Web, macOS, Windows और Linux का समर्थन करती है।
उपयोग
बड़े पैमाने वाले एप्लिकेशन में मैन्युअल JSON सीरियलाइजेशन
प्रकार सुरक्षित डेटा मॉडल प्रबंधन
प्लेटफॉर्म के बीच डेटा विनिमय
गतिशील प्रकारों पर निर्भरता कम करना
प्रदर्शन-महत्वपूर्ण JSON संचालन
कस्टम सीरियलाइजेशन लॉजिक
मुख्य विशेषताएँ
संकलन समय कोड उत्पादन
कोई रनटाइम प्रतिबिंबन नहीं
न्यूनतम बॉलरप्लेट के साथ मजबूत प्रकार
नेस्टेड और जटिल प्रकारों का समर्थन
कस्टम सीरियलाइज़र के माध्यम से विस्तार्य है
हल्का और तेज़
इनके लिए उपयुक्त
JSON अनुक्रमण के पूर्ण नियंत्रण की तलाश में विकासकर्ता
उच्च प्रदर्शन और प्रकार सुरक्षा की आवश्यकता वाले प्रोजेक्ट
तीसरे पक्ष के JSON मैपर पर निर्भरता से बचने वाली टीमें
जटिल डेटा मॉडल वाले अनुप्रयोग
विश्वसनीय डेटा स्थायित्व की आवश्यकता वाले Flutter एप्लिकेशन
विचार योग्य बातें
कोड जनरेशन चरण की आवश्यकता है
कस्टम सीरियलाइज़र सेटअप के लिए सीखने का वक्र
हस्तचालित निर्देशांक लॉजिक पर सीमित
त्वरित बदलते डेटा संरचनाओं के लिए उपयुक्त नहीं
बहुत सारे मॉडल के साथ बिल्ड समय थोड़ा बढ़ सकता है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Typed JSON
A library intended to bring manual JSON serialization to the Dart projects of all scales.
Features:
Human readable and maintainable API
Strict compile-time and runtime types checks
Nested objects parsing
Lists of objects parsing
Exceptions that include a JSON key path with an error
try {
int? intVal = json["year"].intValue;
String? stringVal = json["title"].stringValue;
double? doubleVal = json["price"].doubleValue;
num? numValue = json["price"].numValue;
bool? boolVal = json["inStock"].boolValue;
} onJsonValueExceptioncatch (e) {
print(e.value) // The value of the faulty fieldprint(e) // "Unable to parse the value at the/path/to/field"...
}
In this case, the JsonValueException will be thrown if the type of the value is incorrect. The null value is permitted.
Required values
If the value must not be null, you can use the *OrException properties:
try {
int intVal = json["year"].intOrException;
String stringVal = json["title"].stringOrException;
double doubleVal = json["price"].doubleOrException;
num numValue = json["price"].numOrException;
bool boolVal = json["inStock"].boolOrException;
} onJsonValueExceptioncatch (e) {
print(e.value) // The value of the faulty fieldprint(e) // "Unable to parse the required value at the/path/to/field"...
}
In this case, the JsonValueException will be thrown if the type of the value is incorrect or the value is null.
In the example above the keys "root" and "nested" must not be null, otherwise the JsonException will be thrown.
If you need to parse the nested JSON with keys that can be null, you should check it explicitly.
final value = j["inexisted"].optional["nested"].stringValue; // null
In the example, we marked the path "inexisted" as optional. The JSON parser will know that the "inexisted" can be null. And the "nested" and all the keys to the right side of "inexisted" can be null as well.
Null checks
You can check if the JSON value is not null:
final json = Json.empty(); // json with nullprint(json != null) // trueprint(json.isExist) // false
You can check if the JSON object contains the element with the giving name:
final json = Json.object(); // json with {}print(json.isExist) // trueprint(json["key"].isExist) // false
Now you can work with JSON values of the custom type using the get and set methods.
final valueJson = Json.empty();
valueJson.set(Status.active, StatusAdapter());
Status? valueStatus = valueJson.get(StatusAdapter());
final objectJson = Json.object();
objectJson["key"].set(Status.active, StatusAdapter());
Status? ojectStatus = objectJson["key"].get(StatusAdapter());
If the value must not be null, you can use the getRequired method.
final valueJson = Json.empty();
valueJson.set(Status.active, StatusAdapter());
Status valueStatus = valueJson.getRequired(StatusAdapter());
If the custom adapter returns null, the getRequired method will throw a JsonValueException.
try {
Json("incorrect").getRequired(StatusAdapter());
} onJsonValueExceptioncatch (e) {
print(e.value) // The value of the faulty fieldprint(e) // "Unable to parse the required value at the/path/to/field"...
}
Authors
Evgeniy Safronov (evsafronov.personal@gmail.com)
Alexander Smetannikov (alexsmetdev@gmail.com)
License
Typed JSON is available under the MIT license. See the LICENSE file for more info.