README ¶
json2dart
The json file to dart classes converter
Install
go install github.com/billcoding/json2dart@latest
Usage
json2dart is a converter generate dart class from json file
Usage:
json2dart [flags]
Flags:
--dart-class-name string The dart class name (default "Json2Dart")
--dart-file-dir string The dart file dir
--dart-file-name string The dart file name (default "json2dart.dart")
-h, --help help for json2dart
--json-file-path string The json file path (default "json2dart.json")
Example dart file
/*
Generated by json2dart (https://github.com/billcoding/json2dart)
Created at 2024-01-20T16:03:20
-------------------------------------------------
{
"intV": 100,
"intArrV": [100],
"doubleV": 100.25,
"doubleArrV": [100.25],
"stringV": "helo",
"stringArrV": ["helo"],
"boolV": false,
"boolArrV": [false],
"objV": {
"intV": 100,
"intArrV": [100],
"doubleV": 100.25,
"doubleArrV": [100.25],
"stringV": "helo",
"stringArrV": ["helo"],
"boolV": false,
"boolArrV": [false]
},
"objArrV": [{
"intV": 100,
"intArrV": [100],
"doubleV": 100.25,
"doubleArrV": [100.25],
"stringV": "helo",
"stringArrV": ["helo"],
"boolV": false,
"boolArrV": [false]
}]
}
*/
import 'dart:convert';
class Json2Dart {
final List<bool> boolArrV;
final bool boolV;
final List<double> doubleArrV;
final double doubleV;
final List<int> intArrV;
final int intV;
final List<Json2DartObjArrV> objArrV;
final Json2DartObjV objV;
final List<String> stringArrV;
final String stringV;
const Json2Dart(this.boolArrV, this.boolV, this.doubleArrV, this.doubleV, this.intArrV, this.intV, this.objArrV, this.objV, this.stringArrV, this.stringV);
factory Json2Dart.instance() {
return Json2Dart.fromMap({});
}
factory Json2Dart.fromMap(Map<String, dynamic> m) {
return Json2Dart(
m.containsKey("boolArrV") && m["boolArrV"] != null ? (m["boolArrV"] as List<dynamic>).map((a) => a as bool).toList(): [],
m.containsKey("boolV") && m["boolV"] != null ? m["boolV"]: false,
m.containsKey("doubleArrV") && m["doubleArrV"] != null ? (m["doubleArrV"] as List<dynamic>).map((a) => a as double).toList(): [],
m.containsKey("doubleV") && m["doubleV"] != null ? m["doubleV"]: 0.0,
m.containsKey("intArrV") && m["intArrV"] != null ? (m["intArrV"] as List<dynamic>).map((a) => a as int).toList(): [],
m.containsKey("intV") && m["intV"] != null ? m["intV"]: 0,
m.containsKey("objArrV") && m["objArrV"] != null ? (m["objArrV"] as List<dynamic>).map((a) => a as Map<String, dynamic>).map((a) => Json2DartObjArrV.fromMap(a)).toList(): [],
m.containsKey("objV") && m["objV"] != null ? Json2DartObjV.fromMap(m["objV"]): Json2DartObjV.instance(),
m.containsKey("stringArrV") && m["stringArrV"] != null ? (m["stringArrV"] as List<dynamic>).map((a) => a as String).toList(): [],
m.containsKey("stringV") && m["stringV"] != null ? m["stringV"]: "",
);
}
Map<String, dynamic> toMap() {
return {
"boolArrV": boolArrV,
"boolV": boolV,
"doubleArrV": doubleArrV,
"doubleV": doubleV,
"intArrV": intArrV,
"intV": intV,
"objArrV": objArrV,
"objV": objV,
"stringArrV": stringArrV,
"stringV": stringV,
};
}
Map<String, dynamic> toOriginalMap() {
return {
"boolArrV": boolArrV,
"boolV": boolV,
"doubleArrV": doubleArrV,
"doubleV": doubleV,
"intArrV": intArrV,
"intV": intV,
"objArrV": objArrV,
"objV": objV,
"stringArrV": stringArrV,
"stringV": stringV,
};
}
String toJson() {
return jsonEncode(toMap());
}
String toOriginalJson() {
return jsonEncode(toOriginalMap());
}
@override
String toString() {
return '{boolArrV: $boolArrV, boolV: $boolV, doubleArrV: $doubleArrV, doubleV: $doubleV, intArrV: $intArrV, intV: $intV, objArrV: $objArrV, objV: $objV, stringArrV: $stringArrV, stringV: $stringV}';
}
}
class Json2DartObjArrV {
final List<bool> boolArrV;
final bool boolV;
final List<double> doubleArrV;
final double doubleV;
final List<int> intArrV;
final int intV;
final List<String> stringArrV;
final String stringV;
const Json2DartObjArrV(this.boolArrV, this.boolV, this.doubleArrV, this.doubleV, this.intArrV, this.intV, this.stringArrV, this.stringV);
factory Json2DartObjArrV.instance() {
return Json2DartObjArrV.fromMap({});
}
factory Json2DartObjArrV.fromMap(Map<String, dynamic> m) {
return Json2DartObjArrV(
m.containsKey("boolArrV") && m["boolArrV"] != null ? (m["boolArrV"] as List<dynamic>).map((a) => a as bool).toList(): [],
m.containsKey("boolV") && m["boolV"] != null ? m["boolV"]: false,
m.containsKey("doubleArrV") && m["doubleArrV"] != null ? (m["doubleArrV"] as List<dynamic>).map((a) => a as double).toList(): [],
m.containsKey("doubleV") && m["doubleV"] != null ? m["doubleV"]: 0.0,
m.containsKey("intArrV") && m["intArrV"] != null ? (m["intArrV"] as List<dynamic>).map((a) => a as int).toList(): [],
m.containsKey("intV") && m["intV"] != null ? m["intV"]: 0,
m.containsKey("stringArrV") && m["stringArrV"] != null ? (m["stringArrV"] as List<dynamic>).map((a) => a as String).toList(): [],
m.containsKey("stringV") && m["stringV"] != null ? m["stringV"]: "",
);
}
Map<String, dynamic> toMap() {
return {
"boolArrV": boolArrV,
"boolV": boolV,
"doubleArrV": doubleArrV,
"doubleV": doubleV,
"intArrV": intArrV,
"intV": intV,
"stringArrV": stringArrV,
"stringV": stringV,
};
}
Map<String, dynamic> toOriginalMap() {
return {
"boolArrV": boolArrV,
"boolV": boolV,
"doubleArrV": doubleArrV,
"doubleV": doubleV,
"intArrV": intArrV,
"intV": intV,
"stringArrV": stringArrV,
"stringV": stringV,
};
}
String toJson() {
return jsonEncode(toMap());
}
String toOriginalJson() {
return jsonEncode(toOriginalMap());
}
@override
String toString() {
return '{boolArrV: $boolArrV, boolV: $boolV, doubleArrV: $doubleArrV, doubleV: $doubleV, intArrV: $intArrV, intV: $intV, stringArrV: $stringArrV, stringV: $stringV}';
}
}
class Json2DartObjV {
final List<bool> boolArrV;
final bool boolV;
final List<double> doubleArrV;
final double doubleV;
final List<int> intArrV;
final int intV;
final List<String> stringArrV;
final String stringV;
const Json2DartObjV(this.boolArrV, this.boolV, this.doubleArrV, this.doubleV, this.intArrV, this.intV, this.stringArrV, this.stringV);
factory Json2DartObjV.instance() {
return Json2DartObjV.fromMap({});
}
factory Json2DartObjV.fromMap(Map<String, dynamic> m) {
return Json2DartObjV(
m.containsKey("boolArrV") && m["boolArrV"] != null ? (m["boolArrV"] as List<dynamic>).map((a) => a as bool).toList(): [],
m.containsKey("boolV") && m["boolV"] != null ? m["boolV"]: false,
m.containsKey("doubleArrV") && m["doubleArrV"] != null ? (m["doubleArrV"] as List<dynamic>).map((a) => a as double).toList(): [],
m.containsKey("doubleV") && m["doubleV"] != null ? m["doubleV"]: 0.0,
m.containsKey("intArrV") && m["intArrV"] != null ? (m["intArrV"] as List<dynamic>).map((a) => a as int).toList(): [],
m.containsKey("intV") && m["intV"] != null ? m["intV"]: 0,
m.containsKey("stringArrV") && m["stringArrV"] != null ? (m["stringArrV"] as List<dynamic>).map((a) => a as String).toList(): [],
m.containsKey("stringV") && m["stringV"] != null ? m["stringV"]: "",
);
}
Map<String, dynamic> toMap() {
return {
"boolArrV": boolArrV,
"boolV": boolV,
"doubleArrV": doubleArrV,
"doubleV": doubleV,
"intArrV": intArrV,
"intV": intV,
"stringArrV": stringArrV,
"stringV": stringV,
};
}
Map<String, dynamic> toOriginalMap() {
return {
"boolArrV": boolArrV,
"boolV": boolV,
"doubleArrV": doubleArrV,
"doubleV": doubleV,
"intArrV": intArrV,
"intV": intV,
"stringArrV": stringArrV,
"stringV": stringV,
};
}
String toJson() {
return jsonEncode(toMap());
}
String toOriginalJson() {
return jsonEncode(toOriginalMap());
}
@override
String toString() {
return '{boolArrV: $boolArrV, boolV: $boolV, doubleArrV: $doubleArrV, doubleV: $doubleV, intArrV: $intArrV, intV: $intV, stringArrV: $stringArrV, stringV: $stringV}';
}
}
Documentation ¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.