JsonCare Flutter ๐ก๏ธ
JsonCare Flutter is a production-ready, crash-proof JSON parsing engine and automated Dart model generator. It eliminates runtime app crashes caused by inconsistent API payloads, type mismatches, and unexpected null values.
๐ Problem vs Solution
| API Input Payload | Standard Parsing (json['id'] as int) |
JsonCare (JsonCare.intVal(json['id'])) |
|---|---|---|
"123" (String instead of Int) |
๐ฅ Crash: type 'String' is not a subtype of 'int' |
โ
123 |
"19.99" (String instead of Double) |
๐ฅ Crash: type 'String' is not a subtype of 'double' |
โ
19.99 |
"42.0" (Decimal string to Int) |
๐ฅ Crash: FormatException / TypeError |
โ
42 |
1 or "yes" or "on" (Bool) |
๐ฅ Crash: type 'int' is not a subtype of 'bool' |
โ
true |
null (Null instead of List) |
๐ฅ Crash: Null check operator used on null |
โ
[] (Empty List) |
| Corrupted item inside List | ๐ฅ Crash: Entire array fails | โ Skips bad item, preserves rest |
๐ Key Features
- ๐ก๏ธ Zero-Crash Parsing: Automatically converts mismatched types (Strings, Ints, Doubles, Bools) or falls back to safe defaults.
- ๐๏ธ Automated Model Generator: Build null-safe Dart model classes from JSON in seconds.
- โก CLI Generator Tools: Pass raw JSON strings or JSON files directly via terminal.
- ๐ Reserved Keyword Escaping: Automatically handles reserved keywords (
default,class,is, etc.) seamlessly. - ๐งฉ Array Schema Merging: Merges properties across array samples to create comprehensive data models.
- โฐ Smart DateTime & Enum Conversion: Auto-converts ISO strings to Local Time and supports numeric/string enum lookups.
๐ ๏ธ Installation
Add jsoncare_flutter to your pubspec.yaml:
dependencies:
jsoncare_flutter: ^1.1.0
Or run:
flutter pub add jsoncare_flutter
๐ Safe Parsing Cheat Sheet
Use JsonCare helpers in your models or network repository layer:
import 'package:jsoncare_flutter/json_care.dart';
final dynamic data = {
"id": "101",
"price": "99.50",
"rating": "4.8",
"is_active": "yes",
"status": 1,
"created_at": "2026-08-03T10:00:00Z",
"tags": ["flutter", 123, null],
};
// 1. Safe Numbers
int id = JsonCare.intVal(data['id']); // 101
double price = JsonCare.doubleVal(data['price']); // 99.50
num rating = JsonCare.numVal(data['rating']); // 4.8
// 2. Safe Booleans
bool active = JsonCare.boolVal(data['is_active']); // true ("yes", "on", "1", "t" -> true)
// 3. Safe Enums (By name or ordinal index)
enum Status { unknown, active, inactive }
Status status = JsonCare.enumValue(data['status'], Status.values, def: Status.unknown); // Status.active
// 4. Safe Dates (Auto local timezone)
DateTime date = JsonCare.dateVal(data['created_at']);
// 5. Safe Lists & Maps (Per-item crash isolation)
List<String> tags = JsonCare.list(data['tags'], (i) => JsonCare.string(i)); // ["flutter", "123"]
Map<String, dynamic> meta = JsonCare.map(data['metadata']); // {} if null
๐๏ธ Automated Model Generator (CLI)
Generate null-safe model classes directly from your terminal.
1. Generate from JSON String
dart run jsoncare_flutter generate \
--name UserProfile \
--code '{"id": 1, "name": "John", "address": {"city": "Dhaka"}}'
2. Generate from JSON File (--file / -f)
dart run jsoncare_flutter generate \
--name UserProfile \
--file assets/sample_data.json \
--output lib/models
CLI Option Reference
| Option | Abbreviation | Description | Default |
|---|---|---|---|
--name |
-n |
Required. Name of root model class (e.g. UserProfile) |
N/A |
--code |
-c |
Raw JSON string to parse | N/A |
--file |
-f |
Path to JSON file to parse | N/A |
--output |
-o |
Target directory for generated models | lib/models |
๐ Output Directory Structure
The generator creates a dedicated module folder per root model:
lib/models/user_profile/
โโโ user_profile.dart # Main root class
โโโ address.dart # Nested class
โโโ models.dart # Barrel export file
๐งช Example Project
Check the included Example Directory for runnable demonstrations of safe parsing and programmatic generation.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.