json_model_gen 0.0.8
json_model_gen: ^0.0.8 copied to clipboard
A Dart tool to generate model classes from JSON with fromJson, toJson, copyWith, and equality overrides.
๐งฌ json_model_gen #
A sleek CLI tool to effortlessly generate Dart model classes from raw JSON.
โจ Features #
- ๐ Convert raw JSON to Dart model classes
- โ๏ธ Supports
freezed
annotation - โป๏ธ Supports
equatable
- ๐ Make all fields nullable (optional)
- ๐ฌ Interactive prompts (if flags aren't provided)
- ๐ Overwrite protection with confirmation
- ๐ Auto-renames
.json
โ.dart
when needed
๐ Installation #
From pub.dev:
dart pub add json_model_gen
Or manually add to your pubspec.yaml
:
dependencies:
json_model_gen: ^0.0.6
Then:
dart pub get
๐ง Usage #
โ Full command with all options: #
dart run json_model_gen \
--input=raw.json \
--output=lib/user_model.dart \
--class=UserModel \
--freezed \
--equatable \
--nullable
๐ง Interactive mode: #
If you omit the flags:
dart run json_model_gen
Youโll be guided step-by-step to:
- ๐ Enter input file path
- ๐ Enter output file path
- ๐งช Enter class name
- โ๏ธ Use
equatable
? - โ๏ธ Use
freezed
? - โ Make all fields nullable?
๐ Example #
Input (raw.json
) #
{
"id": 1,
"name": "Alice",
"active": true,
"bio": null
}
Output (user_model.dart
) #
import 'package:freezed_annotation/freezed_annotation.dart';
part 'user_model.freezed.dart';
part 'user_model.g.dart';
@freezed
class UserModel with _$UserModel {
const factory UserModel({
int? id,
String? name,
bool? active,
String? bio,
}) = _UserModel;
factory UserModel.fromJson(Map<String, dynamic> json) =>
_$UserModelFromJson(json);
}
โ ๏ธ Notes #
- โ Input must be raw JSON only (not Dart code).
- ๐ If the input is a
.dart
file, you'll receive a warning. - ๐ณ Class names are validated to ensure PascalCase formatting.
- ๐ Output paths ending in
.json
will be renamed to.dart
automatically.
๏ธโญ Star the GitHub repo if this helps you!