jsoncare_flutter 1.1.0 copy "jsoncare_flutter: ^1.1.0" to clipboard
jsoncare_flutter: ^1.1.0 copied to clipboard

A tolerant JSON-to-Dart model generator and safe parsing helper that prevents runtime crashes caused by API type mismatches.

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

0
likes
160
points
82
downloads

Documentation

API reference

Publisher

verified publisherrahulreza.com

Weekly Downloads

A tolerant JSON-to-Dart model generator and safe parsing helper that prevents runtime crashes caused by API type mismatches.

Homepage

Topics

#json #generator #safe-parsing #crash-prevention #model-generator

License

MIT (license)

Dependencies

args, flutter, path, recase

More

Packages that depend on jsoncare_flutter