94 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
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 |
|
|
|
16 |
import java.awt.Color;
|
|
|
17 |
import java.sql.Date;
|
|
|
18 |
import java.sql.Timestamp;
|
|
|
19 |
import java.text.SimpleDateFormat;
|
|
|
20 |
import java.util.Calendar;
|
|
|
21 |
import java.util.List;
|
|
|
22 |
import java.util.regex.Matcher;
|
|
|
23 |
import java.util.regex.Pattern;
|
|
|
24 |
|
|
|
25 |
import javax.xml.bind.DatatypeConverter;
|
|
|
26 |
|
|
|
27 |
import org.openconcerto.utils.NumberUtils;
|
|
|
28 |
import net.minidev.json.JSONArray;
|
|
|
29 |
import net.minidev.json.JSONObject;
|
|
|
30 |
|
|
|
31 |
public class JSONConverter {
|
|
|
32 |
final static Pattern pattern = Pattern.compile("d{4}-[01]d-[0-3]dT[0-2]d:[0-5]d:[0-5]d.d+([+-][0-2]d:[0-5]d|Z)");
|
|
|
33 |
|
|
|
34 |
public static Object getJSON(Object param) {
|
|
|
35 |
Object result = null;
|
|
|
36 |
|
|
|
37 |
if (param != null) {
|
|
|
38 |
if (param instanceof JSONAble) {
|
|
|
39 |
result = ((JSONAble) param).toJSON();
|
|
|
40 |
} else if (param instanceof Timestamp) {
|
|
|
41 |
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
|
|
|
42 |
result = df.format((Timestamp) param);
|
|
|
43 |
} else if (param instanceof Class<?>) {
|
|
|
44 |
result = ((Class<?>) param).getName();
|
|
|
45 |
} else if (param instanceof Iterable) {
|
|
|
46 |
final Iterable<?> tmp = (Iterable<?>) param;
|
|
|
47 |
final JSONArray jsonArray = new JSONArray();
|
|
|
48 |
for (Object o : tmp) {
|
|
|
49 |
jsonArray.add(getJSON(o));
|
|
|
50 |
}
|
|
|
51 |
result = jsonArray;
|
|
|
52 |
} else if (param instanceof Color) {
|
|
|
53 |
if (param != null) {
|
|
|
54 |
final Color paramColor = (Color) param;
|
|
|
55 |
final JSONObject jsonColor = new JSONObject();
|
|
|
56 |
jsonColor.put("r", paramColor.getRed());
|
|
|
57 |
jsonColor.put("g", paramColor.getGreen());
|
|
|
58 |
jsonColor.put("b", paramColor.getBlue());
|
|
|
59 |
result = jsonColor;
|
|
|
60 |
}
|
|
|
61 |
} else {
|
|
|
62 |
result = param;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
return result;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public static Object getObjectFromJSON(final Object o, final Class<?> type) {
|
|
|
70 |
Object result = null;
|
|
|
71 |
|
|
|
72 |
if (o != null && !o.equals("null")) {
|
|
|
73 |
if (type == Integer.class) {
|
|
|
74 |
if (!o.getClass().isAssignableFrom(Long.class)) {
|
|
|
75 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'");
|
|
|
76 |
} else {
|
|
|
77 |
try {
|
|
|
78 |
result = NumberUtils.ensureInt((Long) o);
|
|
|
79 |
} catch (IllegalArgumentException ex) {
|
|
|
80 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "', " + ex.getMessage());
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
} else if (type == Date.class) {
|
|
|
84 |
if (!o.getClass().isAssignableFrom(String.class)) {
|
|
|
85 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'");
|
|
|
86 |
}
|
|
|
87 |
final String sparam = (String) o;
|
|
|
88 |
final Matcher matcher = pattern.matcher(sparam);
|
|
|
89 |
if (matcher.find()) {
|
|
|
90 |
final Calendar c = DatatypeConverter.parseDateTime(sparam);
|
|
|
91 |
result = c.getTime();
|
|
|
92 |
} else {
|
|
|
93 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "', the format is not valid");
|
|
|
94 |
}
|
|
|
95 |
} else {
|
|
|
96 |
if (!o.getClass().isAssignableFrom(type)) {
|
|
|
97 |
throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'");
|
|
|
98 |
}
|
|
|
99 |
result = o;
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
return result;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public static Object getParameterFromJSON(final JSONObject json, final String key, final Class<?> type) {
|
|
|
107 |
return getParameterFromJSON(json, key, type, null);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public static Object getParameterFromJSON(final JSONObject json, final String key, final Class<?> type, Object defaultValue) {
|
|
|
111 |
Object o = defaultValue;
|
|
|
112 |
if (json.containsKey(key)) {
|
|
|
113 |
o = json.get(key);
|
|
|
114 |
if (o == null || o.equals("null")) {
|
|
|
115 |
o = null;
|
|
|
116 |
} else {
|
|
|
117 |
if (type == Integer.class) {
|
|
|
118 |
if (!o.getClass().isAssignableFrom(Long.class)) {
|
|
|
119 |
throw new IllegalArgumentException("value for '" + key + "' is invalid");
|
|
|
120 |
} else {
|
|
|
121 |
try {
|
|
|
122 |
o = NumberUtils.ensureInt((Long) o);
|
|
|
123 |
} catch (IllegalArgumentException ex) {
|
|
|
124 |
throw new IllegalArgumentException("value for '" + key + "' is invalid, " + ex.getMessage());
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
} else {
|
|
|
128 |
if (!o.getClass().isAssignableFrom(type)) {
|
|
|
129 |
throw new IllegalArgumentException("value for '" + key + "' is invalid");
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
return o;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// TODO move to another class
|
|
|
138 |
public static boolean listInstanceOf(Object object, Class<?> c) {
|
|
|
139 |
if (object instanceof List<?>) {
|
|
|
140 |
final List<?> list = (List<?>) object;
|
|
|
141 |
final int listSize = list.size();
|
|
|
142 |
for (int i = 0; i < listSize; i++) {
|
|
|
143 |
if (!c.isInstance(list.get(i))) {
|
|
|
144 |
return false;
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
} else {
|
|
|
148 |
return false;
|
|
|
149 |
}
|
|
|
150 |
return true;
|
|
|
151 |
}
|
|
|
152 |
}
|