Skip to content

Commit 6e3d006

Browse files
TommyTommy
authored andcommitted
1 parent 79bf3a9 commit 6e3d006

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+13260
-0
lines changed

pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>zuo.biao.apijson.server</groupId>
7+
<artifactId>apijson-orm</artifactId>
8+
<version>3.9.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>APIJSONORM</name>
12+
<description>APIJSON ORM Library</description>
13+
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.alibaba</groupId>
24+
<artifactId>fastjson</artifactId>
25+
<version>1.2.58</version>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<configuration>
35+
<source>1.8</source>
36+
<target>1.8</target>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
43+
</project>
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
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 com.alibaba.fastjson.JSONArray;
18+
import com.alibaba.fastjson.JSONObject;
19+
import com.alibaba.fastjson.parser.Feature;
20+
import com.alibaba.fastjson.serializer.SerializerFeature;
21+
22+
import java.util.List;
23+
24+
/**阿里FastJSON封装类 防止解析时异常
25+
* @author Lemon
26+
*/
27+
public class JSON {
28+
private static final String TAG = "JSON";
29+
30+
/**判断json格式是否正确
31+
* @param s
32+
* @return
33+
*/
34+
public static boolean isJsonCorrect(String s) {
35+
//太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
36+
if (s == null
37+
// || s.equals("[]")
38+
// || s.equals("{}")
39+
|| s.equals("")
40+
|| s.equals("[null]")
41+
|| s.equals("{null}")
42+
|| s.equals("null")) {
43+
return false;
44+
}
45+
return true;
46+
}
47+
48+
/**获取有效的json
49+
* @param s
50+
* @return
51+
*/
52+
public static String getCorrectJson(String s) {
53+
return getCorrectJson(s, false);
54+
}
55+
/**获取有效的json
56+
* @param s
57+
* @param isArray
58+
* @return
59+
*/
60+
public static String getCorrectJson(String s, boolean isArray) {
61+
s = StringUtil.getTrimedString(s);
62+
// if (isArray) {
63+
// while (s.startsWith("\"")) {
64+
// s = s.substring(1);
65+
// }
66+
// while (s.endsWith("\"")) {
67+
// s = s.substring(0, s.length() - 1);
68+
// }
69+
// }
70+
return s;//isJsonCorrect(s) ? s : null;
71+
}
72+
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+
}
87+
/**obj转JSONObject
88+
* @param json
89+
* @return
90+
*/
91+
public static JSONObject parseObject(Object obj) {
92+
if (obj instanceof JSONObject) {
93+
return (JSONObject) obj;
94+
}
95+
return parseObject(toJSONString(obj));
96+
}
97+
/**json转JSONObject
98+
* @param json
99+
* @return
100+
*/
101+
public static JSONObject parseObject(String json) {
102+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
103+
features |= Feature.OrderedField.getMask();
104+
return parseObject(json, features);
105+
}
106+
/**json转JSONObject
107+
* @param json
108+
* @param features
109+
* @return
110+
*/
111+
public static JSONObject parseObject(String json, int features) {
112+
try {
113+
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
114+
} catch (Exception e) {
115+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
116+
}
117+
return null;
118+
}
119+
120+
/**JSONObject转实体类
121+
* @param object
122+
* @param clazz
123+
* @return
124+
*/
125+
public static <T> T parseObject(JSONObject object, Class<T> clazz) {
126+
return parseObject(toJSONString(object), clazz);
127+
}
128+
/**json转实体类
129+
* @param json
130+
* @param clazz
131+
* @return
132+
*/
133+
public static <T> T parseObject(String json, Class<T> clazz) {
134+
if (clazz == null) {
135+
Log.e(TAG, "parseObject clazz == null >> return null;");
136+
} else {
137+
try {
138+
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
139+
features |= Feature.OrderedField.getMask();
140+
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
141+
} catch (Exception e) {
142+
Log.i(TAG, "parseObject catch \n" + e.getMessage());
143+
}
144+
}
145+
return null;
146+
}
147+
148+
/**list转JSONArray
149+
* @param list
150+
* @return
151+
*/
152+
public static JSONArray parseArray(List<Object> list) {
153+
return new JSONArray(list);
154+
}
155+
/**obj转JSONArray
156+
* @param obj
157+
* @return
158+
*/
159+
public static JSONArray parseArray(Object obj) {
160+
if (obj instanceof JSONArray) {
161+
return (JSONArray) obj;
162+
}
163+
return parseArray(toJSONString(obj));
164+
}
165+
/**json转JSONArray
166+
* @param json
167+
* @return
168+
*/
169+
public static JSONArray parseArray(String json) {
170+
try {
171+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true));
172+
} catch (Exception e) {
173+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
174+
}
175+
return null;
176+
}
177+
/**JSONArray转实体类列表
178+
* @param array
179+
* @param clazz
180+
* @return
181+
*/
182+
public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
183+
return parseArray(toJSONString(array), clazz);
184+
}
185+
/**json转实体类列表
186+
* @param json
187+
* @param clazz
188+
* @return
189+
*/
190+
public static <T> List<T> parseArray(String json, Class<T> clazz) {
191+
if (clazz == null) {
192+
Log.e(TAG, "parseArray clazz == null >> return null;");
193+
} else {
194+
try {
195+
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
196+
} catch (Exception e) {
197+
Log.i(TAG, "parseArray catch \n" + e.getMessage());
198+
}
199+
}
200+
return null;
201+
}
202+
203+
/**实体类转json
204+
* @param obj
205+
* @return
206+
*/
207+
public static String toJSONString(Object obj) {
208+
if (obj instanceof String) {
209+
return (String) obj;
210+
}
211+
try {
212+
return com.alibaba.fastjson.JSON.toJSONString(obj);
213+
} catch (Exception e) {
214+
Log.e(TAG, "toJSONString catch \n" + e.getMessage());
215+
}
216+
return null;
217+
}
218+
219+
/**实体类转json
220+
* @param obj
221+
* @param features
222+
* @return
223+
*/
224+
public static String toJSONString(Object obj, SerializerFeature... features) {
225+
if (obj instanceof String) {
226+
return (String) obj;
227+
}
228+
try {
229+
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
230+
} catch (Exception e) {
231+
Log.e(TAG, "parseArray catch \n" + e.getMessage());
232+
}
233+
return null;
234+
}
235+
236+
/**格式化,显示更好看
237+
* @param json
238+
* @return
239+
*/
240+
public static String format(String json) {
241+
return format(parse(json));
242+
}
243+
/**格式化,显示更好看
244+
* @param object
245+
* @return
246+
*/
247+
public static String format(Object object) {
248+
return toJSONString(object, SerializerFeature.PrettyFormat);
249+
}
250+
251+
/**判断是否为JSONObject
252+
* @param obj instanceof String ? parseObject
253+
* @return
254+
*/
255+
public static boolean isJSONObject(Object obj) {
256+
if (obj instanceof JSONObject) {
257+
return true;
258+
}
259+
if (obj instanceof String) {
260+
try {
261+
JSONObject json = parseObject((String) obj);
262+
return json != null && json.isEmpty() == false;
263+
} catch (Exception e) {
264+
Log.e(TAG, "isJSONObject catch \n" + e.getMessage());
265+
}
266+
}
267+
268+
return false;
269+
}
270+
/**判断是否为JSONArray
271+
* @param obj instanceof String ? parseArray
272+
* @return
273+
*/
274+
public static boolean isJSONArray(Object obj) {
275+
if (obj instanceof JSONArray) {
276+
return true;
277+
}
278+
if (obj instanceof String) {
279+
try {
280+
JSONArray json = parseArray((String) obj);
281+
return json != null && json.isEmpty() == false;
282+
} catch (Exception e) {
283+
Log.e(TAG, "isJSONArray catch \n" + e.getMessage());
284+
}
285+
}
286+
287+
return false;
288+
}
289+
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+
298+
}

0 commit comments

Comments
 (0)