Skip to content

Commit 0762c97

Browse files
committed
Server:与Client同步公共类
1 parent ab89b8f commit 0762c97

File tree

3 files changed

+107
-22
lines changed

3 files changed

+107
-22
lines changed

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/JSONObject.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public static boolean isWord(String key) {
240240
* @return {@link #setColumn(String)}
241241
*/
242242
public JSONObject setColumn(String... keys) {
243-
return setColumn(StringUtil.getString(keys));
243+
return setColumn(StringUtil.getString(keys, true));
244244
}
245245
/**set keys need to be returned
246246
* @param keys "key0,key1,key2..."
@@ -259,7 +259,7 @@ public String getColumn() {
259259
* @return {@link #setGroup(String)}
260260
*/
261261
public JSONObject setGroup(String... keys) {
262-
return setGroup(StringUtil.getString(keys));
262+
return setGroup(StringUtil.getString(keys, true));
263263
}
264264
/**set keys for group by
265265
* @param keys "key0,key1,key2..."
@@ -278,7 +278,7 @@ public String getGroup() {
278278
* @return {@link #setHaving(String)}
279279
*/
280280
public JSONObject setHaving(String... keys) {
281-
return setHaving(StringUtil.getString(keys));
281+
return setHaving(StringUtil.getString(keys, true));
282282
}
283283
/**set keys for having
284284
* @param keys "key0,key1,key2..."
@@ -297,7 +297,7 @@ public String getHaving() {
297297
* @return {@link #setOrder(String)}
298298
*/
299299
public JSONObject setOrder(String... keys) {
300-
return setOrder(StringUtil.getString(keys));
300+
return setOrder(StringUtil.getString(keys, true));
301301
}
302302
/**set keys for order by
303303
* @param keys "key0,key1+,key2-..."

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/JSONResponse.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ public JSONArray toArray(String className) {
285285
return toArray(this, className);
286286
}
287287
/**
288+
* array.set(index, isContainer ? value : value.getJSONObject(className));
288289
* @param arrayObject
289-
* @param className
290+
* @param className className.isEmpty() == false && value.containsKey(className) >> isContainer = true;
290291
* @return
291292
*/
292293
public static JSONArray toArray(JSONObject arrayObject, String className) {
@@ -357,28 +358,32 @@ public static JSONObject format(final JSONObject response) {
357358

358359
Set<String> set = response.keySet();
359360
if (set != null) {
361+
360362
Object value;
361363
String arrayKey;
362364
for (String key : set) {
363365
value = response.get(key);
366+
364367
if (value instanceof JSONArray) {//转化JSONArray内部的APIJSON Array
365-
transferredObject.put(key, format((JSONArray) value));
368+
transferredObject.put(getSimpleName(key), format((JSONArray) value));
366369
} else if (value instanceof JSONObject) {//APIJSON Array转为常规JSONArray
367370
if (isArrayKey(key)) {//APIJSON Array转为常规JSONArray
368371
arrayKey = key.substring(0, key.indexOf(KEY_ARRAY));
369-
transferredObject.put(getArrayKey(arrayKey), format(toArray((JSONObject) value, arrayKey)));
372+
transferredObject.put(getArrayKey(getSimpleName(arrayKey))
373+
, format(toArray((JSONObject) value, arrayKey)));//需要将name:alias传至toArray
370374
} else {//常规JSONObject,往下一级提取
371-
transferredObject.put(key, format((JSONObject) value));
375+
transferredObject.put(getSimpleName(key), format((JSONObject) value));
372376
}
373377
} else {//其它Object,直接填充
374-
transferredObject.put(key, value);
378+
transferredObject.put(getSimpleName(key), value);
375379
}
376380
}
377381
}
378382

379383
Log.i(TAG, "format return transferredObject = " + JSON.toJSONString(transferredObject));
380384
return transferredObject;
381385
}
386+
382387
/**
383388
* @param responseArray
384389
* @return
@@ -409,13 +414,13 @@ public static JSONArray format(final JSONArray responseArray) {
409414

410415
/**替换key+KEY_ARRAY为keyList
411416
* @param key
412-
* @return isArrayKey(key) ? getArrayKey(...) : key
417+
* @return getSimpleName(isArrayKey(key) ? getArrayKey(...) : key) {@link #getSimpleName(String)}
413418
*/
414419
public static String replaceArray(String key) {
415420
if (isArrayKey(key)) {
416-
return getArrayKey(key.substring(0, key.indexOf(KEY_ARRAY)));
421+
key = getArrayKey(key.substring(0, key.indexOf(KEY_ARRAY)));
417422
}
418-
return key;
423+
return getSimpleName(key);
419424
}
420425
/**获取列表变量名
421426
* @param key => StringUtil.getNoBlankString(key)
@@ -433,5 +438,19 @@ public static String getArrayKey(String key) {
433438
}
434439
return key + "List";
435440
}
441+
442+
/**获取简单名称
443+
* @param fullName name 或 name:alias
444+
* @return name > name; name:alias > alias
445+
*/
446+
public static String getSimpleName(String fullName) {
447+
//key:alias -> alias; key:alias[] -> alias[]
448+
int index = fullName == null ? -1 : fullName.indexOf(":");
449+
if (index >= 0) {
450+
fullName = fullName.substring(index + 1);
451+
}
452+
return fullName;
453+
}
454+
436455

437456
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/StringUtil.java

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public class StringUtil {
2828
private static final String TAG = "StringUtil";
29-
29+
3030
public StringUtil() {
3131
}
3232

@@ -95,24 +95,48 @@ public static String getString(String s) {
9595
return s == null ? "" : s;
9696
}
9797
/**获取string,为null则返回""
98+
* ignoreEmptyItem = false;
99+
* split = ","
98100
* @param array
99-
* @return
101+
* @return {@link #getString(String[], boolean)}
100102
*/
101103
public static String getString(String[] array) {
102-
return getString(array, null);
104+
return getString(array, false);
105+
}
106+
/**获取string,为null则返回""
107+
* split = ","
108+
* @param array
109+
* @param ignoreEmptyItem
110+
* @return {@link #getString(String[], String, boolean)}
111+
*/
112+
public static String getString(String[] array, boolean ignoreEmptyItem) {
113+
return getString(array, null, ignoreEmptyItem);
103114
}
104115
/**获取string,为null则返回""
116+
* ignoreEmptyItem = false;
105117
* @param array
106118
* @param split
107-
* @return
119+
* @return {@link #getString(String[], String, boolean)}
108120
*/
109121
public static String getString(String[] array, String split) {
122+
return getString(array, split, false);
123+
}
124+
/**获取string,为null则返回""
125+
* @param array
126+
* @param split
127+
* @param ignoreEmptyItem
128+
* @return
129+
*/
130+
public static String getString(String[] array, String split, boolean ignoreEmptyItem) {
110131
String s = "";
111132
if (array != null) {
112133
if (split == null) {
113134
split = ",";
114135
}
115136
for (int i = 0; i < array.length; i++) {
137+
if (ignoreEmptyItem && isEmpty(array[i], true)) {
138+
continue;
139+
}
116140
s += ((i > 0 ? split : "") + array[i]);
117141
}
118142
}
@@ -206,8 +230,50 @@ public static int getLength(String s, boolean trim) {
206230
//获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
207231

208232

209-
//判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
233+
//判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
210234

235+
/**判断字符是否为空
236+
* @param object
237+
* @param trim
238+
* @return
239+
*/
240+
public static boolean isEmpty(Object object, boolean trim) {
241+
return isEmpty(getString(object), trim);
242+
}
243+
/**判断字符是否为空
244+
* @param cs
245+
* @param trim
246+
* @return
247+
*/
248+
public static boolean isEmpty(CharSequence cs, boolean trim) {
249+
return isEmpty(getString(cs), trim);
250+
}
251+
/**判断字符是否为空
252+
* @param s
253+
* @param trim
254+
* @return
255+
*/
256+
public static boolean isEmpty(String s, boolean trim) {
257+
// Log.i(TAG, "getTrimedString s = " + s);
258+
if (s == null) {
259+
return true;
260+
}
261+
if (trim) {
262+
s = s.trim();
263+
}
264+
if (s.isEmpty()) {
265+
return true;
266+
}
267+
268+
currentString = s;
269+
270+
return false;
271+
}
272+
273+
//判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
274+
275+
//判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
276+
211277
/**判断字符是否非空
212278
* @param object
213279
* @param trim
@@ -240,12 +306,12 @@ public static boolean isNotEmpty(String s, boolean trim) {
240306
if (s.length() <= 0) {
241307
return false;
242308
}
243-
309+
244310
currentString = s;
245-
311+
246312
return true;
247313
}
248-
314+
249315
//判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
250316

251317

@@ -261,7 +327,7 @@ public static boolean isNotEmpty(String s, boolean trim) {
261327
namePattern = Pattern.compile("^[0-9a-zA-Z_]+$");//已用55个中英字符测试通过
262328
smallAlphaPattern = Pattern.compile("[a-z]");
263329
}
264-
330+
265331
//判断手机格式是否正确
266332
public static boolean isPhone(String phone) {
267333
if (isNotEmpty(phone, true) == false) {
@@ -576,7 +642,7 @@ public static String getCorrectEmail(String email) {
576642
public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3;
577643
public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4;
578644
public static final String[] PRICE_FORMATS = {
579-
"", "¥", "元", "¥ ", " 元"
645+
"", "¥", "元", "¥ ", " 元"
580646
};
581647

582648
/**获取价格,保留两位小数

0 commit comments

Comments
 (0)