json2freezed 0.1.0
json2freezed: ^0.1.0 copied to clipboard
A command-line generator that creates Freezed Dart model classes from JSON samples.
json2freezed #
json2freezed generates Dart freezed models from JSON samples.
The preferred way to consume this package is through build_runner, so model
files are regenerated as part of the normal Dart or Flutter build workflow. A
CLI is also available for one-off generation.
Install #
For a Dart project, add json2freezed and the build-time generators as dev
dependencies:
dart pub add dev:json2freezed dev:build_runner dev:freezed dev:json_serializable
Generated model files import freezed_annotation and json_annotation. These
are peer dependencies of the generated code, so make sure the consuming package
installs them directly as regular dependencies:
dart pub add freezed_annotation json_annotation
For a Flutter project, use the same dependency split with flutter pub:
flutter pub add dev:json2freezed dev:build_runner dev:freezed dev:json_serializable
flutter pub add freezed_annotation json_annotation
Build Runner Usage #
By default, JSON files under assets/ generate models under lib/generated/:
assets/metro_data_model.json -> lib/generated/metro_data_model.dart
Run:
dart run build_runner build
Configure input and output directories in the consuming package's build.yaml:
targets:
$default:
builders:
json2freezed:
generate_for:
- samples/**/*.json
options:
input_dir: samples
output_dir: lib/models
When overriding paths, keep generate_for aligned with input_dir so
build_runner knows which JSON files should be primary inputs for the builder.
CLI Install #
Install the CLI globally if you want one-off generation outside build_runner:
dart pub global activate json2freezed
Install For Local Development #
dart pub get
dart pub global activate --source path .
Usage #
Generate from a JSON file:
json2freezed <ClassName> <json-file> [path]
The equivalent flag-based form is:
json2freezed <ClassName> --json-file <file> [--path <dir>] [--overwrite]
Generate from an inline JSON string:
json2freezed <ClassName> --json '<json>' [--path <dir>]
Generate from standard input when neither --json nor --json-file is provided:
cat response.json | json2freezed <ClassName> [--path <dir>]
Options #
<ClassName>: Name of the root Dart class to generate. The class name is normalized toPascalCase.<json-file>: Reads the JSON sample from a file.[path]: Writes output into the target directory. If omitted, output is written to the current directory.--json-file <file>: Reads the JSON sample from a file instead of the positional file argument.--json '<json>': Reads the JSON sample from an inline string.--path <dir>: Writes output into the target directory instead of the positional path argument.--overwrite: Replaces an existing generated file.
Output Naming #
The generated Dart class uses PascalCase.
The generated file uses snake_case with a .dart extension. For example:
json2freezed MetroDataModel response.json lib/models
writes:
lib/models/metro_data_model.dart
Generated Model Style #
Generated files are expected to include:
freezed_annotationimport.freezed.dartand.g.dartpartdeclarations- nullable constructor fields with
@JsonKey(name: ...) - nested
@freezedclasses for nested objects and arrays of objects
Run the normal build step in the consuming Dart or Flutter project after generation:
dart run build_runner build