xson_builder 1.0.4 xson_builder: ^1.0.4 copied to clipboard
A library to help you easily generate dart json bean.
xson_builder
can help you to easily generate dart json bean by json source.
You can also change root class name and class name's suffix.
Here are 2 ways to use this package.
-
generate programmatically with
XsonBuilder().generateAndGetFileContent()
orXsonBuilder().generateAndWriteFile()
. -
generate dart json bean with
@XsonBean
via xson_annotation.
Example #
Generate programmatically #
- Create a instance of
XsonBuilder
. - Get a json string via any way.
- Call method
generateAndWriteFile(outputDir, outputFileName, json)
can auto generate file, call methodgenerateAndGetFileContent(outputFileName, json)
can get file content.
import 'dart:io';
import 'package:xson_builder/xson_builder.dart';
XsonBuilder builder = XsonBuilder();
main() async {
String content = File("./example/json/sample_json.json").readAsStringSync();
builder.generateAndWriteFile("./example", "sample1.dart", content);
}
- Then
sample1.dart
will be generated. - execute build_runner command
pub run build_runner build
. - Then
sample1.g.dart
will be generated.
Generate by build_runner
#
- Depends package
xson_annotation
. - Given a dart file
sample3.dart
with aSample3
class annotated with@XsonBean()
. - Given
@XsonBean()
a json source way, raw json(jsonContent:
) or json file path(jsonFilePath:
). You can only choose one way to generate. - If you use raw json way, you class will be like this:
import 'package:xson_annotation/xson_annotation.dart';
const _json = """
{
"name": "apple",
"price": 60.5,
"color": "yellow"
}
""";
@XsonBean(jsonContent: _json)
class Sample4 {}
- If you use json file path way, you class will be like this:
import 'package:xson_annotation/xson_annotation.dart';
@XsonBean(jsonFilePath: "./example/json/sample_json.json")
class Sample3 {}
- Execute build_runner command
pub run build_runner build
. - Then
sample1.xson.dart
andsample1.xson.g.dart
will be generated.