Skip to content

Commit a6d7d3c

Browse files
committed
Server:同步eclipse版至idea版
1 parent 9afc5f9 commit a6d7d3c

28 files changed

+2635
-167
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)
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+
/**自定义Entry
18+
* *java.util.Map.Entry是interface,new Entry(...)不好用,其它的Entry也不好用
19+
* @author Lemon
20+
* @param <K> key
21+
* @param <V> value
22+
* @use new Entry<K, V>(...)
23+
* @warn K,V都需要基本类型时不建议使用,判空麻烦,不如新建一个Model
24+
*/
25+
public class Entry<K, V> {
26+
27+
public K key;
28+
public V value;
29+
30+
public Entry() {
31+
//default
32+
}
33+
public Entry(K key) {
34+
this(key, null);
35+
}
36+
public Entry(K key, V value) {
37+
this.key = key;
38+
this.value = value;
39+
}
40+
41+
42+
public K getKey() {
43+
return key;
44+
}
45+
public void setKey(K key) {
46+
this.key = key;
47+
}
48+
public V getValue() {
49+
return value;
50+
}
51+
public void setValue(V value) {
52+
this.value = value;
53+
}
54+
55+
}

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

Lines changed: 56 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,10 +52,33 @@ 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

75+
/**json转JSONObject
76+
* @param json
77+
* @return
78+
*/
79+
public static JSONObject parseObject(Object obj) {
80+
return parseObject(toJSONString(obj));
81+
}
5782
/**json转JSONObject
5883
* @param json
5984
* @return
@@ -72,11 +97,19 @@ public static JSONObject parseObject(String json, int features) {
7297
try {
7398
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
7499
} catch (Exception e) {
75-
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
100+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
76101
}
77102
return null;
78103
}
79104

105+
/**JSONObject转实体类
106+
* @param object
107+
* @param clazz
108+
* @return
109+
*/
110+
public static <T> T parseObject(JSONObject object, Class<T> clazz) {
111+
return parseObject(toJSONString(object), clazz);
112+
}
80113
/**json转实体类
81114
* @param json
82115
* @param clazz
@@ -88,7 +121,7 @@ public static <T> T parseObject(String json, Class<T> clazz) {
88121
features |= Feature.OrderedField.getMask();
89122
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
90123
} catch (Exception e) {
91-
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
124+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
92125
}
93126
return null;
94127
}
@@ -98,7 +131,20 @@ public static <T> T parseObject(String json, Class<T> clazz) {
98131
* @return
99132
*/
100133
public static JSONArray parseArray(String json) {
101-
return com.alibaba.fastjson.JSON.parseArray(json);
134+
try {
135+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true));
136+
} catch (Exception e) {
137+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
138+
}
139+
return null;
140+
}
141+
/**JSONArray转实体类列表
142+
* @param array
143+
* @param clazz
144+
* @return
145+
*/
146+
public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
147+
return parseArray(toJSONString(array), clazz);
102148
}
103149
/**json转实体类列表
104150
* @param json
@@ -107,9 +153,9 @@ public static JSONArray parseArray(String json) {
107153
*/
108154
public static <T> List<T> parseArray(String json, Class<T> clazz) {
109155
try {
110-
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json), clazz);
156+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
111157
} catch (Exception e) {
112-
System.out.println(TAG + "parseArray catch \n" + e.getMessage());
158+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
113159
}
114160
return null;
115161
}
@@ -125,7 +171,7 @@ public static String toJSONString(Object obj) {
125171
try {
126172
return com.alibaba.fastjson.JSON.toJSONString(obj);
127173
} catch (Exception e) {
128-
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
174+
Log.i(TAG, "toJSONString catch \n" + e.getMessage());
129175
}
130176
return null;
131177
}
@@ -142,7 +188,7 @@ public static String toJSONString(Object obj, SerializerFeature... features) {
142188
try {
143189
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
144190
} catch (Exception e) {
145-
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
191+
Log.i(TAG, "toJSONString catch \n" + e.getMessage());
146192
}
147193
return null;
148194
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
import static zuo.biao.apijson.StringUtil.namePattern;
19+
20+
import java.util.Set;
21+
22+
23+
/**use this class instead of com.alibaba.fastjson.JSONObject
24+
* @author Lemon
25+
*/
26+
public class JSONObject extends com.alibaba.fastjson.JSONObject {
27+
private static final long serialVersionUID = 8907029699680768212L;
28+
29+
/**ordered
30+
*/
31+
public JSONObject() {
32+
super(true);
33+
}
34+
/**transfer Object to JSONObject
35+
* @param object
36+
*/
37+
public JSONObject(Object object) {
38+
this(toJSONString(object));
39+
}
40+
/**parse JSONObject with JSON String
41+
* @param json
42+
*/
43+
public JSONObject(String json) {
44+
this(parseObject(json));
45+
}
46+
/**transfer com.alibaba.fastjson.JSONObject to JSONObject
47+
* @param object
48+
*/
49+
public JSONObject(com.alibaba.fastjson.JSONObject object) {
50+
this();
51+
add(object);
52+
}
53+
54+
55+
56+
57+
/**put key-value in object into this
58+
* @param object
59+
* @return this
60+
*/
61+
public JSONObject add(com.alibaba.fastjson.JSONObject object) {
62+
Set<String> set = object == null ? null : object.keySet();
63+
if (set != null) {
64+
for (String key : set) {
65+
put(key, object.get(key));
66+
}
67+
}
68+
return this;
69+
}
70+
71+
72+
73+
74+
75+
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
76+
public static final String KEY_ARRAY = "[]";
77+
78+
/**判断是否为Array的key
79+
* @param key
80+
* @return
81+
*/
82+
public static boolean isArrayKey(String key) {
83+
return key != null && key.endsWith(KEY_ARRAY);
84+
}
85+
/**判断是否为对应Table的key
86+
* @param key
87+
* @return
88+
*/
89+
public static boolean isTableKey(String key) {
90+
return isWord(key) && bigAlphaPattern.matcher(key.substring(0, 1)).matches();
91+
}
92+
/**判断是否为词,只能包含字母,数字或下划线
93+
* @param key
94+
* @return
95+
*/
96+
public static boolean isWord(String key) {
97+
return StringUtil.isNotEmpty(key, false) && namePattern.matcher(key).matches();
98+
}
99+
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
100+
101+
}

0 commit comments

Comments
 (0)