94 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
94 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils.io;
|
|
|
15 |
|
132 |
ilm |
16 |
import org.openconcerto.utils.NumberUtils;
|
142 |
ilm |
17 |
import org.openconcerto.utils.XMLDateFormat;
|
132 |
ilm |
18 |
|
94 |
ilm |
19 |
import java.awt.Color;
|
142 |
ilm |
20 |
import java.math.BigDecimal;
|
|
|
21 |
import java.math.BigInteger;
|
|
|
22 |
import java.text.Format;
|
94 |
ilm |
23 |
import java.util.Calendar;
|
142 |
ilm |
24 |
import java.util.Date;
|
185 |
ilm |
25 |
import java.util.function.Function;
|
94 |
ilm |
26 |
|
144 |
ilm |
27 |
import com.google.gson.Gson;
|
|
|
28 |
import com.google.gson.JsonElement;
|
|
|
29 |
import com.google.gson.reflect.TypeToken;
|
|
|
30 |
|
94 |
ilm |
31 |
import net.minidev.json.JSONArray;
|
|
|
32 |
import net.minidev.json.JSONObject;
|
132 |
ilm |
33 |
import net.minidev.json.parser.JSONParser;
|
|
|
34 |
import net.minidev.json.parser.ParseException;
|
94 |
ilm |
35 |
|
|
|
36 |
public class JSONConverter {
|
|
|
37 |
|
144 |
ilm |
38 |
// type-safe methods
|
|
|
39 |
|
|
|
40 |
public static final <T> T fromJson(final Gson gson, String json, TypeToken<T> typeOfT) {
|
|
|
41 |
return gson.fromJson(json, typeOfT.getType());
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public static final <T> T fromJson(final Gson gson, JsonElement json, TypeToken<T> typeOfT) {
|
|
|
45 |
return gson.fromJson(json, typeOfT.getType());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// ** net.minidev.json
|
|
|
49 |
|
142 |
ilm |
50 |
static private final Format DF = new XMLDateFormat();
|
|
|
51 |
|
|
|
52 |
static synchronized private final String format(final Date d) {
|
|
|
53 |
return DF.format(d);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public synchronized static String formatCalendar(final Calendar calendar) {
|
|
|
57 |
return DF.format(calendar);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
static synchronized private final Date parse(final String s) throws java.text.ParseException {
|
|
|
61 |
return (Date) DF.parseObject(s);
|
|
|
62 |
}
|
|
|
63 |
|
94 |
ilm |
64 |
public static Object getJSON(Object param) {
|
|
|
65 |
Object result = null;
|
|
|
66 |
|
|
|
67 |
if (param != null) {
|
132 |
ilm |
68 |
if (param instanceof HTMLable) {
|
|
|
69 |
result = ((HTMLable) param).getHTML();
|
|
|
70 |
} else if (param instanceof JSONAble) {
|
94 |
ilm |
71 |
result = ((JSONAble) param).toJSON();
|
185 |
ilm |
72 |
} else if (param instanceof JSONNamed) {
|
|
|
73 |
result = ((JSONNamed) param).getJSONName();
|
142 |
ilm |
74 |
} else if (param instanceof Date) {
|
|
|
75 |
result = format((Date) param);
|
132 |
ilm |
76 |
} else if (param instanceof Calendar) {
|
142 |
ilm |
77 |
result = formatCalendar(((Calendar) param));
|
94 |
ilm |
78 |
} else if (param instanceof Class<?>) {
|
182 |
ilm |
79 |
if (param == String.class) {
|
|
|
80 |
result = "string";
|
|
|
81 |
} else {
|
|
|
82 |
result = ((Class<?>) param).getName();
|
|
|
83 |
}
|
94 |
ilm |
84 |
} else if (param instanceof Iterable) {
|
|
|
85 |
final Iterable<?> tmp = (Iterable<?>) param;
|
|
|
86 |
final JSONArray jsonArray = new JSONArray();
|
|
|
87 |
for (Object o : tmp) {
|
|
|
88 |
jsonArray.add(getJSON(o));
|
|
|
89 |
}
|
|
|
90 |
result = jsonArray;
|
|
|
91 |
} else if (param instanceof Color) {
|
182 |
ilm |
92 |
String hexString = Integer.toHexString(((Color) param).getRGB());
|
|
|
93 |
if (hexString.length() > 6) {
|
|
|
94 |
hexString = hexString.substring(2, hexString.length());
|
94 |
ilm |
95 |
}
|
182 |
ilm |
96 |
result = "#" + hexString;
|
142 |
ilm |
97 |
} else if (param instanceof BigDecimal) {
|
|
|
98 |
result = ((BigDecimal) param).doubleValue();
|
185 |
ilm |
99 |
} else if (param instanceof Enum) {
|
|
|
100 |
result = ((Enum<?>) param).name().toLowerCase();
|
94 |
ilm |
101 |
} else {
|
|
|
102 |
result = param;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return result;
|
|
|
107 |
}
|
|
|
108 |
|
142 |
ilm |
109 |
public static <T> T getObjectFromJSON(final Object o, final Class<T> type) {
|
|
|
110 |
final T result;
|
94 |
ilm |
111 |
if (o != null && !o.equals("null")) {
|
142 |
ilm |
112 |
if (type.isInstance(o)) {
|
|
|
113 |
result = type.cast(o);
|
|
|
114 |
} else if (type.equals(Integer.class)) {
|
|
|
115 |
final int intVal;
|
|
|
116 |
if (o instanceof BigDecimal) {
|
|
|
117 |
intVal = ((BigDecimal) o).intValueExact();
|
|
|
118 |
} else if (o instanceof BigInteger) {
|
|
|
119 |
// TODO use intValueExact() in Java 8
|
|
|
120 |
final BigInteger bigInt = (BigInteger) o;
|
|
|
121 |
if (bigInt.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 || bigInt.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
|
|
|
122 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'");
|
|
|
123 |
intVal = bigInt.intValue();
|
94 |
ilm |
124 |
} else {
|
|
|
125 |
try {
|
142 |
ilm |
126 |
intVal = NumberUtils.ensureInt((Long) o);
|
|
|
127 |
} catch (ArithmeticException ex) {
|
|
|
128 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'", ex);
|
94 |
ilm |
129 |
}
|
|
|
130 |
}
|
142 |
ilm |
131 |
result = type.cast(intVal);
|
132 |
ilm |
132 |
} else if (type.equals(Date.class)) {
|
94 |
ilm |
133 |
final String sparam = (String) o;
|
142 |
ilm |
134 |
try {
|
|
|
135 |
final Date c = parse(sparam);
|
|
|
136 |
result = type.cast(c);
|
|
|
137 |
} catch (java.text.ParseException e) {
|
|
|
138 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "', the format is not valid", e);
|
94 |
ilm |
139 |
}
|
142 |
ilm |
140 |
} else if (type.equals(Color.class)) {
|
182 |
ilm |
141 |
result = type.cast(Color.decode(o.toString()));
|
185 |
ilm |
142 |
} else if (JSONNamed.class.isAssignableFrom(type)) {
|
|
|
143 |
for (final T enumConstant : type.getEnumConstants()) {
|
|
|
144 |
if (((JSONNamed) enumConstant).getJSONName().equals(o))
|
|
|
145 |
return enumConstant;
|
|
|
146 |
}
|
|
|
147 |
throw new IllegalArgumentException("Unknown name '" + o + "' for " + type);
|
|
|
148 |
} else if (Enum.class.isAssignableFrom(type)) {
|
|
|
149 |
@SuppressWarnings("unchecked")
|
|
|
150 |
final Enum<?> enumVal = Enum.valueOf(type.asSubclass(Enum.class), o.toString().toUpperCase());
|
|
|
151 |
result = type.cast(enumVal);
|
94 |
ilm |
152 |
} else {
|
142 |
ilm |
153 |
result = type.cast(o);
|
94 |
ilm |
154 |
}
|
132 |
ilm |
155 |
} else {
|
|
|
156 |
result = null;
|
94 |
ilm |
157 |
}
|
|
|
158 |
|
142 |
ilm |
159 |
return result;
|
94 |
ilm |
160 |
}
|
|
|
161 |
|
142 |
ilm |
162 |
public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type) {
|
94 |
ilm |
163 |
return getParameterFromJSON(json, key, type, null);
|
|
|
164 |
}
|
|
|
165 |
|
142 |
ilm |
166 |
public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type, T defaultValue) {
|
185 |
ilm |
167 |
return getParameterFromJSON(json, key, (o) -> getObjectFromJSON(o, type), defaultValue);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Function<Object, T> ctor, T defaultValue) {
|
180 |
ilm |
171 |
if (json == null) {
|
|
|
172 |
throw new IllegalArgumentException("null JSON");
|
|
|
173 |
}
|
|
|
174 |
if (key == null) {
|
|
|
175 |
throw new IllegalArgumentException("null key");
|
|
|
176 |
}
|
185 |
ilm |
177 |
return json.containsKey(key) ? ctor.apply(json.get(key)) : defaultValue;
|
94 |
ilm |
178 |
}
|
|
|
179 |
|
132 |
ilm |
180 |
public static JSONObject convertStringToJsonObject(final String jsonString) {
|
|
|
181 |
final JSONParser parser = new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT);
|
|
|
182 |
final JSONObject json;
|
|
|
183 |
try {
|
|
|
184 |
json = (JSONObject) parser.parse(jsonString);
|
|
|
185 |
} catch (final ParseException ex) {
|
|
|
186 |
throw new IllegalArgumentException(ex.getMessage(), ex);
|
|
|
187 |
}
|
|
|
188 |
return json;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
public static JSONArray convertStringToJsonArray(final String jsonString) {
|
|
|
192 |
final JSONParser parser = new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT);
|
|
|
193 |
final JSONArray json;
|
|
|
194 |
try {
|
|
|
195 |
json = (JSONArray) parser.parse(jsonString);
|
|
|
196 |
} catch (final ParseException ex) {
|
|
|
197 |
throw new IllegalArgumentException(ex.getMessage(), ex);
|
|
|
198 |
}
|
|
|
199 |
return json;
|
|
|
200 |
}
|
94 |
ilm |
201 |
}
|