json_factory_generator library
JSON Factory Generator Library
This library provides automatic code generation for centralized JSON factories that can parse JSON data into strongly-typed Dart objects.
Key Features
- No forced dependencies: Works with any class that has a
fromJsonmethod - Optional json_serializable support: Works with or without
@JsonSerializable - Type-safe: Compile-time generation with runtime type safety
- List support: Handles
List<T>parsing with proper generic types
Usage
- Add the
@jsonModelannotation to your model classes:
Manual fromJson (No dependencies)
@jsonModel
class User {
final String name;
User({required this.name});
factory User.fromJson(Map<String, dynamic> json) => User(
name: json['name'] as String,
);
}
With json_serializable (Optional)
@jsonModel
@JsonSerializable()
class User {
final String name;
User({required this.name});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
- Run code generation:
dart run build_runner build
- Use the generated JsonFactory:
final user = JsonFactory.fromJson<User>(jsonData);
final users = JsonFactory.fromJson<List<User>>(jsonList);
The library automatically discovers all annotated models with fromJson methods
and generates a centralized factory with type-safe parsing capabilities.
Classes
- JsonFactoryBuilder
- A custom Builder that generates the centralized JsonFactory implementation.
Functions
-
jsonFactoryBuilder(
BuilderOptions options) → Builder - Creates and configures a Builder for generating the JsonFactory code.