Skip to content

Commit e3f1558

Browse files
committed
Server:新增HEAD,POST_HEAD请求;Controller中新增register方法;客户端的JSONRequest,JSONResponse放到公共包;与客户端同步JSON,StringUtil;优化代码,SQL语句全大写易看日志
1 parent fb65608 commit e3f1558

14 files changed

Lines changed: 1162 additions & 100 deletions

File tree

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

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.alibaba.fastjson.parser.Feature;
2222
import com.alibaba.fastjson.serializer.SerializerFeature;
2323

24+
import zuo.biao.apijson.server.Log;
25+
2426
/**阿里json封装类 防止解析时异常
2527
* @author Lemon
2628
*/
@@ -32,7 +34,7 @@ public class JSON {
3234
* @return
3335
*/
3436
public static boolean isJsonCorrect(String s) {
35-
System.out.println(TAG + "isJsonCorrect <<<< " + s + " >>>>>>>");
37+
Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
3638
if (s == null
3739
// || s.equals("[]")
3840
// || s.equals("{}")
@@ -50,8 +52,24 @@ public static boolean isJsonCorrect(String s) {
5052
* @return
5153
*/
5254
public static String getCorrectJson(String s) {
53-
s = StringUtil.getNoBlankString(s);
54-
return isJsonCorrect(s) ? s : "";
55+
return getCorrectJson(s, false);
56+
}
57+
/**获取有效的json
58+
* @param s
59+
* @param isArray
60+
* @return
61+
*/
62+
public static String getCorrectJson(String s, boolean isArray) {
63+
s = StringUtil.getTrimedString(s);
64+
// if (isArray) {
65+
// if (s.startsWith("\"")) {
66+
// s = s.substring(1);
67+
// }
68+
// if (s.endsWith("\"")) {
69+
// s = s.substring(0, s.length() - 1);
70+
// }
71+
// }
72+
return s;//isJsonCorrect(s) ? s : null;
5573
}
5674

5775
/**json转JSONObject
@@ -72,11 +90,19 @@ public static JSONObject parseObject(String json, int features) {
7290
try {
7391
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
7492
} catch (Exception e) {
75-
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
93+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
7694
}
7795
return null;
7896
}
7997

98+
/**JSONObject转实体类
99+
* @param object
100+
* @param clazz
101+
* @return
102+
*/
103+
public static <T> T parseObject(JSONObject object, Class<T> clazz) {
104+
return parseObject(toJSONString(object), clazz);
105+
}
80106
/**json转实体类
81107
* @param json
82108
* @param clazz
@@ -88,7 +114,7 @@ public static <T> T parseObject(String json, Class<T> clazz) {
88114
features |= Feature.OrderedField.getMask();
89115
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
90116
} catch (Exception e) {
91-
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
117+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
92118
}
93119
return null;
94120
}
@@ -98,7 +124,20 @@ public static <T> T parseObject(String json, Class<T> clazz) {
98124
* @return
99125
*/
100126
public static JSONArray parseArray(String json) {
101-
return com.alibaba.fastjson.JSON.parseArray(json);
127+
try {
128+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true));
129+
} catch (Exception e) {
130+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
131+
}
132+
return null;
133+
}
134+
/**JSONArray转实体类列表
135+
* @param array
136+
* @param clazz
137+
* @return
138+
*/
139+
public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
140+
return parseArray(toJSONString(array), clazz);
102141
}
103142
/**json转实体类列表
104143
* @param json
@@ -107,9 +146,9 @@ public static JSONArray parseArray(String json) {
107146
*/
108147
public static <T> List<T> parseArray(String json, Class<T> clazz) {
109148
try {
110-
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json), clazz);
149+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
111150
} catch (Exception e) {
112-
System.out.println(TAG + "parseArray catch \n" + e.getMessage());
151+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
113152
}
114153
return null;
115154
}
@@ -125,7 +164,7 @@ public static String toJSONString(Object obj) {
125164
try {
126165
return com.alibaba.fastjson.JSON.toJSONString(obj);
127166
} catch (Exception e) {
128-
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
167+
Log.i(TAG, "toJSONString catch \n" + e.getMessage());
129168
}
130169
return null;
131170
}
@@ -142,7 +181,7 @@ public static String toJSONString(Object obj, SerializerFeature... features) {
142181
try {
143182
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
144183
} catch (Exception e) {
145-
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
184+
Log.i(TAG, "toJSONString catch \n" + e.getMessage());
146185
}
147186
return null;
148187
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package zuo.biao.apijson;
16+
17+
import static zuo.biao.apijson.StringUtil.bigAlphaPattern;
18+
19+
import java.util.Set;
20+
21+
22+
/**use this class instead of com.alibaba.fastjson.JSONObject
23+
* @author Lemon
24+
*/
25+
public class JSONObject extends com.alibaba.fastjson.JSONObject {
26+
private static final long serialVersionUID = 8907029699680768212L;
27+
28+
/**ordered
29+
*/
30+
public JSONObject() {
31+
super(true);
32+
}
33+
/**transfer Object to JSONObject
34+
* @param object
35+
*/
36+
public JSONObject(Object object) {
37+
this(toJSONString(object));
38+
}
39+
/**parse JSONObject with JSON String
40+
* @param json
41+
*/
42+
public JSONObject(String json) {
43+
this(parseObject(json));
44+
}
45+
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
46+
* @param object
47+
*/
48+
public JSONObject(com.alibaba.fastjson.JSONObject object) {
49+
this();
50+
add(object);
51+
}
52+
53+
54+
55+
56+
/**put key-value in object into this
57+
* @param object
58+
* @return this
59+
*/
60+
public JSONObject add(com.alibaba.fastjson.JSONObject object) {
61+
Set<String> set = object == null ? null : object.keySet();
62+
if (set != null) {
63+
for (String key : set) {
64+
put(key, object.get(key));
65+
}
66+
}
67+
return this;
68+
}
69+
70+
public static final String KEY_ARRAY = "[]";
71+
public static final String KEY_TAG = "tag";
72+
public static final String KEY_COLUMNS = "columns";
73+
/**set tag
74+
* @param tag
75+
* @return
76+
*/
77+
public JSONObject setTag(String tag) {
78+
put(KEY_TAG, tag);
79+
return this;
80+
}
81+
public String getTag() {
82+
return getString(KEY_TAG);
83+
}
84+
85+
/**set columns need to be returned
86+
* @param columns "column0,column1,column2..."
87+
* @return
88+
*/
89+
public JSONObject setColumns(String columns) {
90+
put(KEY_COLUMNS, columns);
91+
return this;
92+
}
93+
public String getColumns() {
94+
return getString(KEY_COLUMNS);
95+
}
96+
97+
98+
99+
100+
101+
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
102+
103+
public static boolean isTableKey(String key) {
104+
return StringUtil.isNotEmpty(key, false) && isArrayKey(key) == false
105+
&& bigAlphaPattern.matcher(key.substring(0, 1)).matches();
106+
}
107+
public static boolean isArrayKey(String key) {
108+
return key != null && key.endsWith(KEY_ARRAY);
109+
}
110+
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
111+
112+
}

0 commit comments

Comments
 (0)