SafeJsonParsing class
Annotation to enable automatic safe JSON parsing code generation.
Add this annotation to your @JsonSerializable() classes to automatically
generate enhanced fromJsonSafe() methods with detailed error messages.
Example:
@JsonSerializable()
@SafeJsonParsing()
class User {
final int id;
final String name;
final int? age;
User({required this.id, required this.name, this.age});
// Standard json_serializable methods
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
// Auto-generated safe parsing method (created by build_runner)
// factory User.fromJsonSafe(Map<String, dynamic> json) => _$UserFromJsonSafe(json);
}
The generated fromJsonSafe() method will:
- Provide crystal-clear error messages for type mismatches
- Show exactly which field caused the parsing error
- Suggest fixes with copy-paste ready solutions
- Include smart field name suggestions for typos
- Handle all the complexity automatically
Constructors
- SafeJsonParsing({bool nullSafety = true, bool validateRequiredKeys = false, String methodName = 'fromJsonSafe', bool generateBothMethods = true})
-
const
Properties
- generateBothMethods → bool
-
Whether to generate both regular and safe methods
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- methodName → String
-
Custom method name for the generated safe parsing method
final
- nullSafety → bool
-
Whether to generate null-safe parsing methods for nullable fields
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- validateRequiredKeys → bool
-
Whether to generate field validation (requireKeys) before parsing
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited