JsonCare Flutter πŸ›‘οΈ

pub package Dart SDK Flutter SDK License: MIT

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.


πŸ§‘β€πŸ’» Author & Maintainer

Md. Rahul Reza
🌐 Website | πŸ™ GitHub | πŸ’Ό LinkedIn