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.dev, 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!