json_model_gen 0.0.8 copy "json_model_gen: ^0.0.8" to clipboard
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.flutter-io.cn:

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!

4
likes
160
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart tool to generate model classes from JSON with fromJson, toJson, copyWith, and equality overrides.

Repository (GitHub)
View/report issues

Topics

#json #model #codegen #dart #cli

Documentation

API reference

License

MIT (license)

Dependencies

args, recase

More

Packages that depend on json_model_gen