Skip to content

Commit 6d2abdb

Browse files
committed
Client:同步adt版至studio版
1 parent a6d7d3c commit 6d2abdb

15 files changed

Lines changed: 1144 additions & 750 deletions

File tree

APIJSON(Android)/APIJSON(AndroidStudio)/APIJSONLibrary/src/main/java/zuo/biao/apijson/JSON.java

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.util.List;
1818

19+
import android.util.Log;
20+
1921
import com.alibaba.fastjson.JSONArray;
2022
import com.alibaba.fastjson.JSONObject;
2123
import com.alibaba.fastjson.parser.Feature;
@@ -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
}
@@ -119,10 +158,13 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
119158
* @return
120159
*/
121160
public static String toJSONString(Object obj) {
161+
if (obj instanceof String) {
162+
return (String) obj;
163+
}
122164
try {
123165
return com.alibaba.fastjson.JSON.toJSONString(obj);
124166
} catch (Exception e) {
125-
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
167+
Log.i(TAG, "toJSONString catch \n" + e.getMessage());
126168
}
127169
return null;
128170
}
@@ -133,10 +175,13 @@ public static String toJSONString(Object obj) {
133175
* @return
134176
*/
135177
public static String toJSONString(Object obj, SerializerFeature... features) {
178+
if (obj instanceof String) {
179+
return (String) obj;
180+
}
136181
try {
137182
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
138183
} catch (Exception e) {
139-
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
184+
Log.i(TAG, "toJSONString catch \n" + e.getMessage());
140185
}
141186
return null;
142187
}
@@ -146,7 +191,14 @@ public static String toJSONString(Object obj, SerializerFeature... features) {
146191
* @return
147192
*/
148193
public static String format(String json) {
149-
return toJSONString(parseObject(json), SerializerFeature.PrettyFormat);
194+
return format(parseObject(json));
195+
}
196+
/**格式化,显示更好看
197+
* @param object
198+
* @return
199+
*/
200+
public static String format(JSONObject object) {
201+
return toJSONString(object, SerializerFeature.PrettyFormat);
150202
}
151203

152204

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
public static final String KEY_COLUMNS = "@columns";//@key关键字都放这个类
74+
75+
/**set columns need to be returned
76+
* @param columns "column0,column1,column2..."
77+
* @return
78+
*/
79+
public JSONObject setColumns(String columns) {
80+
put(KEY_COLUMNS, columns);
81+
return this;
82+
}
83+
public String getColumns() {
84+
return getString(KEY_COLUMNS);
85+
}
86+
87+
88+
89+
//judge <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
90+
public static final String KEY_ARRAY = "[]";
91+
92+
/**判断是否为Array的key
93+
* @param key
94+
* @return
95+
*/
96+
public static boolean isArrayKey(String key) {
97+
return key != null && key.endsWith(KEY_ARRAY);
98+
}
99+
/**判断是否为对应Table的key
100+
* @param key
101+
* @return
102+
*/
103+
public static boolean isTableKey(String key) {
104+
return isWord(key) && bigAlphaPattern.matcher(key.substring(0, 1)).matches();
105+
}
106+
/**判断是否为词,只能包含字母,数字或下划线
107+
* @param key
108+
* @return
109+
*/
110+
public static boolean isWord(String key) {
111+
return StringUtil.isNotEmpty(key, false) && namePattern.matcher(key).matches();
112+
}
113+
//judge >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
114+
115+
}

0 commit comments

Comments
 (0)