Skip to content

Commit 14682b7

Browse files
authored
Fix list with string (#31)
* fix: ["some", "string"] will be encode to [some, string] * return null if input json string is '', 'null' or null. * Revert "return null if input json string is '', 'null' or null." This reverts commit bbb8ce1 * update
1 parent 3897c51 commit 14682b7

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

Test/ListWithStringResp.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'dart:convert' show json;
2+
3+
class ListWithStringResp {
4+
List<String> asd;
5+
List<List<String>> qaz;
6+
List<Qwe> zxc;
7+
8+
ListWithStringResp.fromParams({this.asd, this.qaz, this.zxc});
9+
10+
factory ListWithStringResp(jsonStr) => jsonStr == null
11+
? null
12+
: jsonStr is String
13+
? ListWithStringResp.fromJson(json.decode(jsonStr))
14+
: ListWithStringResp.fromJson(jsonStr);
15+
16+
ListWithStringResp.fromJson(jsonRes) {
17+
asd = jsonRes['asd'] == null ? null : [];
18+
19+
for (var asdItem in asd == null ? [] : jsonRes['asd']) {
20+
asd.add(asdItem);
21+
}
22+
23+
qaz = jsonRes['qaz'] == null ? null : [];
24+
25+
for (var qazItem in qaz == null ? [] : jsonRes['qaz']) {
26+
List<String> qazChild = qazItem == null ? null : [];
27+
for (var qazItemItem in qazChild == null ? [] : qazItem) {
28+
qazChild.add(qazItemItem);
29+
}
30+
qaz.add(qazChild);
31+
}
32+
33+
zxc = jsonRes['zxc'] == null ? null : [];
34+
35+
for (var zxcItem in zxc == null ? [] : jsonRes['zxc']) {
36+
zxc.add(zxcItem == null ? null : Qwe.fromJson(zxcItem));
37+
}
38+
}
39+
40+
@override
41+
String toString() {
42+
return '{"asd": ${asd != null ? '${json.encode(asd)}' : 'null'}, "qaz": ${qaz != null ? '${json.encode(qaz)}' : 'null'}, "zxc": $zxc}';
43+
}
44+
}
45+
46+
class Qwe {
47+
String qwe;
48+
49+
Qwe.fromParams({this.qwe});
50+
51+
Qwe.fromJson(jsonRes) {
52+
qwe = jsonRes['qwe'];
53+
}
54+
55+
@override
56+
String toString() {
57+
return '{"qwe": ${qwe != null ? '${json.encode(qwe)}' : 'null'}}';
58+
}
59+
}

Test/ListWithStringTest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"asd": ["asd", "zxc", "qwe"],
3+
"qaz": [["qaz"]],
4+
"zxc": [{"qwe": "qwe"}]
5+
}

Test/test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'WanResp.dart';
88
import 'ListsResp.dart';
99
import 'IgnoreMapResp.dart';
1010
import 'ListTopResp.dart';
11+
import 'ListWithStringResp.dart';
1112

1213
void main() {
1314

@@ -237,6 +238,16 @@ void main() {
237238
expect(resp.list.toString().replaceAll(' ', ''), json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', ''));
238239
});
239240

241+
test('test list with string', () {
242+
String str = readFromFile('ListWithString');
243+
ListWithStringResp resp = ListWithStringResp(str);
244+
245+
var jsonRes = json.decode(str);
246+
/// 测试传入String和json进行解析的结果是否相同
247+
expect(resp.toString(), new ListWithStringResp(jsonRes).toString());
248+
expect(resp.toString().replaceAll(' ', ''), json.encode(jsonRes).replaceAll('\n', '').replaceAll(' ', ''));
249+
});
250+
240251
}
241252

242253
String readFromFile(String name) {

Test/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ env PUB_ALLOW_PRERELEASE_SDK=quiet $(echo $(which flutter) | awk '{print substr(
55

66
# 运行测试
77
echo test start...
8-
$(echo $(which flutter) | awk '{print substr($0,0,length()-7)}')cache/dart-sdk/bin/dart --preview-dart-2 test.dart
8+
$(echo $(which flutter) | awk '{print substr($0,0,length()-7)}')cache/dart-sdk/bin/dart --preview-dart-2 --null_safety test.dart

tools.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ def add_param_to_code(code, param):
121121

122122
t_code = check_level_type(t)
123123

124-
# 字符串类型和Map类型处理,只需要修改toString中的输出方式
125-
if t_code in [0, 2]:
124+
# 字符串类型、List<String>类型和Map类型处理,需要修改toString中的输出方式
125+
if t_code in [0, 2] or 'List<String>' in t:
126126
code = code.replace(': $%s' % n, ': ${%s != null?\'${json.encode(%s)}\':\'null\'}' % (n, n))
127127

128128
# dict类型处理,只需要修改construction中的输出方式
129129
elif t_code == 4:
130130
code = code.replace('jsonRes[\'%s\']' % f, 'jsonRes[\'%s\'] == null ? null : %s.fromJson(jsonRes[\'%s\'])' % (f, t, f))
131131

132132
# list类型处理,只需要修改construction中的输出方式
133-
elif t_code == 3:
133+
if t_code == 3:
134134
list_loop = build_list_construction(t, f, n)
135135

136136
code = code.replace('jsonRes[\'%s\'];' % f, list_loop)
@@ -225,8 +225,8 @@ def generate_code(work_bean):
225225
out_res += (line + '\n')
226226
if first and r'.fromParams({this.' in line:
227227
class_name = line.split(r'.fromParams({this.')[0].strip()
228-
out_res += '\n factory %s(jsonStr) => jsonStr == null ? null : jsonStr is String ? %s.fromJson(json.decode(jsonStr)) : ' \
229-
'%s.fromJson(jsonStr);\n' \
228+
out_res += '\n factory %s(jsonStr) => [\'null\', \'\', null].contains(jsonStr) ? null : ' \
229+
'jsonStr is String ? %s.fromJson(json.decode(jsonStr)) : %s.fromJson(jsonStr);\n' \
230230
% (class_name, class_name, class_name)
231231
first = False
232232
return out_res

0 commit comments

Comments
 (0)