Skip to content

Commit d216737

Browse files
committed
可以通过下拉框将对象类型设置为Map<String, dynamic>来避免该对象字段的解析
1 parent cb0b3c5 commit d216737

File tree

6 files changed

+133
-10
lines changed

6 files changed

+133
-10
lines changed

Test/IgnoreMapResp.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'dart:convert' show json;
2+
3+
class IgnoreMapResp {
4+
5+
Data data;
6+
7+
IgnoreMapResp.fromParams({this.data});
8+
9+
factory IgnoreMapResp(jsonStr) => jsonStr == null ? null : jsonStr is String ? new IgnoreMapResp.fromJson(json.decode(jsonStr)) : new IgnoreMapResp.fromJson(jsonStr);
10+
11+
IgnoreMapResp.fromJson(jsonRes) {
12+
data = jsonRes['data'] == null ? null : new Data.fromJson(jsonRes['data']);
13+
}
14+
15+
@override
16+
String toString() {
17+
return '{"data": $data}';
18+
}
19+
}
20+
21+
class Data {
22+
23+
int wc;
24+
String author;
25+
String content;
26+
String digest;
27+
String title;
28+
Map<String, dynamic> date;
29+
Extra extra;
30+
31+
Data.fromParams({this.wc, this.author, this.content, this.digest, this.title, this.date, this.extra});
32+
33+
Data.fromJson(jsonRes) {
34+
wc = jsonRes['wc'];
35+
author = jsonRes['author'];
36+
content = jsonRes['content'];
37+
digest = jsonRes['digest'];
38+
title = jsonRes['title'];
39+
date = jsonRes['date'];
40+
extra = jsonRes['extra'] == null ? null : new Extra.fromJson(jsonRes['extra']);
41+
}
42+
43+
@override
44+
String toString() {
45+
return '{"wc": $wc,"author": ${author != null?'${json.encode(author)}':'null'},"content": ${content != null?'${json.encode(content)}':'null'},"digest": ${digest != null?'${json.encode(digest)}':'null'},"title": ${title != null?'${json.encode(title)}':'null'},"date": ${date != null?'${json.encode(date)}':'null'},"extra": $extra}';
46+
}
47+
}
48+
49+
class Extra {
50+
51+
int a;
52+
int b;
53+
int c;
54+
55+
Extra.fromParams({this.a, this.b, this.c});
56+
57+
Extra.fromJson(jsonRes) {
58+
a = jsonRes['a'];
59+
b = jsonRes['b'];
60+
c = jsonRes['c'];
61+
}
62+
63+
@override
64+
String toString() {
65+
return '{"a": $a,"b": $b,"c": $c}';
66+
}
67+
}
68+

Test/IgnoreMapTest.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"data": {
3+
"author": "顾湘",
4+
"content": "content",
5+
"date": {
6+
"curr": "20180216",
7+
"next": "20180217",
8+
"prev": "20180215"
9+
},
10+
"extra":{
11+
"a": 1,
12+
"b": 2,
13+
"c": 3
14+
},
15+
"digest": "digest",
16+
"title": "新年的求婚者",
17+
"wc": 845
18+
}
19+
}

Test/test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'EmptyResp.dart';
66
import 'RegionResp.dart';
77
import 'WanResp.dart';
88
import 'ListsResp.dart';
9+
import 'IgnoreMapResp.dart';
910

1011
void main() {
1112

@@ -201,12 +202,38 @@ void main() {
201202
expect(jsonRes, json.decode(resp.toString()));
202203
});
203204

205+
test('test ignore map', () {
206+
207+
String str = readFromFile('IgnoreMap');
208+
IgnoreMapResp resp = new IgnoreMapResp(str);
209+
var jsonRes = json.decode(str);
210+
211+
/// 测试传入String和json进行解析的结果是否相同
212+
expect(resp.toString(), new IgnoreMapResp(jsonRes).toString());
213+
214+
/// 逐个字段检查是否与json.decode()的结果相同
215+
expect(resp.data.wc, jsonRes['data']['wc']);
216+
expect(resp.data.author, jsonRes['data']['author']);
217+
expect(resp.data.content, jsonRes['data']['content']);
218+
expect(resp.data.digest, jsonRes['data']['digest']);
219+
expect(resp.data.title, jsonRes['data']['title']);
220+
expect(resp.data.extra.a, jsonRes['data']['extra']['a']);
221+
expect(resp.data.extra.b, jsonRes['data']['extra']['b']);
222+
expect(resp.data.extra.c, jsonRes['data']['extra']['c']);
223+
expect(resp.data.date, jsonRes['data']['date']);
224+
225+
/// 检查bean对象toString还原成json字符串后再交给json解析的结果是否与原始字符串相同
226+
expect(jsonRes, json.decode(resp.toString()));
227+
});
228+
204229

205230
}
206231

207232
String readFromFile(String name) {
208233
String str = new File('${name}Test.json').readAsStringSync();
209234
int index1 = str.indexOf('{');
210235
int index2 = str.indexOf('[');
236+
index1 = index1 == -1 ? 0: index1;
237+
index2 = index2 == -1 ? 0: index2;
211238
return str.substring(min(index1, index2));
212239
}

formatter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding:utf-8 -*-
3-
# @Filename : formater.py
3+
# @Filename : formatter.py
44
# @Date : 18-1-26 下午5:48
55
# @Author : DebuggerX
66
from functools import partial
@@ -13,7 +13,7 @@
1313
import pyperclip
1414

1515
# 定义显示的json格式化字符串的缩进量为4个空格
16-
indent = ' '
16+
indent = ' ' * 4
1717

1818
# 临时存储
1919
last_list_com_box = None
@@ -110,6 +110,7 @@ def get_type_combobox(need_connect, line):
110110

111111
com_box.setCurrentIndex(obj_type)
112112
elif obj_type == 8:
113+
com_box.addItem('Map<String, dynamic>')
113114
com_box.setCurrentText('')
114115
elif obj_type == 9:
115116
com_box.setCurrentText('List<>')
@@ -253,5 +254,5 @@ def custom_ui():
253254
custom_ui()
254255
widget.show()
255256
code = check_version()
256-
widget.setWindowTitle(widget.windowTitle().replace('code',str(code)))
257+
widget.setWindowTitle(widget.windowTitle().replace('code', str(code)))
257258
sys.exit(app.exec_())

tools.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def jformat(inp):
3232

3333

3434
def check_level_type(t):
35+
if t == 'Map<String, dynamic>':
36+
# 不用处理的Map类型
37+
return 0
3538
if t in ('int', 'double', 'bool', 'Object'):
3639
# 普通数据类型
3740
return 1
@@ -109,11 +112,10 @@ def add_param_to_code(code, param):
109112
ps = code.find('${toString}')
110113
code = code[:ps] + to_string + code[ps:]
111114

112-
# 字符串类型处理,只需要修改toString中的输出方式
113-
114115
t_code = check_level_type(t)
115116

116-
if t_code == 2:
117+
# 字符串类型和Map类型处理,只需要修改toString中的输出方式
118+
if t_code in [0, 2]:
117119
code = code.replace(': $%s' % n, ': ${%s != null?\'${json.encode(%s)}\':\'null\'}' % (n, n))
118120

119121
# dict类型处理,只需要修改construction中的输出方式
@@ -227,16 +229,22 @@ def check_and_generate_code(bean):
227229
work_bean = []
228230
global msg_box_ui
229231
msg_box_ui = bean[0][1]
230-
232+
ignore_level = None
231233
for f, t, n in bean:
232234
field_text = f.text()
233235

234236
level = field_text.find('※') // 4
237+
if ignore_level is not None and level > ignore_level:
238+
continue
239+
ignore_level = None
240+
235241
field_text = field_text[field_text.find("》") + 1: field_text.find(":") - 1] if ":" in field_text else ''
236242
type_text = t.currentText() if type(t) is QComboBox else t.toPlainText()
237243
name_text = n.text() if type(n) is QLabel else n.toPlainText()
244+
if type_text == 'Map<String, dynamic>':
245+
ignore_level = level
238246

239-
if type_text.strip() != '':
247+
if type_text.strip() != '' or ignore_level is not None:
240248
work_bean.append([level, field_text, type_text, name_text])
241249
else:
242250
QMessageBox().information(msg_box_ui, "警告", "字段类型未设置", QMessageBox.Ok)

version

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"code": 0.6,
3-
"desc": "1.修复json中字段不存在或者为null时可能导致的bean对象创建错误,也就是各字段处理时加入了null判断\n2.放弃对于普通数据类型数组的生成代码优化\n3.修复List嵌套泛型时由于错误的优化逻辑导致的异常代码\n4.优化清理无用空行"
2+
"code": 0.7,
3+
"desc": "可以通过下拉框将对象类型设置为Map<String, dynamic>来避免该对象字段的解析,参考issue #13\n"
44
}

0 commit comments

Comments
 (0)