| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4fed749 commit bd5161f
19 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,67 +4,77 @@ | |||
| 4 | 4 | ||
| 5 | 5 | package apijson; | |
| 6 | 6 | ||
| 7 | + import java.util.Collection; | ||
| 8 | + import java.util.LinkedHashMap; | ||
| 7 | 9 | import java.util.List; | |
| 8 | 10 | import java.util.Map; | |
| 9 | 11 | ||
| 10 | 12 | /**JSON工具类 防止解析时异常 | |
| 11 | 13 | * @author Lemon | |
| 12 | 14 | */ | |
| 13 | 15 | public class JSON { | |
| 14 | - public static Class<?> JSON_OBJECT_CLASS = JSONObject.class; | ||
| 15 | - public static Class<?> JSON_ARRAY_CLASS = JSONArray.class; | ||
| 16 | 16 | ||
| 17 | 17 | static final String TAG = "JSON"; | |
| 18 | 18 | ||
| 19 | 19 | public static JSONParser<? extends Map<String, Object>, ? extends List<Object>> DEFAULT_JSON_PARSER; | |
| 20 | 20 | ||
| 21 | 21 | static { | |
| 22 | - DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() { | ||
| 23 | - | ||
| 24 | - @Override | ||
| 25 | - public JSONObject createJSONObject() { | ||
| 26 | - return new JSONObject(); | ||
| 27 | - } | ||
| 28 | - | ||
| 29 | - @Override | ||
| 30 | - public JSONArray createJSONArray() { | ||
| 31 | - return new JSONArray(); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - @Override | ||
| 35 | - public String toJSONString(Object obj, boolean format) { | ||
| 36 | - throw new UnsupportedOperationException(); | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - @Override | ||
| 40 | - public Object parseJSON(Object json) { | ||
| 41 | - throw new UnsupportedOperationException(); | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - @Override | ||
| 45 | - public JSONObject parseObject(Object json) { | ||
| 46 | - throw new UnsupportedOperationException(); | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - @Override | ||
| 50 | - public <T> T parseObject(Object json, Class<T> clazz) { | ||
| 51 | - throw new UnsupportedOperationException(); | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - @Override | ||
| 55 | - public JSONArray parseArray(Object json) { | ||
| 56 | - throw new UnsupportedOperationException(); | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - @Override | ||
| 60 | - public <T> List<T> parseArray(Object json, Class<T> clazz) { | ||
| 61 | - throw new UnsupportedOperationException(); | ||
| 62 | - } | ||
| 22 | + //DEFAULT_JSON_PARSER = new JSONParser<LinkedHashMap<String, Object>, List<Object>>() { | ||
| 23 | + // | ||
| 24 | + // @Override | ||
| 25 | + // public LinkedHashMap<String, Object> createJSONObject() { | ||
| 26 | + // throw new UnsupportedOperationException(); | ||
| 27 | + // } | ||
| 28 | + // | ||
| 29 | + // @Override | ||
| 30 | + // public List<Object> createJSONArray() { | ||
| 31 | + // throw new UnsupportedOperationException(); | ||
| 32 | + // } | ||
| 33 | + // | ||
| 34 | + // @Override | ||
| 35 | + // public String toJSONString(Object obj, boolean format) { | ||
| 36 | + // throw new UnsupportedOperationException(); | ||
| 37 | + // } | ||
| 38 | + // | ||
| 39 | + // @Override | ||
| 40 | + // public Object parse(Object json) { | ||
| 41 | + // throw new UnsupportedOperationException(); | ||
| 42 | + // } | ||
| 43 | + // | ||
| 44 | + // @Override | ||
| 45 | + // public LinkedHashMap<String, Object> parseObject(Object json) { | ||
| 46 | + // throw new UnsupportedOperationException(); | ||
| 47 | + // } | ||
| 48 | + // | ||
| 49 | + // @Override | ||
| 50 | + // public <T> T parseObject(Object json, Class<T> clazz) { | ||
| 51 | + // throw new UnsupportedOperationException(); | ||
| 52 | + // } | ||
| 53 | + // | ||
| 54 | + // @Override | ||
| 55 | + // public List<Object> parseArray(Object json) { | ||
| 56 | + // throw new UnsupportedOperationException(); | ||
| 57 | + // } | ||
| 58 | + // | ||
| 59 | + // @Override | ||
| 60 | + // public <T> List<T> parseArray(Object json, Class<T> clazz) { | ||
| 61 | + // throw new UnsupportedOperationException(); | ||
| 62 | + // } | ||
| 63 | + // | ||
| 64 | + //}; | ||
| 63 | 65 | ||
| 64 | - }; | ||
| 65 | 66 | } | |
| 66 | 67 | ||
| 67 | 68 | // public static JSONCreator<? extends Map<String, Object>, ? extends List<Object>> DEFAULT_JSON_CREATOR = DEFAULT_JSON_PARSER; | |
| 69 | + // public static <M extends Map<String, Object>> M newObj() { | ||
| 70 | + // return createJSONObject(); | ||
| 71 | + // } | ||
| 72 | + // public static <M extends Map<String, Object>> M newObj(String key, Object value) { | ||
| 73 | + // return createJSONObject(key, value); | ||
| 74 | + // } | ||
| 75 | + // public static <M extends Map<String, Object>> M newObj(Map<? extends String, ?> map) { | ||
| 76 | + // return createJSONObject(map); | ||
| 77 | + // } | ||
| 68 | 78 | ||
| 69 | 79 | public static <M extends Map<String, Object>> M createJSONObject() { | |
| 70 | 80 | return (M) DEFAULT_JSON_PARSER.createJSONObject(); | |
@@ -76,87 +86,61 @@ public static <M extends Map<String, Object>> M createJSONObject(Map<? extends S | |||
| 76 | 86 | return (M) DEFAULT_JSON_PARSER.createJSONObject(map); | |
| 77 | 87 | } | |
| 78 | 88 | ||
| 89 | + //public static <L extends List<Object>> L newArr() { | ||
| 90 | + // return createJSONArray(); | ||
| 91 | + //} | ||
| 92 | + //public static <L extends List<Object>> L newArr(Object obj) { | ||
| 93 | + // return createJSONArray(obj); | ||
| 94 | + //} | ||
| 95 | + //public static <L extends List<Object>> L newArr(List<?> list) { | ||
| 96 | + // return createJSONArray(list); | ||
| 97 | + //} | ||
| 98 | + | ||
| 79 | 99 | public static <L extends List<Object>> L createJSONArray() { | |
| 80 | 100 | return (L) DEFAULT_JSON_PARSER.createJSONArray(); | |
| 81 | 101 | } | |
| 82 | 102 | public static <L extends List<Object>> L createJSONArray(Object obj) { | |
| 83 | 103 | return (L) DEFAULT_JSON_PARSER.createJSONArray(obj); | |
| 84 | 104 | } | |
| 85 | - public static <L extends List<Object>> L createJSONArray(List<?> list) { | ||
| 105 | + public static <L extends List<Object>> L createJSONArray(Collection<?> list) { | ||
| 86 | 106 | return (L) DEFAULT_JSON_PARSER.createJSONArray(list); | |
| 87 | 107 | } | |
| 88 | 108 | ||
| 89 | - public static Object parseJSON(Object json) { | ||
| 90 | - if (json instanceof Boolean || json instanceof Number || json instanceof Enum<?>) { | ||
| 91 | - return json; | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - String s = StringUtil.trim(toJSONString(json)); | ||
| 95 | - if (s.startsWith("{")) { | ||
| 96 | - return parseObject(json, DEFAULT_JSON_PARSER); | ||
| 97 | - } | ||
| 98 | - | ||
| 99 | - if (s.startsWith("[")) { | ||
| 100 | - return parseArray(json, DEFAULT_JSON_PARSER); | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - try { | ||
| 104 | - return DEFAULT_JSON_PARSER.parseJSON(json); | ||
| 105 | - } catch (Throwable e) { | ||
| 106 | - throw new IllegalArgumentException("JSON 格式错误!" + e.getMessage() + "! " + s); | ||
| 107 | - } | ||
| 109 | + public static Object parse(Object json) { | ||
| 110 | + return DEFAULT_JSON_PARSER.parse(json); | ||
| 108 | 111 | } | |
| 109 | 112 | ||
| 110 | - /** | ||
| 111 | - * @param json | ||
| 112 | - * @return | ||
| 113 | - */ | ||
| 114 | - public static <M extends Map<String, Object>> M parseObject(Object json) { | ||
| 115 | - return (M) parseObject(json, DEFAULT_JSON_PARSER); | ||
| 116 | - } | ||
| 117 | 113 | ||
| 118 | - public static <M extends Map<String, Object>, L extends List<Object>> M parseObject(Object json, JSONParser<M, L> parser) { | ||
| 114 | + public static <M extends Map<String, Object>> M parseObject(Object json) { | ||
| 119 | 115 | String s = toJSONString(json); | |
| 120 | 116 | if (StringUtil.isEmpty(s, true)) { | |
| 121 | 117 | return null; | |
| 122 | 118 | } | |
| 123 | 119 | ||
| 124 | - return parser.parseObject(s); | ||
| 120 | + return (M) DEFAULT_JSON_PARSER.parseObject(s); | ||
| 125 | 121 | } | |
| 126 | 122 | ||
| 127 | 123 | public static <T> T parseObject(Object json, Class<T> clazz) { | |
| 128 | - return parseObject(json, clazz, DEFAULT_JSON_PARSER); | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - public static <T, M extends Map<String, Object>, L extends List<Object>> T parseObject(Object json, Class<T> clazz, JSONParser<M, L> parser) { | ||
| 132 | 124 | String s = toJSONString(json); | |
| 133 | 125 | if (StringUtil.isEmpty(s, true)) { | |
| 134 | 126 | return null; | |
| 135 | 127 | } | |
| 136 | 128 | ||
| 137 | - if (parser == null) { | ||
| 138 | - parser = (JSONParser<M, L>) DEFAULT_JSON_PARSER; | ||
| 139 | - } | ||
| 140 | - | ||
| 141 | - return parser.parseObject(s, clazz); | ||
| 129 | + return DEFAULT_JSON_PARSER.parseObject(s, clazz); | ||
| 142 | 130 | } | |
| 143 | 131 | ||
| 144 | 132 | /** | |
| 145 | 133 | * @param json | |
| 146 | 134 | * @return | |
| 147 | 135 | */ | |
| 148 | 136 | public static <L extends List<Object>> L parseArray(Object json) { | |
| 149 | - return (L) parseArray(json, DEFAULT_JSON_PARSER); | ||
| 150 | - } | ||
| 151 | - | ||
| 152 | - public static <M extends Map<String, Object>, L extends List<Object>> L parseArray(Object json, JSONParser<M, L> parser) { | ||
| 153 | 137 | String s = toJSONString(json); | |
| 154 | 138 | if (StringUtil.isEmpty(s, true)) { | |
| 155 | 139 | return null; | |
| 156 | 140 | } | |
| 157 | 141 | ||
| 158 | 142 | try { | |
| 159 | - L arr = parser.parseArray(s); | ||
| 143 | + L arr = (L) DEFAULT_JSON_PARSER.parseArray(s); | ||
| 160 | 144 | return arr; | |
| 161 | 145 | } catch (Exception e) { | |
| 162 | 146 | Log.i(TAG, "parseArray catch \n" + e.getMessage()); | |
@@ -165,17 +149,13 @@ public static <M extends Map<String, Object>, L extends List<Object>> L parseArr | |||
| 165 | 149 | } | |
| 166 | 150 | ||
| 167 | 151 | public static <T> List<T> parseArray(Object json, Class<T> clazz) { | |
| 168 | - return parseArray(json, clazz, DEFAULT_JSON_PARSER); | ||
| 169 | - } | ||
| 170 | - | ||
| 171 | - public static <T, M extends Map<String, Object>, L extends List<Object>> List<T> parseArray(Object json, Class<T> clazz, JSONParser<M, L> parser) { | ||
| 172 | 152 | String s = toJSONString(json); | |
| 173 | 153 | if (StringUtil.isEmpty(s, true)) { | |
| 174 | 154 | return null; | |
| 175 | 155 | } | |
| 176 | 156 | ||
| 177 | 157 | try { | |
| 178 | - return parser.parseArray(s, clazz); | ||
| 158 | + return DEFAULT_JSON_PARSER.parseArray(s, clazz); | ||
| 179 | 159 | } catch (Exception e) { | |
| 180 | 160 | Log.i(TAG, "parseArray catch \n" + e.getMessage()); | |
| 181 | 161 | } | |
@@ -347,14 +327,14 @@ public static <L extends List<Object>> L getJSONArray(List<Object> list, int ind | |||
| 347 | 327 | * @throws IllegalArgumentException If value is not a Map and cannot be converted | |
| 348 | 328 | */ | |
| 349 | 329 | @SuppressWarnings("unchecked") | |
| 350 | - public static Map<String, Object> getMap(Map<String, Object> map, String key) throws IllegalArgumentException { | ||
| 330 | + public static <K, V> Map<K, V> getMap(Map<String, Object> map, String key) throws IllegalArgumentException { | ||
| 351 | 331 | Object value = map == null || key == null ? null : map.get(key); | |
| 352 | 332 | if (value == null) { | |
| 353 | 333 | return null; | |
| 354 | 334 | } | |
| 355 | 335 | ||
| 356 | 336 | if (value instanceof Map) { | |
| 357 | - return (Map<String, Object>) value; | ||
| 337 | + return (Map<K, V>) value; | ||
| 358 | 338 | } | |
| 359 | 339 | ||
| 360 | 340 | throw new IllegalArgumentException("Value for key '" + key + "' is not a Map: " + value.getClass().getName()); | |
@@ -368,14 +348,14 @@ public static Map<String, Object> getMap(Map<String, Object> map, String key) th | |||
| 368 | 348 | * @throws IllegalArgumentException If value is not a List and cannot be converted | |
| 369 | 349 | */ | |
| 370 | 350 | @SuppressWarnings("unchecked") | |
| 371 | - public static List<Object> getList(Map<String, Object> map, String key) throws IllegalArgumentException { | ||
| 351 | + public static <T> List<T> getList(Map<String, Object> map, String key) throws IllegalArgumentException { | ||
| 372 | 352 | Object value = map == null || key == null ? null : map.get(key); | |
| 373 | 353 | if (value == null) { | |
| 374 | 354 | return null; | |
| 375 | 355 | } | |
| 376 | 356 | ||
| 377 | 357 | if (value instanceof List) { | |
| 378 | - return (List<Object>) value; | ||
| 358 | + return (List<T>) value; | ||
| 379 | 359 | } | |
| 380 | 360 | ||
| 381 | 361 | throw new IllegalArgumentException("Value for key '" + key + "' is not a List: " + value.getClass().getName()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments