freezer 0.2.0 freezer: ^0.2.0 copied to clipboard
Automatically generates object classes supported by the freezed library directly from JSON files.
The Most Powerful Way to Automatically Generate Model Objects from JSON Files ⚡
1. Guide 🌎 #
This library was built on the foundation of the json_serializable and freezed libraries.
This library provides the ability to automatically generate class objects supported by the freezed library directly from JSON files.
The goal of this library is to maximize developer productivity by automatically generating implementations from design data.
Show some ❤️ and star the repo to support the project.
Note
Many of the specifications in this library are still in development. Your contributions are very welcome!
1.1. Motivation 💎 #
- Realize JSON-based model design.
- Realize automatic generation of model objects from design.
- Synchronize the design and implementation of model objects.
- Expand the possibilities of the freezed library even more.
- etc...
And all you have to do is prepare a JSON file defining the structure of the model object to be generated and run the command dart run freezer:main
in a terminal.
You can see the following example.
1.2. From #
{
"shop": {
"name.!required": "My Fancy Shop",
"products.!as:product.!name:my_products": [
{
"name": "Chocolate",
"price": 5.99
},
{
"name": "Gummy",
"price": 8.99
}
],
"closed": false,
"$name": "This is a comment for name field.",
"$products": "This is a comment for product field.",
"$$products": "This is a comment for product object."
}
}
1.3. To #
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: invalid_annotation_target
import 'package:freezed_annotation/freezed_annotation.dart';
import 'product.dart';
part 'shop.freezed.dart';
part 'shop.g.dart';
// **************************************************************************
// FreezerGenerator
// **************************************************************************
@freezed
class Shop with _$Shop {
const factory Shop({
/// This is a comment for name field.
required String name,
/// This is a comment for product field.
@JsonKey(name: 'products') List<Product>? myProducts,
bool? closed,
}) = _Shop;
/// Returns [Shop] based on [json].
factory Shop.fromJson(Map<String, Object?> json) => _$ShopFromJson(json);
}
// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:freezed_annotation/freezed_annotation.dart';
part 'product.freezed.dart';
part 'product.g.dart';
// **************************************************************************
// FreezerGenerator
// **************************************************************************
/// This is a comment for product object.
@freezed
class Product with _$Product {
const factory Product({
String? name,
double? price,
}) = _Product;
/// Returns [Product] based on [json].
factory Product.fromJson(Map<String, Object?> json) =>
_$ProductFromJson(json);
}
And .freezed.dart
and .g.dart
files are automatically generated at the same time.
So, there is even no need to run build_runner
yourself!
1.4. Getting Started 🏄 #
1.4.1. Prerequisite #
The codes automatically generated by this library depend on annotations from json_serializable and freezed.
So, let's add the prerequisite libraries to pubspec.yaml
as follows.
dependencies:
freezed_annotation: ^latest
dev_dependencies:
json_serializable: ^latest
freezed: ^latest
1.4.2. Install Library #
Next, let's install the libraries to use the freezer functionality.
Simply add freezer: ^latest
to your pubspec.yaml
's dev_dependencies
.
Or you can add by command as follows.
With Dart:
dart pub add freezer
With Flutter:
flutter pub add freezer
1.4.3. Create a JSON File #
freezer interprets JSON files as design information and automatically generates object classes supported by the freezed library.
And you need to note following rules when you use the freezer
.
- freezer parses files with the
.freezer.json
extension. - freezer parses JSON files stored in the
.design
folder.
So, now let's create a JSON file with the following structure as a trial.
{
"shop": {
"name.!required": "My Fancy Shop",
"products.!as:product": [
{
"name": "Chocolate",
"price": 5.99,
"product_type": 1
},
{
"name": "Gummy",
"price": 8.99
}
],
"closed": false
}
}
And then store this JSON file in the .design
folder of the root of project.
.
├── analysis_options.yaml
├── design
│ └── sample
│ └── shop.freezer.json
├── lib
├── pubspec.lock
└── pubspec.yaml
1.4.4. Execute Command #
Now let's execute the following command and see what happens!
dart run freezer:main
Then, this trial is successful if the following output is obtained.
Started process for 1 files
[INFO] Reading cached asset graph completed, took 28ms
[INFO] Checking for updates since last build completed, took 297ms
[INFO] Running build completed, took 4.0s
[INFO] Caching finalized dependency graph completed, took 19ms
[INFO] Succeeded after 4.1s with 0 outputs (3 actions)
┏━━ Generated dart files
┃ ┣━━ 🎉 /Users/user/freezer/lib/sample/products.dart
┃ ┣━━ 🎉 /Users/user/freezer/lib/sample/products.freezed.dart
┃ ┣━━ 🎉 /Users/user/freezer/lib/sample/products.g.dart
┃ ┣━━ 🎉 /Users/user/freezer/lib/sample/shop.dart
┃ ┣━━ 🎉 /Users/user/freezer/lib/sample/shop.freezed.dart
┃ ┗━━ 🎉 /Users/user/freezer/lib/sample/shop.g.dart
┗━━ 6 files in 5.5022 seconds
And you can see generated dart codes in the .lib
folder like below.
.
├── analysis_options.yaml
├── design
│ └── sample
│ └── shop.freezer.json
├── lib
│ └── sample
│ ├── product.dart
│ ├── product.freezed.dart
│ ├── product.g.dart
│ ├── shop.dart
│ ├── shop.freezed.dart
│ └── shop.g.dart
├── pubspec.lock
└── pubspec.yaml
1.5. Contribution 🏆 #
If you would like to contribute to freezer, please create an issue or create a Pull Request.
There are many ways to contribute to the OSS. For example, the following subjects can be considered:
- There are request parameters or response fields that are not implemented.
- Documentation is outdated or incomplete.
- Have a better way or idea to achieve the functionality.
- etc...
You can see more details from resources below:
Or you can create a discussion if you like.
Feel free to join this development, diverse opinions make software better!
1.6. Support ❤️ #
The simplest way to show us your support is by giving the project a star at GitHub and Pub.dev.
You can also support this project by becoming a sponsor on GitHub:
1.7. License 🔑 #
All resources of freezer is provided under the BSD-3
license.
Copyright 2022 Kato Shinya. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided the conditions.
Note
License notices in the source are strictly validated based on.github/header-checker-lint.yml
. Please check header-checker-lint.yml for the permitted standards.
1.8. More Information 🧐 #
freezer was designed and implemented by Kato Shinya (@myConsciousness).