fson

Last updated:

0 purchases

fson Image
fson Images
Add to Cart

Description:

fson

fson #
只用一行命令,直接将Json文件转为Dart model类。
安装 #
flutter pub global activate fson 或者 pub global activate fson
dev_dependencies:
build_runner: any
json_serializable: any
copied to clipboard
使用 #

在工程根目录下创建一个名为 "jsons" 的目录;
创建或拷贝Json文件到"jsons" 目录中 ;
运行 fson 命令生成Dart model类,生成的文件默认在"lib/models"目录下

特殊 Key #
"_"开头 #
以 _ 开头的 Key 将被 fson 过滤
@import #

在生成的Dart类中导入指定的文件

{
"@import": "test/index.dart",
"name": "fson"
}

// ------------------------------------------------------------

import 'package:json_annotation/json_annotation.dart';
import 'index.dart';
import 'test/index.dart';

part 'test.g.dart';

@JsonSerializable()
class Test {
Test({
this.name,
});

String name;

factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

Map<String, dynamic> toJson() => _$TestToJson(this);
}
copied to clipboard
特殊 Value #
@data #
{
"data": "@data",
"name": "fson"
}

// ------------------------------------------------------------

import 'package:json_annotation/json_annotation.dart';
import 'index.dart';

part 'test.g.dart';

@JsonSerializable()
class Test<T> {
Test({
this.name,
this.data,
});

String name;

@ResultConverter()
T? data;

factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

Map<String, dynamic> toJson() => _$TestToJson(this);
}

@JsonSerializable()
class TestL<T> {
TestL({
this.name,
this.data,
});

String name;

@ResultConverter()
List<T>? data;

factory TestL.fromJson(Map<String, dynamic> json) => _$TestLFromJson(json);

Map<String, dynamic> toJson() => _$TestLToJson(this);
}

copied to clipboard
"$" 开头 #
$ 为 tag 值 ,可修改。
{
"age": "$int",
"name": "fson"
}

// ------------------------------------------------------------

import 'package:json_annotation/json_annotation.dart';
import 'index.dart';

part 'test.g.dart';

@JsonSerializable()
class Test {
Test({
this.age,
this.name,
});

int age;
String name;

factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

Map<String, dynamic> toJson() => _$TestToJson(this);
}
copied to clipboard
"$[]" 开头 #
$ 为 tag 值 ,可修改。( -t, --tag Specify the tag )
{
"age": "$[]int",
"name": "fson"
}

// ------------------------------------------------------------

import 'package:json_annotation/json_annotation.dart';
import 'index.dart';

part 'test.g.dart';

@JsonSerializable()
class Test {
Test({
this.age,
this.name,
});

List<int> age;
String name;

factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

Map<String, dynamic> toJson() => _$TestToJson(this);
}

copied to clipboard
以 "@" 开头 #
您也可以使用json_annotation包中的“@JsonKey”标注特定的字段。
{
"name": "@JsonKey() String"
}

// ------------------------------------------------------------

import 'package:json_annotation/json_annotation.dart';
import 'index.dart';

part 'test.g.dart';

@JsonSerializable()
class Test {
Test({
this.name,
});

@JsonKey()
String name;

factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);

Map<String, dynamic> toJson() => _$TestToJson(this);
}

copied to clipboard
命令参数 #
默认的源json文件目录为根目录下名为 "json" 的目录;可以通过 src 参数自定义源json文件目录,可以通过dist 参数来自定义输出目录,例如:
fson src=json_files dist=data # 输出目录为 lib/data
copied to clipboard
help #
-s, --src Specify the json directory.
(defaults to "./jsons")
-d, --dist Specify the dist directory.
(defaults to "lib/models")
-t, --tag Specify the tag
(defaults to "$")
-h, --help help
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.