-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
182 lines (155 loc) · 4.23 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import 'package:just_jsonify_annotation/jsonify_annotation.dart';
@JsonAble()
class Inner with JsonModel<Inner> {
bool inner;
Inner({this.inner});
static createInner() => Inner();
}
@JsonAble()
class Response with JsonModel<Response> {
int code;
@JsonProperty(name: 'msg', toJson: encode, nullable: true)
String message;
Inner inner;
Response({this.code, this.message, this.inner});
@pragma('vm:entry-point')
static String encode(String decode) {
return decode + decode;
}
}
@JsonAble()
class Annother with JsonModel<Annother> {
@JsonIgnore()
int code;
@JsonProperty(defaultValue: 'ahhhhhh')
String message;
Annother({this.code, this.message});
}
@JsonAble()
class Generic<T extends JsonModel<T>> with JsonModel<Generic<T>> {
final int code;
@JsonProperty(constructors: [Inner.createInner])
final T body;
Generic({this.code, this.body});
}
//@JsonAble()
//class GenericPrimary<T> with JsonModel<GenericPrimary<T>> {
// final int code;
// @JsonProperty(toJson: to)
// final T body;
//
// GenericPrimary({this.code, this.body});
//
// @pragma('vm:entry-point')
// static String to<T>(T body) {
// if (body is String) {
// return body;
// }
//
// print(body.runtimeType);
// print(T.toString());
//
// return 'non-empty';
// }
//}
//
//@JsonAble()
//class ListModel with JsonModel<ListModel> {
// ListModel({this.ints, this.inners});
//
// final List<int> ints;
// final List<Response> inners;
//}
bool listEquals<T>(List<T> a, List<T> b) {
if (a == null) return b == null;
if (b == null || a.length != b.length) return false;
if (identical(a, b)) return true;
for (int index = 0; index < a.length; index += 1) {
if (a[index] != b[index]) return false;
}
return true;
}
bool mapEquals<T, U>(Map<T, U> a, Map<T, U> b) {
if (a == null) return b == null;
if (b == null || a.length != b.length) return false;
if (identical(a, b)) return true;
for (T key in a.keys) {
if (b[key] is Map &&
a[key] is Map &&
mapEquals(b[key] as Map, a[key] as Map)) {
continue;
}
if (b[key] is List &&
a[key] is List &&
listEquals(b[key] as List, a[key] as List)) {
continue;
}
if (!b.containsKey(key) || b[key] != a[key]) {
return false;
}
}
return true;
}
void expect(String testName, Map actual, Map expected) {
if (!mapEquals(actual, expected)) {
print('[Test] $testName error.');
throw 'Expect Error in with actual: $actual expected: $expected';
} else {
print('[Test] $testName passed');
}
}
void testIt() {
{
final Inner inner = Inner(inner: true);
expect('Simple Test', inner.toJson(), {'inner': true});
}
/// JsonAble Recursive.
{
final Inner inner = Inner(inner: true);
final Response res = Response()
..code = 200
..message = 'message'
..inner = inner;
expect('Recurive Model Test', res.toJson(), {
'code': 200,
'msg': 'messagemessage',
'inner': {'inner': true},
});
}
final Map<String, dynamic> mmp = {};
mmp['code'] = 100;
mmp['message'] = 'message';
// mmp['inner'] = null;
final Map<String, dynamic> innermmp = {};
innermmp['inner'] = true;
mmp['inner'] = innermmp;
try {
// ignore: unnecessary_parenthesis
print(Response().fromJson(mmp).toJson());
print(Response().fromJson(mmp).code);
} catch (e, s) {
print(e);
print(s);
}
final Annother anno = Annother()
..code = 100
..message = 'fuccccck';
print(anno.toJson());
final Annother anno1 = Annother()..code = 100;
print(anno1.toJson());
final Map<String, dynamic> annoMap = {};
annoMap['code'] = 100;
annoMap['message'] = 'msg';
print(Annother().fromJson(annoMap).toJson());
print(Annother().fromJson({'code': 100, 'message': null}).toJson());
// final GenericInner generic =
// GenericInner(code: 100, body: Inner(inner: true));
// print(generic.toJson());
// final GenericPrimary<String> genericPrimary =
// GenericPrimary<String>(code: 100, body: 'body');
// print(genericPrimary.toJson());
//
// final ListModel listModel = ListModel(ints: [0, 1, 2, 3, 4, 5], inners: [Response(inner: Inner(inner: true)), Response(inner: Inner(inner: false))]);
// print(json.encode(listModel.toJson()));
}
void main() => testIt();