Skip to content

Commit a3811e4

Browse files
committed
Java和Android:同步并优化公共的核心代码
1 parent a706e1e commit a3811e4

30 files changed

+415
-193
lines changed

APIJSON-Android/APIJSONApp/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
package zuo.biao.apijson;
1616

17-
import java.util.List;
18-
1917
import com.alibaba.fastjson.JSONArray;
2018
import com.alibaba.fastjson.JSONObject;
2119
import com.alibaba.fastjson.parser.Feature;
2220
import com.alibaba.fastjson.serializer.SerializerFeature;
2321

22+
import java.util.List;
23+
2424
/**阿里FastJSON封装类 防止解析时异常
2525
* @author Lemon
2626
*/
@@ -33,9 +33,9 @@ public class JSON {
3333
*/
3434
public static boolean isJsonCorrect(String s) {
3535
//太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
36-
if (s == null
37-
// || s.equals("[]")
38-
// || s.equals("{}")
36+
if (s == null
37+
// || s.equals("[]")
38+
// || s.equals("{}")
3939
|| s.equals("")
4040
|| s.equals("[null]")
4141
|| s.equals("{null}")
@@ -70,6 +70,20 @@ public static String getCorrectJson(String s, boolean isArray) {
7070
return s;//isJsonCorrect(s) ? s : null;
7171
}
7272

73+
/**
74+
* @param json
75+
* @return
76+
*/
77+
public static Object parse(Object obj) {
78+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
79+
features |= Feature.OrderedField.getMask();
80+
try {
81+
return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), features);
82+
} catch (Exception e) {
83+
Log.i(TAG, "parse catch \n" + e.getMessage());
84+
}
85+
return null;
86+
}
7387
/**obj转JSONObject
7488
* @param json
7589
* @return
@@ -139,7 +153,7 @@ public static JSONArray parseArray(List<Object> list) {
139153
return new JSONArray(list);
140154
}
141155
/**obj转JSONArray
142-
* @param json
156+
* @param obj
143157
* @return
144158
*/
145159
public static JSONArray parseArray(Object obj) {
@@ -224,13 +238,13 @@ public static String toJSONString(Object obj, SerializerFeature... features) {
224238
* @return
225239
*/
226240
public static String format(String json) {
227-
return format(parseObject(json));
241+
return format(parse(json));
228242
}
229243
/**格式化,显示更好看
230244
* @param object
231245
* @return
232246
*/
233-
public static String format(JSONObject object) {
247+
public static String format(Object object) {
234248
return toJSONString(object, SerializerFeature.PrettyFormat);
235249
}
236250

@@ -273,4 +287,12 @@ public static boolean isJSONArray(Object obj) {
273287
return false;
274288
}
275289

290+
/**判断是否为 Boolean,Number,String 中的一种
291+
* @param obj
292+
* @return
293+
*/
294+
public static boolean isBooleanOrNumberOrString(Object obj) {
295+
return obj instanceof Boolean || obj instanceof Number || obj instanceof String;
296+
}
297+
276298
}

APIJSON-Android/APIJSONApp/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSONObject.java

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import java.util.ArrayList;
1718
import java.util.List;
1819
import java.util.Map;
1920

@@ -25,10 +26,10 @@
2526
*/
2627
public class JSONObject extends com.alibaba.fastjson.JSONObject {
2728
private static final long serialVersionUID = 1L;
28-
29+
2930
private static final String TAG = "JSONObject";
3031

31-
32+
3233
/**ordered
3334
*/
3435
public JSONObject() {
@@ -82,9 +83,11 @@ public static boolean isTableKey(String key) {
8283

8384
//JSONObject内关键词 key <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
8485

85-
86-
public static final String KEY_ID = "id";
87-
public static final String KEY_ID_IN = KEY_ID + "{}";
86+
87+
public static String KEY_ID = "id";
88+
public static String KEY_ID_IN = KEY_ID + "{}";
89+
public static String KEY_USER_ID = "userId";
90+
public static String KEY_USER_ID_IN = KEY_USER_ID + "{}";
8891

8992
/**set "id":id in Table layer
9093
* @param id
@@ -93,28 +96,57 @@ public static boolean isTableKey(String key) {
9396
public JSONObject setId(Long id) {
9497
return puts(KEY_ID, id);
9598
}
96-
/**set id{}:[] in Table layer
99+
/**set "id{}":[] in Table layer
97100
* @param list
98101
* @return
99102
*/
100103
public JSONObject setIdIn(List<Object> list) {
101104
return puts(KEY_ID_IN, list);
102105
}
103106

104-
107+
/**set "userId":userId in Table layer
108+
* @param id
109+
* @return
110+
*/
111+
public JSONObject setUserId(Long id) {
112+
return puts(KEY_USER_ID, id);
113+
}
114+
/**set "userId{}":[] in Table layer
115+
* @param list
116+
* @return
117+
*/
118+
public JSONObject setUserIdIn(List<Object> list) {
119+
return puts(KEY_USER_ID_IN, list);
120+
}
121+
122+
105123
//@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<<
106124
public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
107-
public static final String KEY_CONDITION = "@condition"; //条件 TODO 用 @where& @where| @where! 替代?
108125
public static final String KEY_TRY = "@try"; //尝试,忽略异常
109126
public static final String KEY_DROP = "@drop"; //丢弃,不返回
110127
public static final String KEY_CORRECT = "@correct"; //字段校正
111-
128+
129+
public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
112130
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
113-
public static final String KEY_ABOUT = "@about"; //关于,返回数据库表的信息,包括表说明和字段说明
114131
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
132+
public static final String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
115133
public static final String KEY_GROUP = "@group"; //分组方式
116134
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
117135
public static final String KEY_ORDER = "@order"; //排序方式
136+
137+
public static final List<String> TABLE_KEY_LIST;
138+
static {
139+
TABLE_KEY_LIST = new ArrayList<String>();
140+
TABLE_KEY_LIST.add(KEY_ROLE);
141+
TABLE_KEY_LIST.add(KEY_DATABASE);
142+
TABLE_KEY_LIST.add(KEY_SCHEMA);
143+
TABLE_KEY_LIST.add(KEY_COLUMN);
144+
TABLE_KEY_LIST.add(KEY_COMBINE);
145+
TABLE_KEY_LIST.add(KEY_GROUP);
146+
TABLE_KEY_LIST.add(KEY_HAVING);
147+
TABLE_KEY_LIST.add(KEY_ORDER);
148+
}
149+
118150
//@key关键字都放这个类 >>>>>>>>>>>>>>>>>>>>>>
119151

120152

@@ -125,33 +157,41 @@ public JSONObject setIdIn(List<Object> list) {
125157
public JSONObject setRole(String role) {
126158
return puts(KEY_ROLE, role);
127159
}
128-
160+
129161
/**set try, ignore exceptions
130162
* @param tri
131163
* @return this
132164
*/
133165
public JSONObject setTry(boolean tri) {
134166
return puts(KEY_TRY, tri);
135167
}
136-
168+
137169
/**set drop, data dropped will not return
138170
* @param drop
139171
* @return this
140172
*/
141173
public JSONObject setDrop(boolean drop) {
142174
return puts(KEY_DROP, drop);
143175
}
144-
176+
145177
/**set correct, correct keys to target ones
146178
* @param correct Map{originKey, [posibleKeys]}, posibleKey之间用 , 隔开
147179
* @return this
148180
*/
149181
public JSONObject setCorrect(Map<String, String> correct) {
150182
return puts(KEY_CORRECT, correct);
151183
}
184+
185+
152186

153-
154-
187+
/**set database where table was puts
188+
* @param database
189+
* @return this
190+
*/
191+
public JSONObject setDatabase(String database) {
192+
return puts(KEY_DATABASE, database);
193+
194+
}
155195
/**set schema where table was puts
156196
* @param schema
157197
* @return this
@@ -160,14 +200,6 @@ public JSONObject setSchema(String schema) {
160200
return puts(KEY_SCHEMA, schema);
161201
}
162202

163-
/**set about
164-
* @param about
165-
* @return this
166-
*/
167-
public JSONObject setAbout(boolean about) {
168-
return puts(KEY_ABOUT, about);
169-
}
170-
171203
/**set keys need to be returned
172204
* @param keys key0, key1, key2 ...
173205
* @return {@link #setColumn(String)}
@@ -183,6 +215,21 @@ public JSONObject setColumn(String keys) {
183215
return puts(KEY_COLUMN, keys);
184216
}
185217

218+
/**set combination of keys for conditions
219+
* @param keys key0,&key1,|key2,!kye3 ...
220+
* @return {@link #setColumn(String)}
221+
*/
222+
public JSONObject setCombine(String... keys) {
223+
return setCombine(StringUtil.getString(keys, true));
224+
}
225+
/**set combination of keys for conditions
226+
* @param keys key0,&key1,|key2,!kye3 ...
227+
* @return
228+
*/
229+
public JSONObject setCombine(String keys) {
230+
return puts(KEY_COMBINE, keys);
231+
}
232+
186233
/**set keys for group by
187234
* @param keys key0, key1, key2 ...
188235
* @return {@link #setGroup(String)}
@@ -300,7 +347,7 @@ public JSONObject putsSearch(String key, String value, int type) {
300347

301348
//Request >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
302349

303-
350+
304351

305352
/**puts key-value in object into this
306353
* @param map
@@ -328,7 +375,7 @@ public JSONObject puts(Object value) {
328375
}
329376
/**put and return this
330377
* @param key
331-
* @param value
378+
* @param value
332379
* @return this
333380
* @see {@link #put(String, Object)}
334381
*/
@@ -356,10 +403,10 @@ public Object put(String key, Object value) {
356403
return null;
357404
}
358405
if (StringUtil.isEmpty(key, true)) {
359-
Class<?> clazz = value.getClass();
360-
if (clazz == null || clazz.getAnnotation(MethodAccess.class) == null) {
361-
throw new IllegalArgumentException("puts StringUtil.isNotEmpty(key, true) == false" +
362-
" && clazz == null || clazz.getAnnotation(MethodAccess.class) == null" +
406+
Class<?> clazz = value.getClass(); //should not return null
407+
if (clazz.getAnnotation(MethodAccess.class) == null) {
408+
throw new IllegalArgumentException("puts StringUtil.isEmpty(key, true)" +
409+
" clazz.getAnnotation(MethodAccess.class) == null" +
363410
" \n key为空时仅支持 类型被@MethodAccess注解 的value !!!" +
364411
" \n 如果一定要这么用,请对 " + clazz.getName() + " 注解!" +
365412
" \n 如果是类似 key[]:{} 结构的请求,建议用 putsAll(...) !");
@@ -370,5 +417,5 @@ public Object put(String key, Object value) {
370417
}
371418

372419

373-
420+
374421
}

APIJSON-Android/APIJSONApp/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSONRequest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package zuo.biao.apijson;
1616

17+
import java.util.ArrayList;
18+
import java.util.List;
1719
import java.util.Map;
1820

1921
/**wrapper for request
@@ -52,6 +54,7 @@ public JSONRequest(String name, Object object) {
5254

5355
public static final String KEY_TAG = "tag";//只在最外层,最外层用JSONRequest
5456
public static final String KEY_VERSION = "version";//只在最外层,最外层用JSONRequest
57+
public static final String KEY_FORMAT = "format";//只在最外层,最外层用JSONRequest
5558

5659
/**set "tag":tag in outermost layer
5760
* for write operations
@@ -62,13 +65,21 @@ public JSONRequest setTag(String tag) {
6265
return puts(KEY_TAG, tag);
6366
}
6467
/**set "version":version in outermost layer
65-
* for write operations
68+
* for target version of request
6669
* @param version
6770
* @return
6871
*/
69-
public JSONRequest setVersion(int version) {
72+
public JSONRequest setVersion(String version) {
7073
return puts(KEY_VERSION, version);
7174
}
75+
/**set "format":format in outermost layer
76+
* for format APIJSON special keys to normal keys of response
77+
* @param format
78+
* @return
79+
*/
80+
public JSONRequest setFormat(Boolean format) {
81+
return puts(KEY_FORMAT, format);
82+
}
7283

7384

7485
//array object <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -80,6 +91,15 @@ public JSONRequest setVersion(int version) {
8091
public static final String KEY_QUERY = "query";
8192
public static final String KEY_COUNT = "count";
8293
public static final String KEY_PAGE = "page";
94+
public static final String KEY_JOIN = "join";
95+
96+
public static final List<String> ARRAY_KEY_LIST;
97+
static {
98+
ARRAY_KEY_LIST = new ArrayList<String>();
99+
ARRAY_KEY_LIST.add(KEY_QUERY);
100+
ARRAY_KEY_LIST.add(KEY_COUNT);
101+
ARRAY_KEY_LIST.add(KEY_PAGE);
102+
}
83103

84104
/**set what to query in Array layer
85105
* @param query what need to query, Table,total,ALL?

0 commit comments

Comments
 (0)