Skip to content

Commit f269fd7

Browse files
committed
init project
0 parents  commit f269fd7

19 files changed

+684
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Files and directories created by pub
2+
.dart_tool/
3+
.packages
4+
5+
# Omit commiting pubspec.lock for library packages:
6+
# https://dart.dev/guides/libraries/private-files#pubspeclock
7+
pubspec.lock
8+
9+
# Conventional directory for build outputs
10+
build/
11+
12+
# Directory created by dartdoc
13+
doc/api/
14+
15+
.idea/

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# json2model
2+
This library generates a model object corresponding to a JSON string by executing commands in code
3+
4+
## Usage
5+
6+
A simple usage example:
7+
8+
```dart
9+
import 'package:json2model/json2model.dart';
10+
11+
void main() {
12+
run(['--src=example/json', '--dst=example/lib/models']);
13+
}
14+
```
15+
16+
## Features and bugs
17+
18+
Please file feature requests and bugs at the [issue tracker][tracker].
19+
20+
[tracker]: https://github.com/venshine/json2model/issues
21+
22+
## About
23+
24+
25+
## License
26+
[Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0.html)

analysis_options.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Defines a default set of lint rules enforced for
2+
# projects at Google. For details and rationale,
3+
# see https://github.com/dart-lang/pedantic#enabled-lints.
4+
include: package:effective_dart/analysis_options.yaml
5+
6+
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
7+
# Uncomment to specify additional rules.
8+
# linter:
9+
# rules:
10+
# - camel_case_types
11+
12+
analyzer:
13+
# exclude:
14+
# - path/to/excluded/files/**
15+
:

cmd.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub global activate --source path .
2+
json2model run --src=example/json --dst=example/lib/models

example/json/test.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"aListOfInts": [
3+
1,
4+
2,
5+
3
6+
],
7+
"list1": {
8+
"list2": [
9+
true,
10+
false
11+
],
12+
"list3": [
13+
8.5,
14+
"2.3"
15+
],
16+
"list4": null
17+
}
18+
}

example/json/testarray.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"md5": "fc15dcab3217608a7e31d74a893778a0",
4+
"height": "120",
5+
"width": "90",
6+
"type": "image",
7+
"format": [
8+
{
9+
"type": "String",
10+
"name": "image1"
11+
},
12+
{
13+
"type": "String",
14+
"name": "image2"
15+
}
16+
],
17+
"url": "https://github.com/venshine"
18+
}
19+
]

example/json2model_example.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'package:json2model/json2model.dart';
2+
3+
void main() {
4+
run(['--src=example/json', '--dst=example/lib/models']);
5+
}

example/lib/models/json/test.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'dart:convert' show json;
2+
Test testFromJson(String str) => Test.fromJson(json.decode(str));
3+
String testToJson(Test data) => json.encode(data.toJson());
4+
5+
class Test {
6+
Test({
7+
this.aListOfInts,
8+
this.list1
9+
});
10+
final List<int> aListOfInts;
11+
final List1 list1;
12+
13+
factory Test.fromJson(Map<String, dynamic> json) => Test(
14+
aListOfInts: json['aListOfInts'] == null ? null : List<int>.from(json['aListOfInts'].map((x) => x)),
15+
list1: json['list1'] == null ? null : List1.fromJson(json['list1'])
16+
);
17+
18+
Map<String, dynamic> toJson() => {
19+
'aListOfInts': aListOfInts == null ? null : List<dynamic>.from(aListOfInts.map((x) => x)),
20+
'list1': list1 == null ? null : list1.toJson()
21+
};
22+
23+
}
24+
25+
26+
class List1 {
27+
List1({
28+
this.list2,
29+
this.list3,
30+
this.list4
31+
});
32+
final List<bool> list2;
33+
final List<dynamic> list3;
34+
final dynamic list4;
35+
36+
factory List1.fromJson(Map<String, dynamic> json) => List1(
37+
list2: json['list2'] == null ? null : List<bool>.from(json['list2'].map((x) => x)),
38+
list3: json['list3'] == null ? null : List<dynamic>.from(json['list3'].map((x) => x)),
39+
list4: json['list4']
40+
);
41+
42+
Map<String, dynamic> toJson() => {
43+
'list2': list2 == null ? null : List<dynamic>.from(list2.map((x) => x)),
44+
'list3': list3 == null ? null : List<dynamic>.from(list3.map((x) => x)),
45+
'list4': list4
46+
};
47+
48+
}
49+

example/lib/models/json/testarray.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'dart:convert' show json;
2+
List<Testarray> testarrayFromJson(String str) => List<Testarray>.from(json.decode(str).map((x) => Testarray.fromJson(x)));
3+
String testarrayToJson(List<Testarray> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
4+
5+
class Testarray {
6+
Testarray({
7+
this.md5,
8+
this.height,
9+
this.width,
10+
this.type,
11+
this.format,
12+
this.url
13+
});
14+
final String md5;
15+
final String height;
16+
final String width;
17+
final String type;
18+
final List<Format> format;
19+
final String url;
20+
21+
factory Testarray.fromJson(Map<String, dynamic> json) => Testarray(
22+
md5: json['md5'],
23+
height: json['height'],
24+
width: json['width'],
25+
type: json['type'],
26+
format: json['format'] == null ? null : List<Format>.from(json['format'].map((x) => Format.fromJson(x))),
27+
url: json['url']
28+
);
29+
30+
Map<String, dynamic> toJson() => {
31+
'md5': md5,
32+
'height': height,
33+
'width': width,
34+
'type': type,
35+
'format': format == null ? null : List<dynamic>.from(format.map((x) => x.toJson())),
36+
'url': url
37+
};
38+
39+
}
40+
41+
42+
class Format {
43+
Format({
44+
this.type,
45+
this.name
46+
});
47+
final String type;
48+
final String name;
49+
50+
factory Format.fromJson(Map<String, dynamic> json) => Format(
51+
type: json['type'],
52+
name: json['name']
53+
);
54+
55+
Map<String, dynamic> toJson() => {
56+
'type': type,
57+
'name': name
58+
};
59+
60+
}
61+

json2model.iml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
7+
<excludeFolder url="file://$MODULE_DIR$/.pub" />
8+
<excludeFolder url="file://$MODULE_DIR$/build" />
9+
</content>
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="library" name="Dart SDK" level="project" />
12+
<orderEntry type="library" name="Dart Packages" level="project" />
13+
</component>
14+
</module>

lib/json2model.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library json2model;
2+
3+
export 'src/json_model.dart';
4+
export 'src/run.dart';

lib/src/create_dart_class.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'type.dart';
2+
3+
String _fromJson(String className, List<DartType> fieldNames) => """
4+
factory $className.fromJson(Map<String, dynamic> json) => $className(
5+
${fieldNames.map((e) {
6+
var v = "";
7+
switch (e.type) {
8+
case Type.ARRAY:
9+
switch (e.className) {
10+
case "int":
11+
case "double":
12+
case "bool":
13+
case "String":
14+
case "dynamic":
15+
v = "json['${e.key}'] == null ? null : List<${e.className}>.from(json['${e.key}'].map((x) => x))";
16+
break;
17+
default:
18+
v = "json['${e.key}'] == null ? null : List<${e.className}>.from(json['${e.key}'].map((x) => ${e.className}.fromJson(x)))";
19+
break;
20+
}
21+
break;
22+
case Type.OBJECT:
23+
v = "json['${e.key}'] == null ? null : ${e.className}.fromJson(json['${e.key}'])";
24+
break;
25+
case Type.COMMON:
26+
default:
27+
v = "json['${e.key}']";
28+
break;
29+
}
30+
return '${e.key}: $v';
31+
}).join(',\n\t\t')}
32+
);
33+
""";
34+
35+
String _toJson(List<DartType> fieldNames) => """
36+
Map<String, dynamic> toJson() => {
37+
${fieldNames.map((e) {
38+
var v = "";
39+
switch (e.type) {
40+
case Type.ARRAY:
41+
switch (e.className) {
42+
case "int":
43+
case "double":
44+
case "bool":
45+
case "String":
46+
case "dynamic":
47+
v = "${e.key} == null ? null : List<dynamic>.from(${e.key}.map((x) => x))";
48+
break;
49+
default:
50+
v = "${e.key} == null ? null : List<dynamic>.from(${e.key}.map((x) => x.toJson()))";
51+
break;
52+
}
53+
break;
54+
case Type.OBJECT:
55+
v = "${e.key} == null ? null : ${e.key}.toJson()";
56+
break;
57+
case Type.COMMON:
58+
default:
59+
v = e.key;
60+
break;
61+
}
62+
return "'${e.key}': $v";
63+
}).join(',\n\t\t')}
64+
};
65+
""";
66+
67+
String createDartClass({String import, String decoder, String className, String attrs, List<DartType> fieldNames}) {
68+
var classOut = """
69+
$import
70+
$decoder
71+
""";
72+
var classIn = """
73+
class $className {
74+
$className({
75+
${fieldNames.map((e) => 'this.${e.key}').join(',\n\t\t')}
76+
});
77+
\r$attrs
78+
${_fromJson(className, fieldNames)}
79+
${_toJson(fieldNames)}
80+
}
81+
""";
82+
return classOut + (className.isNotEmpty ? classIn : '');
83+
}

0 commit comments

Comments
 (0)