json_model_gen 0.0.3
json_model_gen: ^0.0.3 copied to clipboard
A Dart tool to generate model classes from JSON with fromJson, toJson, copyWith, and equality overrides.
𧬠json_model_gen #
json_model_gen
is a Dart CLI tool that helps you convert a raw JSON file into a clean, fully-functional Dart model class β complete with:
fromJson
&toJson
copyWith
method==
operator &hashCode
Perfect for Flutter and Dart developers who want fast, consistent, and maintainable model classes.
π Installation #
To install this CLI tool globally:
dart pub global activate --source path .
Make sure you're inside the package directory when running the command above.
βοΈ How to Use
1οΈβ£ Prepare a JSON file
Example: sample.json
{
"id": 1,
"name": "Raagull",
"isActive": true,
"rating": 4.5
}
Save it anywhere on your system.
2οΈβ£ Run the generator
json_model_gen path/to/sample.json
β
This command will:
Parse the JSON
Generate a Dart model class
Save it as root_model.dart in the same directory
3οΈβ£ Output: What You Get
root_model.dart will contain something like:
class RootModel {
final int id;
final String name;
final bool isActive;
final double rating;
const RootModel({
required this.id,
required this.name,
required this.isActive,
required this.rating,
});
factory RootModel.fromJson(Map<String, dynamic> json) => RootModel(
id: json['id'] as int,
name: json['name'] as String,
isActive: json['isActive'] as bool,
rating: json['rating'] as double,
);
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'isActive': isActive,
'rating': rating,
};
RootModel copyWith({
int? id,
String? name,
bool? isActive,
double? rating,
}) => RootModel(
id: id ?? this.id,
name: name ?? this.name,
isActive: isActive ?? this.isActive,
rating: rating ?? this.rating,
);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is RootModel &&
runtimeType == other.runtimeType &&
id == other.id &&
name == other.name &&
isActive == other.isActive &&
rating == other.rating;
@override
int get hashCode =>
id.hashCode ^
name.hashCode ^
isActive.hashCode ^
rating.hashCode;
}
π§ͺ Example: Using in Dart Code
You can also use it programmatically:
import 'package:json_model_gen/json_model_gen.dart';
import 'dart:convert';
void main() {
final jsonString = '{"id": 1, "name": "Raagull", "isActive": true}';
final jsonMap = jsonDecode(jsonString);
final classCode = generateDartModel(jsonMap, className: 'User');
print(classCode);
}
π¦ Features
β
Clean & readable Dart code
β
Handles primitive types: int, double, bool, String
β
Constructor, fromJson, toJson, copyWith
β
Value equality support (==, hashCode)
π License
This project is licensed under the MIT License.
You can use, copy, modify, and distribute this software freely β just give credit and donβt blame the author if something breaks.
π Author
Made with β€οΈ by Raagull Sakthivel
β¨ Contributions
Feel free to fork, contribute, or suggest ideas via GitHub Issues!
Let me know if you want this published as a public GitHub repo or want a GitHub Actions workflow to auto-release it to pub.flutter-io.cn on each new version.