json2entity 1.0.9

json2entity #

pub version

中文

A tool for converting JSON strings into dart entity classes

support json_serializable.

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

Usage #

Firstly:

dependencies:
  json2entity: ^1.0.8
  1. activate json2entity global.

pub global activate json2entity

  1. run anywhere:

json2entity -j '{"result":1,"msg":"ok"}' -o output/BaseEntity

or

j2e -j '{"result":1,"msg":"ok"}' -o output/BaseEntity

SYNOPSIS:

Usage:
        -j, --json                              Input json string
        -f, --file                              Input json from file
        -o, --output                            Input output file path and name
        -v, --[no-]verbose                      Show verbose
        -s, --[no-]json-serializable-support    Indicates whether json-serializable is supported
        -h, --[no-]help                         Help

Custom #

import 'package:json2entity/json2entity.dart';

main(List<String> args) {
  var jsonStr = '{"result":1,"msg":"ok"}';
  print(Clazz.fromJson(jsonStr).toString()); 
  print(JsonSerializableClazz.fromJson(jsonStr).toString()); 
}

Output:

  class AutoModel {
    num result;
    String msg;
    AutoModel({
    this.result,
    this.msg
    });

    AutoModel.fromJson(Map < String, dynamic > json):
      result=json['result'],
      msg=json['msg'];
      Map <String, dynamic> toJson() => {
      'result':result,
      'msg':msg
    };
  }

  @JsonSerializable()
  class AutoModel {
    num result;
      String msg;
      AutoModel({
      this.result,
      this.msg
    });

    factory AutoModel.fromJson(Map<String, dynamic> json) => _$AutoModelFromJson(json);
      Map<String, dynamic> toJson() => _$AutoModelToJson(this);
    }

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

1.0.9 #

  • Fix bugs

1.0.8 #

  • Fix the problem of losing the key under certain circumstances. Add file overwrite prompt.

1.0.7 #

  • Fixed some 'Health suggestions'

1.0.6 #

  • Cancel the necessary parameter: --output, default output to stdout

1.0.5 #

  • Add doc.

1.0.4 #

  • Init publish.

example/example.dart

// 6种不同类型json测试用例,分别是
// 简单无嵌套json
// 嵌套json
// 嵌套简单列表json
// 嵌套对象列表json
// 对象列表json
// 复杂嵌套json

import 'package:json2entity/json2entity.dart';

var json1 = '{"result":1,"msg":"ok"}';
var json2 = '{"result":1,"msg":"ok","data":{"answer":"A"}}';
var json3 = '{"city":"Mumbai","streets":["address1","address2"]}';
var json4 =
    '{"id":1,"name":"ProductName","images":[{"id":11,"imageName":"xCh-rhy"},{"id":31,"imageName":"fjs-eun"}]}';
var json5 =
    '[{"albumId":1,"id":1,"title":"accusamus beatae ad facilis cum similique qui sunt","url":"http://placehold.it/600/92c952","thumbnailUrl":"http://placehold.it/150/92c952"}]';
var json6 =
    '{"message":"success","data":[{"title":"父亲车祸母亲失踪女孩急等骨髓救命48岁姑父为其寻母8年未间断","gallary_flag":2,"image_list":[{"url":"//p99.pstatp.com/list/364x360/pgc-image/9c14e3a8dd1141bf9e84482ec0a969fd"},{"url":"//p3.pstatp.com/list/272x178/pgc-image/fc93fe97e6254a00a8c54782c368a66a"},{"url":"//p99.pstatp.com/list/272x178/pgc-image/51ea0b3d56484e64bb4173fb40d88941"}],"article_url":"/group/6631418812048605700/","cover_image_url":"//p99.pstatp.com/list/300x170/pgc-image/9c14e3a8dd1141bf9e84482ec0a969fd","gallery_image_count":14},{"title":"清朝12位皇帝真实容貌:道光帝开始脸变小,末代皇帝溥仪如此英俊","gallary_flag":1,"image_list":[{"url":"//p1.pstatp.com/list/640x360/pgc-image/1537076286050672437e773"},{"url":"//p3.pstatp.com/list/pgc-image/1537076286006e0a40f97e3"},{"url":"//p9.pstatp.com/list/pgc-image/1537076334677c121c62849"}],"article_url":"/group/6601692955138851342/","cover_image_url":"//p1.pstatp.com/list/300x170/pgc-image/1537076286050672437e773","gallery_image_count":12},{"title":"全世界最不可思议的十大土豪生活,只有你想不到的,没有做不到的","gallary_flag":1,"image_list":[{"url":"//p3.pstatp.com/list/640x360/pgc-image/15379270782881eb823c455"},{"url":"//p3.pstatp.com/list/pgc-image/1537927078358b5fb372180"},{"url":"//p3.pstatp.com/list/pgc-image/1537927078196fb83166b4a"}],"article_url":"/group/6605348757426930189/","cover_image_url":"//p3.pstatp.com/list/300x170/pgc-image/15379270782881eb823c455","gallery_image_count":10},{"title":"这3个景点都取消了门票费,免费让游客参观,你会考虑去一次吗?","gallary_flag":2,"image_list":[{"url":"//p9.pstatp.com/list/364x360/pgc-image/c3f7ca96ebe24b5380c9ead22f7dcc2b"},{"url":"//p9.pstatp.com/list/272x178/pgc-image/23aa8619a4284c1b88e92c5fe2a9d85e"},{"url":"//p99.pstatp.com/list/272x178/pgc-image/ce7bcc2f39fc477db02e3f122272659e"}],"article_url":"/group/6624419395680600583/","cover_image_url":"//p9.pstatp.com/list/300x170/pgc-image/c3f7ca96ebe24b5380c9ead22f7dcc2b","gallery_image_count":8},{"title":"这三位明星背景强大,几乎没人敢惹","gallary_flag":2,"image_list":[{"url":"//p99.pstatp.com/list/dfic-imagehandler/0a786347-bc90-4db1-b14e-4e569344e402"},{"url":"//p1.pstatp.com/list/dfic-imagehandler/9fb8f421-0c9c-455c-a7fc-2a9e9d90cf80"},{"url":"//p3.pstatp.com/list/dfic-imagehandler/ef3b09a3-b4bb-4463-bb54-d72163d98559"}],"article_url":"/group/6630935274056581636/","cover_image_url":"//p99.pstatp.com/list/dfic-imagehandler/0a786347-bc90-4db1-b14e-4e569344e402","gallery_image_count":3},{"title":"古城修缮:复建中的明太原县城,晋阳古城遗址上的古城文脉传承","gallary_flag":1,"image_list":[{"url":"//p9.pstatp.com/list/640x360/b71f00046c2f82588541"},{"url":"//p3.pstatp.com/list/b71f00046c302e6b6f9d"},{"url":"//p1.pstatp.com/list/b72400031bb4f9fee5bd"}],"article_url":"/group/6601406546641420803/","cover_image_url":"//p9.pstatp.com/list/300x170/b71f00046c2f82588541","gallery_image_count":6},{"title":"像瓜不是瓜,像藕不是藕,农村夫妻祖传手艺制作稀罕物,日赚1000","gallary_flag":1,"image_list":[{"url":"//p99.pstatp.com/list/640x360/pgc-image/15368358594420267044bea"},{"url":"//p3.pstatp.com/list/pgc-image/153683585801878fe338032"},{"url":"//p3.pstatp.com/list/pgc-image/15368358580542cd67b1121"}],"article_url":"/group/6600699719456915976/","cover_image_url":"//p99.pstatp.com/list/300x170/pgc-image/15368358594420267044bea","gallery_image_count":9},{"title":"活佛眼睛(二)","gallary_flag":2,"image_list":[{"url":"//p3.pstatp.com/list/364x360/pgc-image/75e16a0f519b45f0a527b3f9228150ef"},{"url":"//p99.pstatp.com/list/272x178/pgc-image/3154065bef234dfbbc0cd90ac8b765f8"},{"url":"//p99.pstatp.com/list/272x178/pgc-image/7fe76b56fa844f46b4088ba62bf5e118"}],"article_url":"/group/6631057751093035534/","cover_image_url":"//p3.pstatp.com/list/300x170/pgc-image/75e16a0f519b45f0a527b3f9228150ef","gallery_image_count":3}]}';

main(List<String> args) {
  var jsonStr = json1;
  print(Clazz.fromJson(jsonStr).toString());
  /*
  class AutoModel {
    num result;
    String msg;
    AutoModel({
      this.result,
      this.msg
    });

    AutoModel.fromJson(Map < String, dynamic > json):
      result=json['result'],
      msg=json['msg'];
    Map <String, dynamic> toJson() => {
      'result':result,
      'msg':msg
    };
  }
  */
  print(JsonSerializableClazz.fromJson(jsonStr).toString());
  /*
  @JsonSerializable()
  class AutoModel {
    num result;
    String msg;
    AutoModel({
      this.result,
      this.msg
    });

    factory AutoModel.fromJson(Map<String, dynamic> json) => _$AutoModelFromJson(json);
    Map<String, dynamic> toJson() => _$AutoModelToJson(this);
  }
  */
}

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate json2entity

2. Use it

The package has the following executables:


$ j2e
$ json2entity
$ zip_json

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json2entity: ^1.0.9

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json2entity/json2entity.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
76
Learn more about scoring.

We analyzed this package on Sep 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:json2entity/json2entity.dart with components: io.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
args ^1.5.1 1.5.2
dart_style ^1.2.2 1.2.10
path ^1.6.2 1.6.4
Transitive dependencies
async 2.3.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
front_end 0.1.24
glob 1.1.7
html 0.14.0+2
kernel 0.3.24
meta 1.1.7
package_config 1.1.0
pedantic 1.8.0+1
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.1.16
Dev dependencies
analyzer ^0.34.0 0.38.2
build_runner ^1.1.2
json_annotation ^2.0.0
json_serializable ^2.0.1
test ^1.5.1+1