mg_tools 1.0.17
mg_tools: ^1.0.17 copied to clipboard
A Dart tool to generate dart Freezed models from .dto.json files, nested objects support and auto DateTime detection.
π mg_tools #
Tool to generate Dart models from .dto.json files using freezed and json_serializable.
π Features #
- π Auto-scan project for all
.dto.jsonfiles - π Supports targeting a single file
- π Smart overwrite control using
--replace - π§© Supports nested objects and nested lists
- π Auto-detect
DateTimefields - π Annotates with
@JsonKey(name: "...", includeIfNull: false) - π Output all models in the same
.dartfile - π Generates helper methods:
MyModel myModelFromJson(String str)String myModelToJson(MyModel data)- or for list responses:
List<MyModel> myModelListFromJson(String str)String myModelListToJson(List<MyModel> data)
- π£ Clean, minimal, and fully ready for
freezed&json_serializable
π§° Getting started #
- Make sure you have the following dev dependencies in your
pubspec.yaml:
dependencies:
freezed_annotation:
json_annotation:
dev_dependencies:
mg_tools:
build_runner:
freezed:
json_serializable:
Then run:
dart pub get
βοΈ Usage #
β
Generate models from all .dto.json files #
dart run mg_tools
π Force replace existing generated files #
dart run mg_tools --replace
π― Generate model from a single file #
dart run mg_tools user.dto.json
π― + π Replace single file if it exists #
dart run mg_tools user.dto.json --replace
π Example #
Given a file named user.dto.json:
{
"id": 1,
"name": "John",
"email": "john@example.com",
"createdAt": "2024-03-20T12:00:00Z",
"profile": {
"avatar": "link"
},
"tags": ["dev", "dart"]
}
It generates a user.dart file like:
@freezed
class User with _$User {
const factory User({
@JsonKey(name: "id", includeIfNull: false) int? id,
@JsonKey(name: "name", includeIfNull: false) String? name,
@JsonKey(name: "email", includeIfNull: false) String? email,
@JsonKey(name: "createdAt", includeIfNull: false) DateTime? createdAt,
@JsonKey(name: "profile", includeIfNull: false) UserProfile? profile,
@JsonKey(name: "tags", includeIfNull: false) List<String>? tags,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
π‘ Tips #
π£ Contribute #
Feel free to open an issue or submit a PR with improvements, features, or bug fixes π
π License #
MIT
