001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.utils;
017
018import com.alibaba.fastjson.JSON;
019import com.alibaba.fastjson.JSONArray;
020import com.alibaba.fastjson.JSONObject;
021import com.jfinal.kit.LogKit;
022import io.jboot.web.json.JsonBodyParseInterceptor;
023
024import java.math.BigDecimal;
025import java.math.BigInteger;
026import java.util.Date;
027import java.util.List;
028import java.util.Set;
029
030/**
031 * 基于 FastJson,方便解析 Json 内容
032 * <p>
033 * 例如:
034 * <p>
035 * {
036 * "array": [
037 * 1,
038 * 2,
039 * 3
040 * ],
041 * "type": true,
042 * "null": null,
043 * "number": 123,
044 * "object": {
045 * "a": "b",
046 * "c": "d",
047 * "e":1
048 * },
049 * "key": "welcome to CodeFormat.CN"
050 * }
051 * <p>
052 * Boolean type = JsonUtil.getBool(json,"type");
053 * //type == true
054 * <p>
055 * int e = JsonUtil.getInt(json,"object.e")
056 * // e == 1
057 * <p>
058 * BigInteger n = JsonUtil.getBigInteger("number")
059 * // n == 123
060 * <p>
061 * String[] array = JsonUtil.get(json,"array",String[].class)
062 * //array == ["1","2","3"]
063 * <p>
064 * int[] array = JsonUtil.get(json,"array",int[].class)
065 * //array == [1,2,3]
066 * <p>
067 * Map map = JsonUtil.get(json,"object",Map.class)
068 * //map == {"a":"b","c":"d","e":1}
069 * <p>
070 * int x = JsonUtil.getInt(json,"array[1]");
071 * // x == 2
072 * <p>
073 * String key = JsonUtil.getString(json,"key");
074 * // key == "welcome to CodeFormat.CN"
075 */
076public class JsonUtil {
077
078    public static String getString(String json, String key) {
079        return get(json, key, String.class);
080    }
081
082    public static String getString(Object jsonObjectOrArray, String key) {
083        return get(jsonObjectOrArray, key, String.class);
084    }
085
086    public static String getString(String json, String key, String defaultValue) {
087        String value = getString(json, key);
088        return value != null ? value : defaultValue;
089    }
090
091    public static String getString(Object jsonObjectOrArray, String key, String defaultValue) {
092        String value = getString(jsonObjectOrArray, key);
093        return value != null ? value : defaultValue;
094    }
095
096
097    public static Boolean getBool(String json, String key) {
098        return get(json, key, Boolean.class);
099    }
100
101    public static Boolean getBool(Object jsonObjectOrArray, String key) {
102        return get(jsonObjectOrArray, key, Boolean.class);
103    }
104
105    public static boolean getBool(String json, String key, boolean defaultValue) {
106        Boolean value = getBool(json, key);
107        return value != null ? value : defaultValue;
108    }
109
110    public static boolean getBool(Object jsonObjectOrArray, String key, boolean defaultValue) {
111        Boolean value = getBool(jsonObjectOrArray, key);
112        return value != null ? value : defaultValue;
113    }
114
115
116    public static Integer getInt(String json, String key) {
117        return get(json, key, Integer.class);
118    }
119
120    public static Integer getInt(Object jsonObjectOrArray, String key) {
121        return get(jsonObjectOrArray, key, Integer.class);
122    }
123
124    public static int getInt(String json, String key, int defaultValue) {
125        Integer value = getInt(json, key);
126        return value != null ? value : defaultValue;
127    }
128
129    public static int getInt(Object jsonObjectOrArray, String key, int defaultValue) {
130        Integer value = getInt(jsonObjectOrArray, key);
131        return value != null ? value : defaultValue;
132    }
133
134    public static Float getFloat(String json, String key) {
135        return get(json, key, Float.class);
136    }
137
138    public static Float getFloat(Object jsonObjectOrArray, String key) {
139        return get(jsonObjectOrArray, key, Float.class);
140    }
141
142    public static float getFloat(String json, String key, float defaultValue) {
143        Float value = getFloat(json, key);
144        return value != null ? value : defaultValue;
145    }
146
147    public static float getFloat(Object jsonObjectOrArray, String key, float defaultValue) {
148        Float value = getFloat(jsonObjectOrArray, key);
149        return value != null ? value : defaultValue;
150    }
151
152    public static Double getDouble(String json, String key) {
153        return get(json, key, Double.class);
154    }
155
156    public static Double getDouble(Object jsonObjectOrArray, String key) {
157        return get(jsonObjectOrArray, key, Double.class);
158    }
159
160    public static double getDouble(String json, String key, double defaultValue) {
161        Double value = getDouble(json, key);
162        return value != null ? value : defaultValue;
163    }
164
165    public static double getDouble(Object jsonObjectOrArray, String key, double defaultValue) {
166        Double value = getDouble(jsonObjectOrArray, key);
167        return value != null ? value : defaultValue;
168    }
169
170    public static Long getLong(String json, String key) {
171        return get(json, key, Long.class);
172    }
173
174    public static Long getLong(Object jsonObjectOrArray, String key) {
175        return get(jsonObjectOrArray, key, Long.class);
176    }
177
178    public static long getLong(String json, String key, long defaultValue) {
179        Long value = getLong(json, key);
180        return value != null ? value : defaultValue;
181    }
182
183    public static long getLong(Object jsonObjectOrArray, String key, long defaultValue) {
184        Long value = getLong(jsonObjectOrArray, key);
185        return value != null ? value : defaultValue;
186    }
187
188    public static BigInteger getBigInteger(String json, String key) {
189        return get(json, key, BigInteger.class);
190    }
191
192    public static BigInteger getBigInteger(Object jsonObjectOrArray, String key) {
193        return get(jsonObjectOrArray, key, BigInteger.class);
194    }
195
196    public static BigInteger getBigInteger(String json, String key, BigInteger defaultValue) {
197        BigInteger value = getBigInteger(json, key);
198        return value != null ? value : defaultValue;
199    }
200
201
202    public static BigInteger getBigInteger(Object jsonObjectOrArray, String key, BigInteger defaultValue) {
203        BigInteger value = getBigInteger(jsonObjectOrArray, key);
204        return value != null ? value : defaultValue;
205    }
206
207    public static BigDecimal getBigDecimal(String json, String key) {
208        return get(json, key, BigDecimal.class);
209    }
210
211    public static BigDecimal getBigDecimal(Object jsonObjectOrArray, String key) {
212        return get(jsonObjectOrArray, key, BigDecimal.class);
213    }
214
215    public static BigDecimal getBigDecimal(String json, String key, BigDecimal defaultValue) {
216        BigDecimal value = getBigDecimal(json, key);
217        return value != null ? value : defaultValue;
218    }
219
220
221    public static BigDecimal getBigDecimal(Object jsonObjectOrArray, String key, BigDecimal defaultValue) {
222        BigDecimal value = getBigDecimal(jsonObjectOrArray, key);
223        return value != null ? value : defaultValue;
224    }
225
226
227    public static Date getDate(String json, String key) {
228        return get(json, key, Date.class);
229    }
230
231    public static Date getDate(Object jsonObjectOrArray, String key) {
232        return get(jsonObjectOrArray, key, Date.class);
233    }
234
235    public static Date getDate(String json, String key, Date defaultValue) {
236        Date date = get(json, key, Date.class);
237        return date != null ? date : defaultValue;
238    }
239
240    public static Date getDate(Object jsonObjectOrArray, String key, Date defaultValue) {
241        Date date = get(jsonObjectOrArray, key, Date.class);
242        return date != null ? date : defaultValue;
243    }
244
245
246    public static JSONObject getJSONObject(String json, String key) {
247        return get(json, key, JSONObject.class);
248    }
249
250    public static JSONObject getJSONObject(Object jsonObjectOrArray, String key) {
251        return get(jsonObjectOrArray, key, JSONObject.class);
252    }
253
254    public static JSONArray getJSONArray(String json, String key) {
255        return get(json, key, JSONArray.class);
256    }
257
258    public static JSONArray getJSONArray(Object jsonObjectOrArray, String key) {
259        return get(jsonObjectOrArray, key, JSONArray.class);
260    }
261
262
263    public static <T> List<T> getList(String json, String key, Class<T> clazz) {
264        return getList(getJsonObjectOrArray(json), key, clazz);
265    }
266
267
268    public static <T> List<T> getList(Object jsonObjectOrArray, String key, Class<T> clazz) {
269        return get(jsonObjectOrArray, key, TypeDef.wrapper(List.class, clazz));
270    }
271
272
273    public static <T> Set<T> getSet(String json, String key, Class<T> clazz) {
274        return getSet(getJsonObjectOrArray(json), key, clazz);
275    }
276
277
278    public static <T> Set<T> getSet(Object jsonObjectOrArray, String key, Class<T> clazz) {
279        return get(jsonObjectOrArray, key, TypeDef.wrapper(Set.class, clazz));
280    }
281
282
283    public static <T> T get(String json, String key, Class<T> clazz) {
284        return get(getJsonObjectOrArray(json), key, clazz);
285    }
286
287    public static <T> T get(Object jsonObjectOrArray, String key, Class<T> clazz) {
288        if (jsonObjectOrArray == null) {
289            return null;
290        }
291        try {
292            return (T) JsonBodyParseInterceptor.parseJsonBody(jsonObjectOrArray, clazz, clazz, key);
293        } catch (Exception e) {
294            LogKit.error(e.toString(), e);
295        }
296        return null;
297    }
298
299
300    public static <T> T get(String json, String key, TypeDef<?> typeDef) {
301        return get(getJsonObjectOrArray(json), key, typeDef);
302    }
303
304
305    public static <T> T get(Object jsonObjectOrArray, String key, TypeDef<?> typeDef) {
306        if (jsonObjectOrArray == null) {
307            return null;
308        }
309        try {
310            return (T) JsonBodyParseInterceptor.parseJsonBody(jsonObjectOrArray, typeDef.getDefClass(), typeDef.getType(), key);
311        } catch (Exception e) {
312            LogKit.error(e.toString(), e);
313        }
314        return null;
315    }
316
317    public static Object getJsonObjectOrArray(String json) {
318        if (StrUtil.isNotBlank(json)) {
319            try {
320                return JSON.parse(json);
321            } catch (Exception e) {
322                LogKit.error(e.toString(), e);
323            }
324        }
325        return null;
326    }
327
328
329}