JSON को fromJson factory और toJson method वाली Dart classes में बदलें। कोड सादा Dart है, कोई code-generation package नहीं, इसलिए build_runner वाला कदम नहीं है।
यह उन Flutter apps के लिए ठीक है जो http या dio से मिले कच्चे Map<String, dynamic> responses सीधे widgets में भेजते हैं और उनकी जगह सही model classes चाहते हैं, या उन छोटे projects के लिए जिन्हें json_serializable अपनाने से पहले काम करने वाली serialization चाहिए।
टाइप का नाम में top-level class का नाम डालें (उदाहरण के लिए Store)।lib/models/ के अंदर किसी फ़ाइल में पेस्ट करें; किसी import की ज़रूरत नहीं।Store.fromJson(jsonDecode(response.body) as Map<String, dynamic>) से objects बनाएं और jsonEncode(store.toJson()) से भेजें।{
"storeId": 77,
"storeName": "Mapo Kimbap",
"isOpen": true,
"rating": 4.7,
"menuItems": [
{ "name": "Tuna Kimbap", "price": 4500, "soldOut": false },
{ "name": "Ramyeon", "price": 5000, "soldOut": true, "spicy": 2 }
]
}class Store {
final int storeId;
final String storeName;
final bool isOpen;
final double rating;
final List<MenuItem> menuItems;
Store({
required this.storeId,
required this.storeName,
required this.isOpen,
required this.rating,
required this.menuItems,
});
factory Store.fromJson(Map<String, dynamic> json) => Store(
storeId: json['storeId'] as int,
storeName: json['storeName'] as String,
isOpen: json['isOpen'] as bool,
rating: (json['rating'] as num).toDouble(),
menuItems: (json['menuItems'] as List<dynamic>).map((e) => MenuItem.fromJson(e as Map<String, dynamic>)).toList(),
);
Map<String, dynamic> toJson() => {
'storeId': storeId,
'storeName': storeName,
'isOpen': isOpen,
'rating': rating,
'menuItems': menuItems.map((e) => e.toJson()).toList(),
};
}
class MenuItem {
final String name;
final int price;
final bool soldOut;
final int? spicy;
MenuItem({
required this.name,
required this.price,
required this.soldOut,
this.spicy,
});
factory MenuItem.fromJson(Map<String, dynamic> json) => MenuItem(
name: json['name'] as String,
price: json['price'] as int,
soldOut: json['soldOut'] as bool,
spicy: json['spicy'] as int?,
);
Map<String, dynamic> toJson() => {
'name': name,
'price': price,
'soldOut': soldOut,
'spicy': spicy,
};
}
menuItems array के elements MenuItem class बनते हैं, जिन्हें fromJson के अंदर .map(...).toList() से बदला जाता है। rating को (json['rating'] as num).toDouble() के रूप में पढ़ा जाता है, इसलिए integer वैल्यू भी सुरक्षित है, और सिर्फ़ कुछ menu items में मौजूद spicy constructor में required के बिना int? है।
final हैं; constructors named parameters ({required this.x}) इस्तेमाल करते हैं।factory X.fromJson(Map<String, dynamic> json) और Map<String, dynamic> toJson(), बिना किसी package dependency के।fromJson / toJson call करते हैं।double fields (as num).toDouble() से पढ़े जाते हैं, इसलिए 4 जैसा JSON integer cast error नहीं देता।null वाले fields nullable (int?) बनते हैं और fromJson में null आगे भेजते हैं।dynamic होती हैं; class या is जैसे Dart reserved words के अंत में underscore लगता है।typedef Root = List<RootItem>; बनाता है।FutureBuilder या Riverpod/Bloc state में Map घुमाने से key की typos runtime तक छिपी रहती हैं। बनी हुई classes से store.menuItems.first.price पर completion और type checks मिलते हैं।
jsonEncode(store.toJson()) को shared_preferences या फ़ाइल में store करें और startup पर fromJson से restore करें, कुछ ही lines में cache तैयार।
पहले field सूची और types पक्के करें; project बढ़ने पर उन्हीं fields पर @JsonSerializable() जोड़ें और code generation पर चले जाएं।
5000.0 को integer 5000 पढ़ता है। Dart का jsonDecode 5000.0 को double रखता है, इसलिए as int exception देता है। जिन price या coordinate fields में decimal point आ सकता है, उन्हें double में बदलें।toJson nullable fields को null होने पर भी लिखता है ('spicy': null)। अगर आपका server null keys अस्वीकार करता है, तो उन्हें शर्त के साथ छोड़ें।String ही रहती हैं; उन्हें DateTime.parse(json['createdAt'] as String) से खुद बदलें।copyWith, ==/hashCode या toString नहीं बनते। ज़रूरत हो तो freezed या equatable पर विचार करें।dynamic fields बिना जांच के values रखते हैं; असली type पता चलने पर उन्हें बदलें।आउटपुट packages के बिना हाथ से लिखे जैसा fromJson/toJson है। Field declarations लें, @JsonSerializable() और part 'store.g.dart'; जोड़ें, और code generation पर जाने के लिए dart run build_runner build चलाएं।
type 'int' is not a subtype of type 'double' मिलता है।यह तब होता है जब कोड as double से cast करता है और JSON वैल्यू integer है। बने हुए double fields इससे बचने के लिए (as num).toDouble() इस्तेमाल करते हैं; हाथ से बदले किसी भी field पर यही pattern लगाएं।
[ {...}, {...} ]) को कैसे decode करूं?Top-level array से typedef और element class बनती है। हर element पर fromJson call करें: (jsonDecode(body) as List<dynamic>).map((e) => RootItem.fromJson(e as Map<String, dynamic>)).toList()।
आउटपुट Dart 2.12 से आगे का sound null safety syntax (int?, required) इस्तेमाल करता है। Null safety से पहले के projects में compile करने के लिए ? और required हटाने होंगे।