json_to_dart_model 0.0.8 json_to_dart_model: ^0.0.8 copied to clipboard
Gernerating Dart model class from Json file.
json_to_dart_model #
Gernerating Dart model class from Json file.
Getting Started #
- import dependencies
dependencies:
json_annotation: ^2.2.0
dev_dependencies:
build_runner: ^1.0.0
json_serializable: ^2.2.0
json_to_dart_model: latest
- create json files in lib/model
- run
- run
pub run build_runner build
(Dart VM project) orflutter packages pub run build_runner build
(Flutter), create files once - run
pub run build_runner watch
(Dart VM project) orflutter packages pub run build_runner watch
(Flutter中) watch and create files when json files changed
Examples #
File: jsons/user.json
{
"@import": ["card.dart", "test_dir/profile.dart"],
"profile": {
"pre": "@JsonKey(ignore: true)",
"type": "Profile"
},
"loved": {
"pre": "@JsonKey(name: '+1')",
"type": "int"
},
"name": "String",
"father": "UserInfo",
"friends": "List<UserInfo>",
"keywords": "List<String>",
"bankCards": "List<Card>",
"age": "int"
}
Run pub run build_runner build
, then you'll see the generated json file.
// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// JsonModel Builder
// **
import 'package:json_annotation/json_annotation.dart';
import 'card.dart';
import 'test_dir/profile.dart';
part 'user_info.g.dart';
@JsonSerializable()
class UserInfo {
UserInfo();
@JsonKey(ignore: true)
Profile profile;
@JsonKey(name: '+1')
int loved;
String name;
UserInfo father;
List<UserInfo> friends;
List<String> keywords;
List<Card> bankCards;
int age;
factory UserInfo.fromJson(Map<String,dynamic> json) => _$UserInfoFromJson(json);
Map<String, dynamic> toJson() => _$UserInfoToJson(this);
}
Json Fields Description #
- @import Classes that need to be imported, this value support String or List.
- @with Use the with keyword to expand the current class
- @extends Use the extends keyword for current inheritance
- other fileds value's type is String,value is the field's type value's type is Map,value["pre"] is json_serializable's annotations、value["type"] is current field's type
Others Description #
- This lib library only generates .dart files from json files, Support for all json_serializable annotations
- By default, the json file in lib/model will exchange. If you need to exchange the files in other directories, set build.yml, and set the converted directory by include and exclude.
targets:
$default:
builders:
json_to_dart_model|jsonBuilder:
generate_for:
include:
- lib/test_build_yml/**