json_to_dart_model 0.0.8 copy "json_to_dart_model: ^0.0.8" to clipboard
json_to_dart_model: ^0.0.8 copied to clipboard

Gernerating Dart model class from Json file.

Language: English | 中文简体

json_to_dart_model Pub #

Gernerating Dart model class from Json file.

Getting Started #

  1. import dependencies
dependencies:
  json_annotation: ^2.2.0

dev_dependencies:
  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.

// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// JsonModel Builder
// **
import 'package:json_annotation/json_annotation.dart';
import 'card.dart';
import 'test_dir/profile.dart';
part 'user_info.g.dart';

@JsonSerializable()
class UserInfo  {
  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.
targets:
  $default:
    builders:
      json_to_dart_model|jsonBuilder:
        generate_for:
          include:
            - lib/test_build_yml/**

1
likes
5
pub points
14%
popularity

Publisher

unverified uploader

Gernerating Dart model class from Json file.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

build, path

More

Packages that depend on json_to_dart_model