Coder Social home page Coder Social logo

dut3062796s / json_model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flutterchina/json_model

0.0 1.0 0.0 22 KB

Gernerate model class from Json file. 一行命令,通过Json文件生成Dart Model类。

License: Other

Dart 99.27% Smarty 0.73%

json_model's Introduction

Language: English | 中文简体

json_model Pub

Gernerating Dart model class from Json file.

Installing

dev_dependencies:
  json_model: #latest version
  build_runner: ^1.0.0
  json_serializable: ^2.0.0

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

{
  "name":"wendux",
  "father":"$user", //Other class model 
  "friends":"$[]user", // Array  
  "keywords":"$[]String", // Array
  "age":20
}

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';

@JsonSerializable()
class User {
    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",
  "name":"wendux",
  "age":20
}

The generated class is:

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

@JsonSerializable()
class User {
    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);
}

Test:

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",
  "name":"wendux",
  "age":20
}

The generated class:

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

@JsonSerializable()
class User {
    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
}

json_model's People

Contributors

wendux avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.