json_to_dart_model 0.0.8

Language: English | 中文简体

json_to_dart_model Pub

Gernerating Dart model class from Json file.

Getting Started #

  1. import dependencies
  json_annotation: ^2.2.0

  build_runner: ^1.0.0
  json_serializable: ^2.2.0
  json_to_dart_model: latest
  1. create json files in lib/model
  2. run
  3. run pub run build_runner build (Dart VM project) or flutter packages pub run build_runner build(Flutter), create files once
  4. run pub run build_runner watch (Dart VM project) or flutter packages pub run build_runner watch(Flutter中) watch and create files when json files changed

Examples #

File: jsons/user.json

  "@import": ["card.dart", "test_dir/profile.dart"],
  "profile": {
    "pre": "@JsonKey(ignore: true)",
    "type": "Profile"
  "loved": {
    "pre": "@JsonKey(name: '+1')",
    "type": "int"
  "name": "String",
  "father": "UserInfo",
  "friends": "List<UserInfo>",
  "keywords": "List<String>",
  "bankCards": "List<Card>",
  "age": "int"

Run pub run build_runner build, then you'll see the generated json file.

// **************************************************************************
// JsonModel Builder
// **
import 'package:json_annotation/json_annotation.dart';
import 'card.dart';
import 'test_dir/profile.dart';
part 'user_info.g.dart';

class UserInfo  {

  @JsonKey(ignore: true)
  Profile profile;

  @JsonKey(name: '+1')
  int loved;

  String name;

  UserInfo father;

  List<UserInfo> friends;

  List<String> keywords;

  List<Card> bankCards;

  int age;

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

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

Json Fields Description #

  1. @import Classes that need to be imported, this value support String or List.
  2. @with Use the with keyword to expand the current class
  3. @extends Use the extends keyword for current inheritance
  4. other fileds value's type is String,value is the field's type value's type is Map,value["pre"] is json_serializable's annotations、value["type"] is current field's type

Others Description #

  1. This lib library only generates .dart files from json files, Support for all json_serializable annotations
  2. By default, the json file in lib/model will exchange. If you need to exchange the files in other directories, set build.yml, and set the converted directory by include and exclude.
            - lib/test_build_yml/**

[0.0.8] - 2019.11.03

  1. 默认生成的类的字段为 final

Use this package as a library

1. Depend on it

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

  json_to_dart_model: ^0.0.8

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:json_to_dart_model/builder.dart';
import 'package:json_to_dart_model/create_template_class.dart';
import 'package:json_to_dart_model/json_builder.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 Feb 13, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Format lib/create_template_class.dart.

Run dartfmt to format lib/create_template_class.dart.

Format lib/json_builder.dart.

Run dartfmt to format lib/json_builder.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (build).

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.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

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

For more information see the pub package layout conventions.

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.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
build >=1.0.0 <1.2.0 1.1.6 1.2.2
path ^1.6.2 1.6.4
Transitive dependencies
analyzer 0.38.5 0.39.4
args 1.5.2
async 2.4.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
front_end 0.1.27 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.27 0.3.29
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0 2.0.0
pedantic 1.9.0
pub_semver 1.4.3
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.3.1
test ^1.5.1