ToJson () and fromJson methods are automatically generated using build_Runner and jSON_serialIZABLE commands and annotations. The biggest problem with this approach is that it requires the developer to manually write a Model class. In a single project, we might need so many Model classes that it would be cumbersome and uninteresting to do them all manually and repeatedly. To solve this problem, we have created a new development tool, Package Json_model, that can convert Json files into Dart classes with a single command line, without having to write the Dart classes manually.
The installation
Please refer to the Github documentation.
use
- Create a directory named “jsons” in the project root directory.
- Create/copy Json file to “jsons” directory
- run
pub run json_model
Dart VM Project orflutter packages pub run json_model
The Dart Model class is generated by the command Flutter. The generated files are in the “lib/ Models “directory by default
example
Json file: jsons/user.json
{
"name":"wendux"."father":"$user".// Other Model classes can be referenced with the "$" symbol
"friends":"$[]user".// Arrays can be referenced with "$[]"
"keywords":"$[]String"./ / same as above
"age":20
}
Copy the code
The generated Dart Model class:
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable(a)class User {
User();
String name;
User father;
List<User> friends;
List<String> keywords;
num age;
factory User.fromJson(Map<String.dynamic> json) => _$UserFromJson(json);
Map<String.dynamic> toJson() => _$UserToJson(this);
}
Copy the code
@JsonKey
You can also annotate specific fields using the “@jsonKey” in the JSON_Annotation package.
This function is useful in certain scenarios. For example, a Json file has a field named “+1”. After being converted to the Dart class, the field name will be treated as the variable name.
{
"@JsonKey(ignore: true) dynamic":"md"."@JsonKey(name: '+1') int": "loved".// Map '+1' to 'loved'
"name":"wendux"."age":20
}
Copy the code
The generated file is as follows:
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable(a)class User {
User();
@JsonKey(name: '+ 1') int loved;
String name;
num age;
factory User.fromJson(Map<String.dynamic> json) => _$UserFromJson(json);
Map<String.dynamic> toJson() => _$UserToJson(this);
}
Copy the code
Testing:
import 'models/index.dart';
void main() {
var u = User.fromJson({"name": "Jack"."age": 16."+ 1": 20});
print(u.loved); / / 20
}
Copy the code
See the Json_Annotation package for details on the @JsonKey annotation;
@Import
In addition, an @import directive is provided that imports the specified file in the generated Dart class:
{
"@import":"test_dir/profile.dart"."@JsonKey(ignore: true) Profile":"profile"."name":"wendux"."age":20
}
Copy the code
The generated Dart class:
import 'package:json_annotation/json_annotation.dart';
import 'test_dir/profile.dart'; // The command takes effect
part 'user.g.dart';
@JsonSerializable(a)class User {
User();
@JsonKey(ignore: true) Profile profile; //file
String name;
num age;
factory User.fromJson(Map<String.dynamic> json) => _$UserFromJson(json);
Map<String.dynamic> toJson() => _$UserToJson(this);
}
Copy the code
A more complete example can be found here.
The command parameter
The default source JSON file directory is a directory named “JSON” in the root directory. You can customize the source JSON file directory with the SRC parameter, for example:
pub run json_model src=json_files
Copy the code
The default build directory is “lib/models”, and the output directory can also be customized with the dist argument:
Pub run json_model SRC =json_files dist=dataCopy the code
Note that dist has lib as the root directory by default.
Code calls
If you are developing a tool and want to use jSON_model in your code, you cannot call jSON_model from the command line. Instead, you can call jSON_model from your code:
import 'package:json_model/json_model.dart';
void main() {
run(['src=jsons']); // The run method is the json_model exposed method;
}
Copy the code
Contrast with visual generation tools
There are also IDE plug-ins that provide Json to Dart class functionality compared to Json_model:
- Json_model needs to maintain a separate Json file folder. If changes are made, just modify the Json file to regenerate the Model class. IDE plug-ins generally require the user to manually copy the Json content into an input box, so that the generated Json file is not archived, and subsequent changes need to be made manually.
- Json_model can manually specify other Model classes that are referenced by a field, avoiding the generation of duplicate classes; IDE plug-ins typically generate a separate Model class for all nested objects in a Json file, even though those nested objects may have been generated in other Model classes.
- Json_model provides command-line transformations that can be easily integrated into non-UI environments such as CI.
The last
If you find it useful, welcome Star, Github Json_model project address.