json_annotation_tools 0.1.3
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!');
}