json_model_gen 0.0.6
json_model_gen: ^0.0.6 copied to clipboard
A Dart tool to generate model classes from JSON with fromJson, toJson, copyWith, and equality overrides.
𧬠json_model_gen
Generate Dart model classes effortlessly from raw JSON using this CLI tool.
β¨ Features
π Converts JSON into Dart classes βοΈ Supports freezed annotation β»οΈ Supports equatable π Optional field nullability π¬ Interactive prompts (if no flags provided) π Prevents accidental overwrites π Auto-renames .json β .dart when needed
π Installation
To install from pub.flutter-io.cn, run:
dart pub add json_model_gen Or manually add it to your pubspec.yaml:
dependencies: json_model_gen: ^0.0.6 Then run:
dart pub get
π§ Usage
π¦ Full command with flags:
dart run json_model_gen
--input=raw.json
--output=lib/user_model.dart
--class=UserModel
--freezed
--equatable
--nullable
π§ Or use interactive mode: Just run:
dart run json_model_gen It will ask you step-by-step:
π₯ Input file path π€ Output file path π§ͺ 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 file must contain ONLY valid JSON, not Dart code. π If your input file ends with .dart, it will still work, but a warning will appear. π§ͺ Class names are validated to be in PascalCase. π --output=model.json will auto-convert to model.dart.
β Star the repo on GitHub if you like it!