json_type_guard 1.0.0 copy "json_type_guard: ^1.0.0" to clipboard
json_type_guard: ^1.0.0 copied to clipboard

A tiny, zero-codegen, runtime-safe JSON parsing helper for Dart & Flutter with precise field-level error messages.

πŸ›‘οΈ json_type_guard #

Protector of Models. Warden of Types. Keeper of the Seven JSON Kingdoms. #

A tiny, zero-codegen, runtime-safe JSON parsing helper for Dart & Flutter.
Stop guessing which field broke your .fromJson.
Stop seeing β€œNull is not a subtype of…” with no clue where it came from.

json_type_guard gives you precise, field-level errors, safe parsing, optional fields, defaults, and full type validation β€” all without build runners, annotations, or boilerplate.


πŸš€ Features #

  • πŸ›‘οΈ Runtime type-safe JSON access
  • πŸ”Ž Clear, explicit error messages
  • ❌ No code generation
  • 🧩 No annotations
  • 🎯 Zero boilerplate
  • πŸ‘€ Detects wrong data types per field
  • πŸ”„ Fallback/default value support
  • ❓ Optional fields & nullable types
  • πŸ“š Supports all Dart types
  • 🧱 Consistent & predictable API

πŸ“¦ Installation #

dependencies:
  json_type_guard: ^1.0.0

🎯 Quick Start #

import 'package:json_type_guard/json_type_guard.dart';

class User {
  final String name;
  final int age;
  final String? email;
  final String role;

  User.fromJson(Map json)
      : name = json.guard<String>("name"),
        age = json.guard<int>("age"),
        email = json.guardOrNull<String>("email"),
        role = json.guard<String>("role", defaultValue: "user");
}

void main() {
  final json = {
    'name': 'Alice',
    'age': 30,
    'email': 'alice@example.com',
  };
  
  final user = User.fromJson(json);
  print(user.name); // Alice
}

πŸ“– Usage #

Basic Type Extraction #

final json = {'name': 'Bob', 'age': 25};

// Required fields
final name = json.guard<String>('name');
final age = json.guard<int>('age');

Optional Fields #

// Returns null if missing or null
final email = json.guardOrNull<String>('email');

Default Values #

// Uses default if field is missing
final role = json.guard<String>('role', defaultValue: 'user');

Nested Objects #

class Address {
  final String city;
  Address.fromJson(Map json) : city = json.guard<String>('city');
}

class Person {
  final String name;
  final Address address;

  Person.fromJson(Map json)
      : name = json.guard<String>('name'),
        address = json.guardObject<Address>('address', Address.fromJson);
}

Lists #

// List of primitives
final tags = json.guardList<String>('tags', (v) => v as String);

// List of objects
final users = json.guardList<User>('users', (v) => User.fromJson(v as Map));

🚨 Error Handling #

When parsing fails, you get precise, readable errors:

final json = {'name': 'Charlie', 'age': '30'}; // age is String, not int

try {
  final user = User.fromJson(json);
} catch (e) {
  print(e);
  // Output:
  // JsonGuardError:
  //   Field: "age"
  //   Expected: int
  //   Received: String (30)
}

Custom Error Themes #

import 'package:json_type_guard/json_type_guard.dart';

GuardTheme.setTheme(
  errorPrefix: '🚨 CUSTOM ERROR:',
  fieldLabel: 'πŸ“ Field',
  expectedLabel: '🎯 Expected',
  receivedLabel: 'πŸ“¦ Received',
);

πŸ”§ API Reference #

Guard Methods #

Method Description
guard<T>(key, {defaultValue}) Extract required field with optional default
guardOrNull<T>(key) Extract optional/nullable field
guardObject<T>(key, builder) Parse nested object
guardList<T>(key, convert) Parse list with converter

Static Methods #

// Use Guard class directly
Guard.value<String>(json, 'name');
Guard.valueOrNull<int>(json, 'age');
Guard.object<Address>(json, 'address', Address.fromJson);
Guard.list<String>(json, 'tags', (v) => v as String);

Debug Mode #

Guard.setDebugLogging(true);  // Enable debug logs
Guard.setDebugLogging(false); // Disable debug logs

πŸ†š Comparison #

Feature json_type_guard json_serializable Manual parsing
Code generation ❌ βœ… ❌
Build runner ❌ βœ… ❌
Annotations ❌ βœ… ❌
Clear errors βœ… ❌ ❌
Runtime safety βœ… βœ… ❌
Setup time Instant Minutes Instant
Boilerplate Minimal Medium High

🀝 Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.


πŸ“„ License #

MIT License - see the LICENSE file for details.


🌟 Show Your Support #

If you find this package helpful, please give it a ⭐ on GitHub!

2
likes
0
points
163
downloads

Publisher

unverified uploader

Weekly Downloads

A tiny, zero-codegen, runtime-safe JSON parsing helper for Dart & Flutter with precise field-level error messages.

Repository (GitHub)
View/report issues

Topics

#json #parsing #validation #type-safety #error-handling

License

unknown (license)

More

Packages that depend on json_type_guard