json_model 0.0.2

Language: English | 中文简体

json_model Pub

Gernerating Dart model class from Json file.

Installing #

  json_model: #latest version

Getting Started #

  1. Create a "jsons" directory in the root of your project;
  2. Create a Json file under "jsons" dir ;
  3. Run flutter packages pub run json_model (in Flutter) or pub run json_model (in Dart VM)

Examples #

File: jsons/user.json

  "father":"$user", //Other class model 
  "friends":"$[]user", // Array  
  "keywords":"$[]String", // Array

Run pub run json_model, then you'll see the generated json file under lib/models/ dir:

import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';

class 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);

@JsonKey #

You can also use “@JsonKey” annotation from json_annotation package.

  "@JsonKey(ignore: true) dynamic":"md",
  "@JsonKey(name: '+1') int": "loved",

The generated class is:

import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';

class User {

    @JsonKey(ignore: true) dynamic md;
    @JsonKey(name: '+1') int loved;
    String name;
    num age;
    factory User.fromJson(Map<String,dynamic> json) => _$UserFromJson(json);
    Map<String, dynamic> toJson() => _$UserToJson(this);


import 'models/index.dart';

void main() {
  var u = User.fromJson({"name": "Jack", "age": 16, "+1": 20});
  print(u.loved); // 20

@Import #

  "@import":"test_dir/profile.dart", //import file for model class
  "@JsonKey(ignore: true) Profile":"profile",

The generated class:

import 'package:json_annotation/json_annotation.dart';
import 'test_dir/profile.dart';  // import file
part 'user.g.dart';

class 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);

For completed examples see here .

Command arguments #

The default json source file directory is project_root/jsons; you can custom the src file directory by src argument, for example:

pub run json_model src=json_files 

You can also custom the dist directory by dist argument:

pub run json_model src=json_files  dist=data # will save in lib/data dir

The dist root is lib

Run by code #

If you want to run json_model by code instead command line, you can:

import 'package:json_model/json_model.dart';
void main() {
  run(['src=jsons']);  //run

[0.0.2] - 2019.5.10

  • update doc and demo

Use this package as a library

1. Depend on it

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

  json_model: ^0.0.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

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

3. Import it

Now in your Dart code, you can use:

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

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

  • Dart: 2.4.0
  • pana: 0.12.19


Detected platforms: other

Primary library: package:json_model/json_model.dart with components: io, isolate, build, mirrors.

Health suggestions

Format bin/json_model.dart.

Run dartfmt to format bin/json_model.dart.

Format lib/index.dart.

Run dartfmt to format lib/index.dart.

Format lib/json_model.dart.

Run dartfmt to format lib/json_model.dart.

Maintenance issues and suggestions

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (json_annotation, json_serializable).

The package description is too short. (-16 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and json_model.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
args ^1.5.1 1.5.2
build >=1.0.0 <1.2.0 1.1.6
build_resolvers ^1.0.0 1.0.7
build_runner ^1.0.0 1.6.7
build_runner_core ^3.0.0 3.0.9
glob ^1.1.0 1.1.7
io ^0.3.0 0.3.3
json_annotation ^2.2.0 2.3.0 3.0.0
json_serializable ^2.0.0 2.3.0 3.2.2
logging ^0.11.2 0.11.3+2
path ^1.6.2 1.6.4
Transitive dependencies
analyzer 0.36.4 0.38.1
async 2.3.0
build_config 0.4.1+1
build_daemon 2.0.0
built_collection 4.2.2
built_value 6.7.0
charcode 1.1.2
checked_yaml 1.0.2
code_builder 3.2.0
collection 1.14.12
convert 2.1.1
crypto 2.1.2
csslib 0.16.1
dart_style 1.2.9 1.2.10
fixnum 0.10.9
front_end 0.1.19 0.1.23
graphs 0.2.0
html 0.14.0+2
http 0.12.0+2
http_multi_server 2.1.0
http_parser 3.1.3
js 0.6.1+1
kernel 0.3.19 0.3.23
matcher 0.12.5
meta 1.1.7
mime 0.9.6+3
package_config 1.1.0
package_resolver 1.0.10
pedantic 1.8.0+1
pool 1.4.0
pub_semver 1.4.2
pubspec_parse 0.1.5
quiver 2.0.5
shelf 0.7.5
shelf_web_socket 0.2.3
source_gen 0.9.4+4
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
stream_transform 0.0.19
string_scanner 1.0.5
term_glyph 1.1.0
timing 0.1.1+2
typed_data 1.1.6
watcher 0.9.7+12
web_socket_channel 1.0.15
yaml 2.1.16
Dev dependencies
test ^1.5.1