๐Ÿงฌ 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!

Libraries

generator
json_model_gen
A Dart library for generating Dart model classes from JSON data.