项目作者: laxian

项目描述 :
Dart语言的json转实体类工具 - Convert json to entity class for Dart and flutter
高级语言: Dart
项目地址: git://github.com/laxian/dart-json2entity.git
创建时间: 2018-10-31T15:12:26Z
项目社区:https://github.com/laxian/dart-json2entity

开源协议:Apache License 2.0

下载


json2entity


pub version

中文

A tool for converting JSON strings into dart entity classes

support json_serializable.

无需手写,自动生成Flutter/Dart实体类文件 — 掘金

Usage

    1. dependencies
      1. dependencies:
      2. json2entity: ^1.0.9
    1. activate json2entity global.

pub global activate json2entity

    1. run anywhere:
  1. # output to stdout
  2. j2e -j '{"result":1,"msg":"ok"}'
  3. # output to file
  4. j2e -j '{"result":1,"msg":"ok"}' -o output/BaseEntity

Windows users:

If you are a Windows user, you need to escape the JSON string you entered

j2e -j '{\"result\":1,\"msg\":\"ok\"}'

SYNOPSIS:

  1. Usage:
  2. -j, --json Input json string
  3. -f, --file Input json from file
  4. -o, --output Input output file path and name
  5. -v, --[no-]verbose Show verbose
  6. -s, --[no-]json-serializable-support Indicates whether json-serializable is supported
  7. -c, --[no-]camelize convert underscore to camel case
  8. -h, --[no-]help Help

Custom

  1. import 'package:json2entity/json2entity.dart';
  2. main(List<String> args) {
  3. var jsonStr = '{"result":1,"msg":"ok"}';
  4. print(Clazz.fromJson(jsonStr).toString());
  5. print(JsonSerializableClazz.fromJson(jsonStr).toString());
  6. }

Output:

  1. class AutoModel {
  2. num result;
  3. String msg;
  4. AutoModel({
  5. this.result,
  6. this.msg
  7. });
  8. AutoModel.fromJson(Map < String, dynamic > json):
  9. result=json['result'],
  10. msg=json['msg'];
  11. Map <String, dynamic> toJson() => {
  12. 'result':result,
  13. 'msg':msg
  14. };
  15. }
  16. @JsonSerializable()
  17. class AutoModel {
  18. num result;
  19. String msg;
  20. AutoModel({
  21. this.result,
  22. this.msg
  23. });
  24. factory AutoModel.fromJson(Map<String, dynamic> json) => _$AutoModelFromJson(json);
  25. Map<String, dynamic> toJson() => _$AutoModelToJson(this);
  26. }

Main classes

Clazz

Converting JSON strings into entity classes.

JsonSerializableClazz

Inheriting from Clazz, transforming JSON strings into entity classes that support json_serializable

Example

wiki

Test

./test_all.sh