Skip to content

Commit 8406bac

Browse files
committed
Server:同步eclipse版至idea版
1 parent 25dde1b commit 8406bac

File tree

3 files changed

+60
-36
lines changed

3 files changed

+60
-36
lines changed

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/ObjectParser.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public ObjectParser parse() throws Exception {
155155
key = entry.getKey();
156156

157157
if (value instanceof JSONObject) {//JSONObject,往下一级提取
158-
putChild(key, onChildParse(key, (JSON) value));
158+
putChild(key, (JSON) value);
159159
} else if (method == PUT && value instanceof JSONArray) {//PUT JSONArray
160160
onPUTArrayParse(key, (JSONArray) value);
161161
} else {//JSONArray或其它Object,直接填充
@@ -186,10 +186,10 @@ public ObjectParser parse() throws Exception {
186186
*/
187187
protected void putChild(String key, JSON child) throws Exception {
188188
if (child != null) {
189-
if (childMap != null) {
189+
if (childMap != null) {//添加到childMap,最后再解析
190190
childMap.put(key, child);
191-
} else {
192-
response.put(key, child);
191+
} else {//直接解析并替换原来的
192+
response.put(key, onChildParse(key, child));
193193
}
194194
}
195195
}
@@ -408,14 +408,21 @@ public ObjectParser executeSQL(int count, int page, int position) throws Excepti
408408
* @throws Exception
409409
*/
410410
public JSONObject response() throws Exception {
411-
if (sqlReponse != null) {
411+
if (sqlReponse == null || sqlReponse.isEmpty()) {
412+
if (isTableKey) {//Table自身都获取不到值,则里面的Child都无意义,不需要再解析
413+
return response;
414+
}
415+
} else {
412416
response.putAll(sqlReponse);
413417
}
414418

415-
if (customMap != null) {//把isTableKey时取出去的custom重新添加回来
419+
//把isTableKey时取出去的custom重新添加回来
420+
if (customMap != null) {
416421
response.putAll(customMap);
417422
}
418-
if (functionMap != null) {//解析函数function
423+
424+
//解析函数function
425+
if (functionMap != null) {
419426
Set<Entry<String, String>> functionSet = functionMap == null ? null : functionMap.entrySet();
420427
if (functionSet != null && functionSet.isEmpty() == false) {
421428
for (Entry<String, String> entry : functionSet) {
@@ -425,10 +432,16 @@ public JSONObject response() throws Exception {
425432
}
426433
}
427434

428-
if (childMap != null) {//把isTableKey时取出去的child重新添加回来
429-
response.putAll(childMap);
435+
//把isTableKey时取出去child解析后重新添加回来
436+
Set<Entry<String, JSON>> set = childMap == null ? null : childMap.entrySet();
437+
if (set != null) {
438+
for (Entry<String, JSON> entry : set) {
439+
if (entry != null) {
440+
response.put(entry.getKey(), onChildParse(entry.getKey(), entry.getValue()));
441+
}
442+
}
430443
}
431-
444+
432445
onComplete();
433446

434447
return response;

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/Parser.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,6 @@ public com.alibaba.fastjson.JSON parseChild(@NotNull String path, @NotNull Strin
485485
return response;
486486
}
487487

488-
489-
/**
490-
* TODO 一次获取QueryConfig的方式减少了count-1次JSONObject->QueryConfig的过程,大幅提升了性能。
491-
* 但导致第一个Table位置被重新put到最后,并且需要重复getObject内的代码,目前[]中第一个Table还缺少对key()和自定义@key的支持。
492-
* 评估下[]:{FirstTableKey:{Table}}第一个Table需要使用这两种功能符的场景、频率和替代方式(外层?)
493-
*/
494488
/**获取对象数组,该对象数组处于parentObject内
495489
* @param parentPath parentObject的路径
496490
* @param name parentObject的key
@@ -501,8 +495,8 @@ public com.alibaba.fastjson.JSON parseChild(@NotNull String path, @NotNull Strin
501495
private JSONArray getArray(String parentPath, String name, final JSONObject request) throws Exception {
502496
Log.i(TAG, "\n\n\n getArray parentPath = " + parentPath
503497
+ "; name = " + name + "; request = " + JSON.toJSONString(request));
504-
if (isHeadMethod(requestMethod, true)) {
505-
throw new UnsupportedOperationException("HEAD、POST_HEAD方法不允许重复查询!不应该传 " + name + " 等key[]:{}!");
498+
if (isGetMethod(requestMethod, true) == false) {
499+
throw new UnsupportedOperationException("key[]:{}只支持GET类方法!不允许传 " + name + ":{} !");
506500
}
507501
if (request == null || request.isEmpty()) {//jsonKey-jsonValue条件
508502
return null;

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.HashSet;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
2324
import java.util.Map.Entry;
@@ -120,15 +121,17 @@ public static JSONObject parseRequest(final RequestMethod method, final String n
120121
}
121122

122123

123-
124-
125124
//解析
126125
return parse(name, target, request, new OnParseCallback() {
127126

128127
@Override
129128
public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj) throws Exception {
130129
// Log.i(TAG, "parseRequest.parse.onParseJSONObject key = " + key + "; robj = " + robj);
131-
if (robj != null && Parser.isTableKey(key)) {
130+
if (robj == null) {
131+
if (tobj != null) {//不允许不传Target中指定的Table
132+
throw new IllegalArgumentException("请设置 " + key + " !");
133+
}
134+
} else if (Parser.isTableKey(key)) {
132135
if (method == POST) {
133136
if (robj.containsKey(QueryConfig.ID)) {
134137
throw new IllegalArgumentException("POST " + key + " 请求不能设置" + QueryConfig.ID + "!");
@@ -217,12 +220,13 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
217220
//获取配置>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
218221

219222

223+
Set<String> tableKeySet = new HashSet<String>();
220224

221225

222-
real = operate(TYPE_VERIFY, verify, real);
223-
real = operate(TYPE_ADD, add, real);
224-
real = operate(TYPE_PUT, put, real);
225-
real = operate(TYPE_REPLACE, replace, real);
226+
real = operate(TYPE_VERIFY, verify, real, tableKeySet);
227+
real = operate(TYPE_ADD, add, real, tableKeySet);
228+
real = operate(TYPE_PUT, put, real, tableKeySet);
229+
real = operate(TYPE_REPLACE, replace, real, tableKeySet);
226230

227231

228232
//移除字段<<<<<<<<<<<<<<<<<<<
@@ -274,11 +278,10 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
274278

275279

276280

277-
List<String> tableKeyList = new ArrayList<String>();
278281
Set<Entry<String, Object>> set = new LinkedHashSet<>(target.entrySet());
282+
zuo.biao.apijson.server.Entry<String, String> pair;
279283
if (set.isEmpty() == false) {
280284

281-
zuo.biao.apijson.server.Entry<String, String> pair;
282285
String key;
283286
Object tvalue;
284287
Object rvalue;
@@ -298,7 +301,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
298301

299302
pair = Pair.parseEntry(key, true);
300303
if (pair != null && Parser.isTableKey(pair.getKey())) {
301-
tableKeyList.add(key);
304+
tableKeySet.add(key);
302305
}
303306
} else if (tvalue instanceof JSONArray) {//JSONArray
304307
tvalue = callback.onParseJSONArray(key, (JSONArray) tvalue, (JSONArray) rvalue);
@@ -317,7 +320,8 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
317320

318321
//不允许操作未指定Table<<<<<<<<<<<<<<<<<<<<<<<<<
319322
for (String rk : rkset) {
320-
if (Parser.isTableKey(rk) && tableKeyList.contains(rk) == false) {
323+
pair = Pair.parseEntry(rk, true);//非GET类操作不允许Table:alias别名
324+
if (pair != null && Parser.isTableKey(pair.getKey()) && tableKeySet.contains(rk) == false) {
321325
throw new UnsupportedOperationException("不允许操作 " + rk + " !");
322326
}
323327
}
@@ -334,10 +338,12 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
334338
* @param operate
335339
* @param targetChild
336340
* @param real
341+
* @param tableKeySet
337342
* @return
338343
* @throws Exception
339344
*/
340-
private static JSONObject operate(int type, JSONObject targetChild, JSONObject real) throws Exception {
345+
private static JSONObject operate(int type, JSONObject targetChild, JSONObject real
346+
, Set<String> tableKeySet) throws Exception {
341347
if (targetChild == null) {
342348
return real;
343349
}
@@ -349,10 +355,6 @@ private static JSONObject operate(int type, JSONObject targetChild, JSONObject r
349355
return real;
350356
}
351357

352-
if (type == TYPE_PUT) {
353-
real.putAll(targetChild);
354-
return real;
355-
}
356358

357359
Set<Entry<String, Object>> set = new LinkedHashSet<>(targetChild.entrySet());
358360
String tk;
@@ -432,14 +434,16 @@ private static JSONObject operate(int type, JSONObject targetChild, JSONObject r
432434

433435
}
434436

437+
} else if (type == TYPE_PUT) {
438+
putTargetChild(real, tk, tv, tableKeySet);
435439
} else {
436440
if (real.containsKey(tk)) {
437441
if (type == TYPE_REPLACE) {
438-
real.put(tk, tv);
442+
putTargetChild(real, tk, tv, tableKeySet);
439443
}
440444
} else {
441445
if (type == TYPE_ADD) {
442-
real.put(tk, tv);
446+
putTargetChild(real, tk, tv, tableKeySet);
443447
}
444448
}
445449
}
@@ -449,6 +453,19 @@ private static JSONObject operate(int type, JSONObject targetChild, JSONObject r
449453
}
450454

451455

456+
/**
457+
* @param real
458+
* @param tk
459+
* @param tv
460+
* @param tableKeySet
461+
*/
462+
private static void putTargetChild(JSONObject real, String tk, Object tv, Set<String> tableKeySet) {
463+
real.put(tk, tv);
464+
zuo.biao.apijson.server.Entry<String, String> pair = Pair.parseEntry(tk, true);
465+
if (pair != null && Parser.isTableKey(pair.getKey())) {
466+
tableKeySet.add(tk);
467+
}
468+
}
452469

453470

454471
public static final int TYPE_DEFAULT = 0;

0 commit comments

Comments
 (0)