A high-performance Dart data class forge that crafts perfect code structures multiple times faster than build_runner.
- ⚡ Lightning Fast: Multiple times faster than
build_runner - 🎯 Zero Configuration: Works out of the box
- 📦 Complete: Auto-generates
copyWith,==,hashCode,toJson,toString - 🔄 Automatic
fromJson: Automatically addsfromJsonfactory method to original file whenincludeFromJsonis true - 🔧 Flexible: Supports nested objects, collections, custom field mapping
Add dependency to pubspec.yaml:
dependencies:
dataforge_annotation:
git:
url: https://github.com/lwj1994/dataforge
ref: main
path: annotationInstall CLI tool:
dart pub global activate --source git https://github.com/lwj1994/dataforgeCreate a data class:
import 'package:dataforge_annotation/dataforge_annotation.dart';
part 'user.data.dart';
@Dataforge(includeFromJson: true, includeToJson: true)
class User with _User {
@override
final String name;
@override
@JsonKey(name: "user_age")
final int age;
@override
final List<String> hobbies;
const User({
required this.name,
this.age = 0,
this.hobbies = const [],
});
factory User.fromJson(Map<String, dynamic> json) => _User.fromJson(json);
}Generate code:
dataforge .Use generated methods:
final user = User(name: "John", age: 25);
final updated = user.copyWith(age: 26);
final json = user.toJson();
final fromJson = User.fromJson(json);
print(user.toString()); // User(name: John, age: 25, hobbies: [])@Dataforge: Configure code generation
@Dataforge(includeFromJson: true, includeToJson: true)@JsonKey: Customize field serialization
@JsonKey(name: "custom_name") // Rename field
@JsonKey(ignore: true) // Skip field
@JsonKey(alternateNames: ["alt1"]) // Multiple names
@JsonKey(includeIfNull: false) // Exclude null values from JSONIf you're migrating from the old data_class_gen package, the @DataClass annotation is still supported but deprecated. We recommend migrating to @Dataforge for new projects.
Legacy Support (Deprecated):
// ❌ Deprecated - still works but not recommended
@DataClass(includeFromJson: true, includeToJson: true)
class User with _User {
// ...
}
// ✅ Recommended - use the new annotation
@Dataforge(includeFromJson: true, includeToJson: true)
class User with _User {
// ...
}Migration Steps:
- Replace
@DataClasswith@Dataforge - Replace
@dataClasswith@dataforge - Update import from
data_class_annotationtodataforge_annotation - Update CLI command from
data_class_gentodataforge
Why migrate?
- Better naming that reflects the tool's purpose as a "data forge"
- Future features will only be available in
@Dataforge - Cleaner, more intuitive API
- Basic:
String,int,double,bool, optional types - Collections:
List<T>,Set<T>,Map<K, V> - Complex: Nested objects, custom classes
git clone https://github.com/lwj1994/dataforge.git
cd dataforge
dart pub get
dart test