json_annotation_tools 0.1.3 copy "json_annotation_tools: ^0.1.3" to clipboard
json_annotation_tools: ^0.1.3 copied to clipboard

Transform cryptic JSON parsing errors into crystal-clear, actionable error messages. Perfect companion to json_annotation.

example/main.dart

library;

import 'package:json_annotation_tools/json_annotation_tools.dart';

void main() {
  print('šŸš€ json_annotation_tools Example');
  print('=' * 50);

  // šŸŽÆ SAFE JSON PARSING WITH ENHANCED ERRORS
  print('\nšŸ“± Safe JSON Parsing with Enhanced Error Messages');
  print('-' * 30);
  
  final userJson = {
    'id': 123,
    'name': 'John Doe',
    'email': 'john@example.com',
    'age': 30,
    'isActive': true,
  };

  try {
    // Using safe extension methods - no crashes on type mismatches!
    final id = userJson.getSafeInt('id');
    final name = userJson.getSafeString('name');
    final email = userJson.getSafeString('email');
    final age = userJson.getNullableSafeInt('age');
    final isActive = userJson.getSafeBool('isActive');
    
    print('āœ… SUCCESS: Parsed user data safely');
    print('   ID: $id, Name: $name, Age: $age, Active: $isActive');
  } catch (e) {
    print('āŒ Error: $e');
  }

  // šŸ”„ ERROR DEMONSTRATION: See enhanced error messages
  print('\nšŸ”„ ERROR DEMONSTRATION: Enhanced Error Messages');
  print('-' * 30);
  
  final badJson = {
    'id': 'not-a-number', // āŒ Should be int, got String
    'name': 'Jane Doe',
    'email': 'jane@example.com',
    'isActive': true,
  };

  print('\nšŸ“‹ Trying to parse invalid JSON (id should be int, got String):');
  
  try {
    final id = badJson.getSafeInt('id');
    print('āŒ Should have failed but got: $id');
  } catch (e) {
    print('āœ… CAUGHT ENHANCED ERROR:');
    print('šŸ”½ Enhanced error message:');
    final lines = e.toString().split('\n');
    for (int i = 0; i < lines.length && i < 8; i++) {
      print('   ${lines[i]}');
    }
    print('   ...');
    print('šŸ’” Notice: Clear diagnosis + copy-paste solutions!');
  }

  // šŸ”„ MISSING FIELD DEMONSTRATION
  print('\nšŸ”„ MISSING FIELD DEMONSTRATION');
  print('-' * 30);
  
  final incompleteJson = {
    'name': 'Bob Wilson',
    'email': 'bob@example.com',
    // Missing 'id' field
  };

  print('\nšŸ“‹ Trying to get missing field "id":');
  
  try {
    final id = incompleteJson.getSafeInt('id');
    print('āŒ Should have failed but got: $id');
  } catch (e) {
    print('āœ… CAUGHT MISSING FIELD ERROR:');
    print('šŸ”½ Enhanced error message with suggestions:');
    final lines = e.toString().split('\n');
    for (int i = 0; i < lines.length && i < 6; i++) {
      print('   ${lines[i]}');
    }
    print('   ...');
    print('šŸ’” Notice: Suggests similar field names!');
  }

  // šŸŽÆ REAL-WORLD SCENARIO: API Response Handling
  print('\n🌐 REAL-WORLD SCENARIO: API Response Handling');
  print('-' * 30);
  
  // Simulate different API responses
  final apiResponses = [
    {'status': 'Valid user', 'data': userJson},
    {'status': 'Type error', 'data': badJson},
    {'status': 'Missing field', 'data': incompleteJson},
  ];

  for (int i = 0; i < apiResponses.length; i++) {
    final response = apiResponses[i];
    final status = response['status'] as String;
    print('\nšŸ“” API Response ${i + 1}: $status');
    
    try {
      final data = response['data'] as Map<String, dynamic>;
      final id = data.getSafeInt('id');
      final name = data.getSafeString('name');
      print('   āœ… SUCCESS: Parsed user $name (ID: $id)');
    } catch (e) {
      print('   āŒ FAILED: ${e.toString().split('\n').first}');
      // In production: crashlytics.recordError(e, null);
    }
  }

  print('\nšŸŽ‰ Example Complete!');
  print('=' * 50);
  print('āœ… No more cryptic errors like "type \'String\' is not a subtype of type \'int\'"!');
  print('āœ… Clear, actionable error messages for faster debugging!');
  print('āœ… Copy-paste solutions save development time!');
  print('āœ… Perfect for production apps with external APIs!');
  print('\nšŸš€ For code generation (@SafeJsonParsing), see PRODUCTION_SETUP.md!');
}
2
likes
0
points
50
downloads

Publisher

verified publisherkhokan.me

Weekly Downloads

Transform cryptic JSON parsing errors into crystal-clear, actionable error messages. Perfect companion to json_annotation.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, build, flutter, json_annotation, meta, source_gen

More

Packages that depend on json_annotation_tools