55985
|
1 |
/*
|
55997
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
55985
|
24 |
*/
|
55997
|
25 |
|
55985
|
26 |
package com.oracle.jmx.remote.rest.mapper;
|
|
27 |
|
|
28 |
import com.oracle.jmx.remote.rest.json.JSONArray;
|
|
29 |
import com.oracle.jmx.remote.rest.json.JSONElement;
|
|
30 |
import com.oracle.jmx.remote.rest.json.JSONObject;
|
|
31 |
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
|
55994
|
32 |
|
|
33 |
import javax.management.MalformedObjectNameException;
|
|
34 |
import javax.management.ObjectName;
|
|
35 |
import javax.management.openmbean.*;
|
55985
|
36 |
import java.lang.reflect.Array;
|
|
37 |
import java.math.BigDecimal;
|
|
38 |
import java.math.BigInteger;
|
|
39 |
import java.text.DateFormat;
|
|
40 |
import java.text.ParseException;
|
|
41 |
import java.text.SimpleDateFormat;
|
55994
|
42 |
import java.util.*;
|
55985
|
43 |
|
|
44 |
/**
|
|
45 |
* @author harsha
|
|
46 |
*/
|
|
47 |
public final class JSONMappingFactory {
|
|
48 |
|
|
49 |
public static final JSONMappingFactory INSTANCE;
|
|
50 |
private static final Map<Class<?>, JSONMapper> typeMapper;
|
|
51 |
private static final Map<String, Class<?>> primitiveMap = new HashMap<>();
|
|
52 |
|
|
53 |
static {
|
|
54 |
// Make order of Initialization explicit
|
|
55 |
typeMapper = new HashMap<>();
|
|
56 |
|
|
57 |
primitiveMap.put("boolean", Boolean.TYPE);
|
|
58 |
primitiveMap.put("int", Integer.TYPE);
|
|
59 |
primitiveMap.put("long", Long.TYPE);
|
|
60 |
primitiveMap.put("double", Double.TYPE);
|
|
61 |
primitiveMap.put("float", Float.TYPE);
|
|
62 |
primitiveMap.put("bool", Boolean.TYPE);
|
|
63 |
primitiveMap.put("char", Character.TYPE);
|
|
64 |
primitiveMap.put("byte", Byte.TYPE);
|
|
65 |
primitiveMap.put("void", Void.TYPE);
|
|
66 |
primitiveMap.put("short", Short.TYPE);
|
|
67 |
|
|
68 |
INSTANCE = new JSONMappingFactory();
|
|
69 |
}
|
|
70 |
|
|
71 |
private JSONMappingFactory() {
|
|
72 |
|
56002
|
73 |
typeMapper.put(void.class, new VoidMapper());
|
|
74 |
typeMapper.put(Void.class, new VoidMapper());
|
55985
|
75 |
|
|
76 |
typeMapper.put(boolean.class, new BooleanMapper());
|
|
77 |
typeMapper.put(Boolean.class, new BooleanMapper());
|
|
78 |
|
|
79 |
typeMapper.put(byte.class, new ByteMapper());
|
|
80 |
typeMapper.put(Byte.class, new ByteMapper());
|
|
81 |
|
|
82 |
typeMapper.put(short.class, new ShortMapper());
|
|
83 |
typeMapper.put(Short.class, new ShortMapper());
|
|
84 |
|
|
85 |
typeMapper.put(int.class, new IntegerMapper());
|
|
86 |
typeMapper.put(Integer.class, new IntegerMapper());
|
|
87 |
|
|
88 |
typeMapper.put(long.class, new LongMapper());
|
|
89 |
typeMapper.put(Long.class, new LongMapper());
|
|
90 |
|
|
91 |
typeMapper.put(float.class, new FloatMapper());
|
|
92 |
typeMapper.put(Float.class, new FloatMapper());
|
|
93 |
|
|
94 |
typeMapper.put(double.class, new DoubleMapper());
|
|
95 |
typeMapper.put(Double.class, new DoubleMapper());
|
|
96 |
|
|
97 |
typeMapper.put(char.class, new CharacterMapper());
|
|
98 |
typeMapper.put(Character.class, new CharacterMapper());
|
|
99 |
|
|
100 |
typeMapper.put(String.class, new StringMapper());
|
|
101 |
typeMapper.put(BigInteger.class, new BigIntegerMapper());
|
|
102 |
typeMapper.put(BigDecimal.class, new BigDecimalMapper());
|
|
103 |
typeMapper.put(ObjectName.class, new ObjectNameMapper());
|
|
104 |
typeMapper.put(Date.class, new DateMapper());
|
|
105 |
}
|
|
106 |
|
|
107 |
private Class<?> getArrayComponent(Class<?> cls) {
|
|
108 |
if (cls == null) {
|
|
109 |
return cls;
|
|
110 |
}
|
|
111 |
Class<?> compType = cls;
|
|
112 |
// TODO: Add check for max array dimention of 15
|
|
113 |
while (compType.isArray()) {
|
|
114 |
compType = compType.getComponentType();
|
|
115 |
}
|
|
116 |
return compType;
|
|
117 |
}
|
|
118 |
|
|
119 |
private Object getArrayElement(Object arrayObj) {
|
|
120 |
if (arrayObj != null && arrayObj.getClass().isArray()) {
|
|
121 |
while (arrayObj.getClass().isArray()) {
|
|
122 |
Class<?> componentType = arrayObj.getClass().getComponentType();
|
|
123 |
if (Array.getLength(arrayObj) > 0) {
|
|
124 |
Object component = null;
|
|
125 |
for (int i = 0; i < Array.getLength(arrayObj); i++) {
|
|
126 |
component = Array.get(arrayObj, i);
|
|
127 |
if (component != null) {
|
|
128 |
break;
|
|
129 |
}
|
|
130 |
}
|
|
131 |
if (component == null) {
|
|
132 |
return null;
|
|
133 |
}
|
|
134 |
if (componentType.isPrimitive()) {
|
|
135 |
componentType = component.getClass();
|
|
136 |
}
|
|
137 |
arrayObj = componentType.cast(component);
|
|
138 |
} else {
|
|
139 |
return null;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|
|
143 |
return arrayObj;
|
|
144 |
}
|
|
145 |
|
|
146 |
// TODO: This should be access controlled. Define new permissions
|
|
147 |
public void addMapping(Class<?> cls, JSONMapper mapper) {
|
|
148 |
Class<?> input = cls;
|
|
149 |
if (cls.isArray()) {
|
|
150 |
input = getArrayComponent(cls);
|
|
151 |
}
|
|
152 |
if (!typeMapper.containsKey(input)) {
|
|
153 |
typeMapper.put(input, mapper);
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
public JSONMapper getTypeMapper(Object object) {
|
56002
|
158 |
return getTypeMapper(object, true);
|
|
159 |
}
|
|
160 |
|
|
161 |
private JSONMapper getTypeMapper(Object object, boolean deepTypeCheck) {
|
55994
|
162 |
if (object == null) return null;
|
55985
|
163 |
Object obj = object;
|
|
164 |
Class<?> cls = object.getClass();
|
|
165 |
if (cls.isArray()) {
|
|
166 |
Object arrayElement = getArrayElement(obj);
|
55994
|
167 |
if (arrayElement instanceof CompositeData) {
|
|
168 |
CompositeData cds = (CompositeData) arrayElement;
|
55985
|
169 |
return new OpenArrayTypeMapper(cls, cds.getCompositeType());
|
55994
|
170 |
} else if (arrayElement instanceof TabularData) {
|
|
171 |
TabularData tds = (TabularData) arrayElement;
|
55985
|
172 |
return new OpenArrayTypeMapper(cls, tds.getTabularType());
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
55994
|
176 |
if (object instanceof CompositeData) {
|
|
177 |
CompositeData cd = (CompositeData) object;
|
|
178 |
return getTypeMapper(cd.getCompositeType());
|
|
179 |
} else if (object instanceof TabularData) {
|
|
180 |
TabularData cds = (TabularData) object;
|
55985
|
181 |
return getTypeMapper(cds.getTabularType());
|
55994
|
182 |
} else if (object instanceof Collection<?>) {
|
56002
|
183 |
if (deepTypeCheck) {
|
|
184 |
Collection<?> c = (Collection<?>) object;
|
|
185 |
boolean unknownMapper = c.stream().anyMatch(k -> (k != null) && (getTypeMapper(k) == null));
|
|
186 |
if (unknownMapper)
|
|
187 |
return null;
|
|
188 |
}
|
|
189 |
return new CollectionMapper();
|
55994
|
190 |
} else if (object instanceof Map<?, ?>) {
|
56002
|
191 |
if (deepTypeCheck) {
|
|
192 |
Map<?, ?> map = (Map<?, ?>) object;
|
|
193 |
boolean unknownMapper = map.keySet().stream().
|
|
194 |
anyMatch(k -> ((k != null) && (getTypeMapper(k) == null)
|
|
195 |
|| (map.get(k) != null && getTypeMapper(map.get(k)) == null)));
|
|
196 |
if (unknownMapper)
|
|
197 |
return null;
|
|
198 |
}
|
|
199 |
return new MapMapper();
|
55985
|
200 |
} else {
|
|
201 |
return getTypeMapper(cls);
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
public JSONMapper getTypeMapper(Class<?> type) {
|
55994
|
206 |
if (type == null) return null;
|
55985
|
207 |
if (type.isArray()) {
|
|
208 |
Class<?> compType = getArrayComponent(type);
|
|
209 |
if (typeMapper.get(compType) != null) {
|
|
210 |
return new GenericArrayMapper(type);
|
|
211 |
} else {
|
|
212 |
return null;
|
|
213 |
}
|
|
214 |
}
|
|
215 |
return typeMapper.get(type);
|
|
216 |
}
|
|
217 |
|
|
218 |
public JSONMapper getTypeMapper(OpenType<?> type) {
|
|
219 |
if (type instanceof CompositeType) {
|
|
220 |
return new OpenCompositeTypeMapper((CompositeType) type);
|
|
221 |
} else if (type instanceof SimpleType) {
|
|
222 |
try {
|
|
223 |
return getTypeMapper(Class.forName(((SimpleType) type).getClassName()));
|
|
224 |
} catch (ClassNotFoundException ex) { // This should not happen as SimpleTypes are always loaded
|
|
225 |
throw new RuntimeException(ex);
|
|
226 |
}
|
|
227 |
} else if (type instanceof ArrayType) {
|
|
228 |
try {
|
|
229 |
ArrayType<?> at = (ArrayType) type;
|
|
230 |
Class<?> arrayClass = Class.forName(type.getClassName());
|
|
231 |
return new OpenArrayTypeMapper(arrayClass, at.getElementOpenType());
|
|
232 |
} catch (ClassNotFoundException ex) { // This should not happen as SimpleTypes are always loaded
|
|
233 |
throw new RuntimeException(ex);
|
|
234 |
}
|
|
235 |
} else if (type instanceof TabularType) {
|
55994
|
236 |
return new OpenTabularTypeMapper((TabularType) type);
|
55985
|
237 |
}
|
|
238 |
return null; //keep compiler happy
|
|
239 |
}
|
|
240 |
|
|
241 |
public boolean isTypeMapped(String type) throws ClassNotFoundException {
|
|
242 |
if (primitiveMap.get(type) != null) {
|
|
243 |
return true;
|
|
244 |
}
|
|
245 |
Class<?> inputCls = Class.forName(type);
|
|
246 |
inputCls = getArrayComponent(inputCls);
|
|
247 |
if (inputCls.equals(CompositeData.class) || inputCls.equals(TabularData.class)
|
|
248 |
|| inputCls.equals(CompositeDataSupport.class) || inputCls.equals(TabularDataSupport.class)) {
|
|
249 |
return true;
|
|
250 |
}
|
|
251 |
return JSONMappingFactory.INSTANCE.getTypeMapper(inputCls) != null;
|
|
252 |
}
|
|
253 |
|
|
254 |
private static class OpenArrayTypeMapper extends GenericArrayMapper {
|
|
255 |
|
55994
|
256 |
OpenArrayTypeMapper(Class<?> type, OpenType<?> elementOpenType) {
|
55985
|
257 |
super(type);
|
|
258 |
mapper = JSONMappingFactory.INSTANCE.getTypeMapper(elementOpenType);
|
|
259 |
}
|
|
260 |
}
|
|
261 |
|
55994
|
262 |
// This mapper array type for any java class
|
55985
|
263 |
private static class GenericArrayMapper implements JSONMapper {
|
|
264 |
|
|
265 |
private final Class<?> type;
|
|
266 |
protected JSONMapper mapper;
|
|
267 |
|
|
268 |
GenericArrayMapper(Class<?> type) {
|
|
269 |
this.type = type;
|
|
270 |
mapper = JSONMappingFactory.INSTANCE.getTypeMapper(JSONMappingFactory.INSTANCE.getArrayComponent(type));
|
|
271 |
}
|
|
272 |
|
|
273 |
private Object handleArrayType(Class<?> classType, JSONElement jsonValue) throws JSONDataException {
|
|
274 |
if (!(jsonValue instanceof JSONArray)) {
|
|
275 |
throw new JSONDataException("Invald JSON data format");
|
|
276 |
}
|
|
277 |
JSONArray jarr = (JSONArray) jsonValue;
|
|
278 |
Class<?> compType = classType.getComponentType();
|
|
279 |
Object resultArray = Array.newInstance(compType, jarr.size());
|
|
280 |
|
|
281 |
for (int i = 0; i < jarr.size(); i++) {
|
|
282 |
if (compType != null && compType.isArray()) {
|
|
283 |
Array.set(resultArray, i, handleArrayType(compType, jarr.get(i)));
|
|
284 |
} else {
|
|
285 |
Array.set(resultArray, i, mapper.toJavaObject(jarr.get(i)));
|
|
286 |
}
|
|
287 |
}
|
|
288 |
return resultArray;
|
|
289 |
}
|
|
290 |
|
|
291 |
@Override
|
|
292 |
public Object toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
293 |
return handleArrayType(type, jsonValue);
|
|
294 |
}
|
|
295 |
|
|
296 |
@Override
|
|
297 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
298 |
if (data == null) {
|
56002
|
299 |
return null;
|
55985
|
300 |
}
|
|
301 |
if (!data.getClass().equals(type)) {
|
|
302 |
throw new JSONMappingException("Illegal type : " + data.getClass());
|
|
303 |
}
|
|
304 |
return getJasonValue(data);
|
|
305 |
}
|
|
306 |
|
|
307 |
private JSONElement getJasonValue(Object data) throws JSONMappingException {
|
|
308 |
if (data == null) {
|
56002
|
309 |
return null;
|
55985
|
310 |
}
|
|
311 |
if (!data.getClass().isArray()) {
|
|
312 |
return mapper.toJsonValue(data);
|
|
313 |
} else {
|
|
314 |
JSONArray jArray = new JSONArray();
|
|
315 |
for (int i = 0; i < Array.getLength(data); i++) {
|
|
316 |
jArray.add(getJasonValue(Array.get(data, i)));
|
|
317 |
}
|
|
318 |
return jArray;
|
|
319 |
}
|
|
320 |
}
|
|
321 |
}
|
|
322 |
|
55994
|
323 |
/*
|
|
324 |
Mapper for compositeType. CompositeData cannot be mapped without it's associated
|
|
325 |
OpenType
|
|
326 |
*/
|
55985
|
327 |
private static class OpenCompositeTypeMapper implements JSONMapper {
|
|
328 |
|
|
329 |
private final CompositeType type;
|
|
330 |
|
|
331 |
OpenCompositeTypeMapper(CompositeType type) {
|
|
332 |
this.type = type;
|
|
333 |
}
|
|
334 |
|
|
335 |
@Override
|
|
336 |
public CompositeDataSupport toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
337 |
if (!(jsonValue instanceof JSONObject)) {
|
|
338 |
throw new JSONDataException("JSON String not an object");
|
|
339 |
}
|
|
340 |
|
|
341 |
JSONObject jObject = (JSONObject) jsonValue;
|
|
342 |
Map<String, Object> compositeDataMap = new HashMap<>();
|
|
343 |
for (String itemName : type.keySet()) {
|
|
344 |
OpenType<?> oType = type.getType(itemName);
|
|
345 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(oType);
|
|
346 |
compositeDataMap.put(itemName, typeMapper.toJavaObject(jObject.get(itemName)));
|
|
347 |
}
|
|
348 |
try {
|
|
349 |
return new CompositeDataSupport(type, compositeDataMap);
|
|
350 |
} catch (OpenDataException ex) {
|
|
351 |
throw new JSONDataException("Could not create CompositeData", ex);
|
|
352 |
}
|
|
353 |
}
|
|
354 |
|
|
355 |
@Override
|
|
356 |
public JSONElement toJsonValue(Object d) throws JSONMappingException {
|
55994
|
357 |
CompositeData data = (CompositeData) d;
|
55985
|
358 |
if (data == null) {
|
56002
|
359 |
return null;
|
55985
|
360 |
}
|
|
361 |
JSONObject jObject = new JSONObject();
|
|
362 |
for (String itemName : type.keySet()) {
|
|
363 |
OpenType<?> oType = type.getType(itemName);
|
|
364 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(oType);
|
|
365 |
if (typeMapper != null) {
|
|
366 |
jObject.put(itemName, typeMapper.toJsonValue(data.get(itemName)));
|
|
367 |
} else {
|
|
368 |
System.out.println("Unable to find mapper for : " + oType);
|
|
369 |
}
|
|
370 |
}
|
|
371 |
return jObject;
|
|
372 |
}
|
|
373 |
}
|
|
374 |
|
|
375 |
private static class OpenTabularTypeMapper implements JSONMapper {
|
|
376 |
|
|
377 |
private final TabularType type;
|
|
378 |
|
|
379 |
public OpenTabularTypeMapper(TabularType type) {
|
|
380 |
this.type = type;
|
|
381 |
}
|
|
382 |
|
|
383 |
/*
|
56002
|
384 |
Tabular data in JSON can follow below schema
|
|
385 |
{
|
|
386 |
"keys" : [<list of elements>],
|
|
387 |
"rows": [{ <CompositeData> }]
|
|
388 |
}
|
55985
|
389 |
*/
|
|
390 |
@Override
|
|
391 |
public TabularDataSupport toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
392 |
throw new UnsupportedOperationException();
|
|
393 |
}
|
|
394 |
|
|
395 |
@Override
|
|
396 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
55994
|
397 |
if (data == null) {
|
56002
|
398 |
return null;
|
55994
|
399 |
}
|
|
400 |
TabularDataSupport tds = (TabularDataSupport) data;
|
|
401 |
JSONArray jsonArray = new JSONArray();
|
|
402 |
for (Map.Entry<Object, Object> a : tds.entrySet()) {
|
|
403 |
CompositeData cds = (CompositeData) a.getValue();
|
|
404 |
JSONMapper cdsMapper = JSONMappingFactory.INSTANCE.getTypeMapper(cds);
|
56002
|
405 |
if (cdsMapper != null) {
|
55994
|
406 |
jsonArray.add(cdsMapper.toJsonValue(cds));
|
|
407 |
}
|
|
408 |
}
|
|
409 |
return jsonArray;
|
55985
|
410 |
}
|
|
411 |
}
|
|
412 |
|
|
413 |
private static class VoidMapper implements JSONMapper {
|
|
414 |
@Override
|
|
415 |
public Void toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
416 |
return null;
|
|
417 |
}
|
|
418 |
|
|
419 |
@Override
|
|
420 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
56002
|
421 |
return null;
|
55985
|
422 |
}
|
|
423 |
}
|
|
424 |
|
|
425 |
private static class BooleanMapper implements JSONMapper {
|
|
426 |
|
|
427 |
@Override
|
|
428 |
public Boolean toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
429 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Boolean) {
|
|
430 |
return (Boolean) ((JSONPrimitive) jsonValue).getValue();
|
|
431 |
} else {
|
|
432 |
throw new JSONDataException("Invalid type convertion - cannot convert "
|
56002
|
433 |
+ ((JSONPrimitive) jsonValue).getValue()
|
|
434 |
+ "(" + ((JSONPrimitive) jsonValue).getValue().getClass() + ")" + " to boolean");
|
55985
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
@Override
|
|
439 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
440 |
return new JSONPrimitive((Boolean) data);
|
|
441 |
}
|
|
442 |
}
|
|
443 |
|
|
444 |
private static class ByteMapper implements JSONMapper {
|
|
445 |
|
|
446 |
@Override
|
|
447 |
public Byte toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
448 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
|
|
449 |
return ((Long) ((JSONPrimitive) jsonValue).getValue()).byteValue();
|
|
450 |
} else {
|
|
451 |
throw new JSONDataException("Invalid type convertion - cannot convert "
|
|
452 |
+ (((JSONPrimitive) jsonValue).getValue().getClass()) + " to byte");
|
|
453 |
}
|
|
454 |
}
|
|
455 |
|
|
456 |
@Override
|
|
457 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
458 |
return new JSONPrimitive((Byte) data);
|
|
459 |
}
|
|
460 |
}
|
|
461 |
|
|
462 |
private static class ShortMapper implements JSONMapper {
|
|
463 |
|
|
464 |
@Override
|
|
465 |
public Short toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
466 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
|
|
467 |
return ((Long) ((JSONPrimitive) jsonValue).getValue()).shortValue();
|
|
468 |
} else {
|
|
469 |
throw new JSONDataException("Invalid JSON");
|
|
470 |
}
|
|
471 |
}
|
|
472 |
|
|
473 |
@Override
|
|
474 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
475 |
return new JSONPrimitive((Short) data);
|
|
476 |
}
|
|
477 |
}
|
|
478 |
|
|
479 |
private static class IntegerMapper implements JSONMapper {
|
|
480 |
|
|
481 |
@Override
|
|
482 |
public Integer toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
483 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
|
|
484 |
return ((Long) ((JSONPrimitive) jsonValue).getValue()).intValue();
|
|
485 |
} else {
|
|
486 |
throw new JSONDataException("Invalid JSON");
|
|
487 |
}
|
|
488 |
}
|
|
489 |
|
|
490 |
@Override
|
|
491 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
492 |
return new JSONPrimitive((Integer) data);
|
|
493 |
}
|
|
494 |
}
|
|
495 |
|
|
496 |
private static class LongMapper implements JSONMapper {
|
|
497 |
|
|
498 |
@Override
|
|
499 |
public Long toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
500 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
|
|
501 |
return (Long) ((JSONPrimitive) jsonValue).getValue();
|
|
502 |
} else {
|
|
503 |
throw new JSONDataException("Invalid JSON");
|
|
504 |
}
|
|
505 |
}
|
|
506 |
|
|
507 |
@Override
|
|
508 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
509 |
return new JSONPrimitive((Long) data);
|
|
510 |
}
|
|
511 |
}
|
|
512 |
|
|
513 |
private static class FloatMapper implements JSONMapper {
|
|
514 |
|
|
515 |
@Override
|
|
516 |
public Float toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
517 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Double) {
|
|
518 |
return ((Double) ((JSONPrimitive) jsonValue).getValue()).floatValue();
|
|
519 |
} else {
|
|
520 |
throw new JSONDataException("Invalid JSON");
|
|
521 |
}
|
|
522 |
}
|
|
523 |
|
|
524 |
@Override
|
|
525 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
526 |
return new JSONPrimitive((Float) data);
|
|
527 |
}
|
|
528 |
}
|
|
529 |
|
|
530 |
private static class DoubleMapper implements JSONMapper {
|
|
531 |
|
|
532 |
@Override
|
|
533 |
public Double toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
534 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
|
|
535 |
return (Double) ((JSONPrimitive) jsonValue).getValue();
|
|
536 |
} else {
|
|
537 |
throw new JSONDataException("Invalid JSON");
|
|
538 |
}
|
|
539 |
}
|
|
540 |
|
|
541 |
@Override
|
|
542 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
543 |
return new JSONPrimitive((Double) data);
|
|
544 |
}
|
|
545 |
}
|
|
546 |
|
|
547 |
private static class CharacterMapper implements JSONMapper {
|
|
548 |
|
|
549 |
@Override
|
|
550 |
public Character toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
551 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
|
|
552 |
String data = ((String) ((JSONPrimitive) jsonValue).getValue());
|
|
553 |
if (data.length() < 1) {
|
|
554 |
throw new JSONDataException("Invalid char");
|
|
555 |
}
|
|
556 |
return data.charAt(0);
|
|
557 |
} else {
|
|
558 |
throw new JSONDataException("Invalid JSON");
|
|
559 |
}
|
|
560 |
}
|
|
561 |
|
|
562 |
@Override
|
|
563 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
564 |
return new JSONPrimitive((Character) data);
|
|
565 |
}
|
|
566 |
}
|
|
567 |
|
|
568 |
private static class StringMapper implements JSONMapper {
|
|
569 |
|
|
570 |
@Override
|
|
571 |
public String toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
572 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
|
|
573 |
return (String) ((JSONPrimitive) jsonValue).getValue();
|
|
574 |
} else {
|
|
575 |
throw new JSONDataException("Invalid JSON");
|
|
576 |
}
|
|
577 |
}
|
|
578 |
|
|
579 |
@Override
|
|
580 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
581 |
return new JSONPrimitive((String) data);
|
|
582 |
}
|
|
583 |
}
|
|
584 |
|
|
585 |
private static class BigDecimalMapper implements JSONMapper {
|
|
586 |
|
|
587 |
@Override
|
|
588 |
public BigDecimal toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
589 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Double) {
|
|
590 |
return BigDecimal.valueOf((Double) ((JSONPrimitive) jsonValue).getValue());
|
|
591 |
} else {
|
|
592 |
throw new JSONDataException("Invalid JSON");
|
|
593 |
}
|
|
594 |
}
|
|
595 |
|
|
596 |
@Override
|
|
597 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
598 |
return new JSONPrimitive(((BigDecimal) data).doubleValue());
|
|
599 |
}
|
|
600 |
}
|
|
601 |
|
|
602 |
private static class BigIntegerMapper implements JSONMapper {
|
|
603 |
|
|
604 |
@Override
|
|
605 |
public BigInteger toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
606 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
|
|
607 |
return BigInteger.valueOf((Long) ((JSONPrimitive) jsonValue).getValue());
|
|
608 |
} else {
|
|
609 |
throw new JSONDataException("Invalid JSON");
|
|
610 |
}
|
|
611 |
}
|
|
612 |
|
|
613 |
@Override
|
|
614 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
615 |
return new JSONPrimitive(((BigInteger) data).longValue());
|
|
616 |
}
|
|
617 |
}
|
|
618 |
|
|
619 |
private static class ObjectNameMapper implements JSONMapper {
|
|
620 |
|
|
621 |
@Override
|
|
622 |
public ObjectName toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
623 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
|
|
624 |
try {
|
|
625 |
return new ObjectName((String) ((JSONPrimitive) jsonValue).getValue());
|
|
626 |
} catch (MalformedObjectNameException ex) {
|
|
627 |
throw new JSONDataException("Invalid Objectname");
|
|
628 |
}
|
|
629 |
} else {
|
|
630 |
throw new JSONDataException("Invalid JSON");
|
|
631 |
}
|
|
632 |
}
|
|
633 |
|
|
634 |
@Override
|
|
635 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
636 |
return new JSONPrimitive(data.toString());
|
|
637 |
}
|
|
638 |
}
|
|
639 |
|
|
640 |
private static class DateMapper implements JSONMapper {
|
|
641 |
|
|
642 |
private final DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
|
|
643 |
|
|
644 |
@Override
|
|
645 |
public Date toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
646 |
if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
|
|
647 |
String data = ((String) ((JSONPrimitive) jsonValue).getValue());
|
|
648 |
try {
|
|
649 |
return df.parse(data);
|
|
650 |
} catch (ParseException ex) {
|
|
651 |
throw new JSONDataException("Invalid Data " + data);
|
|
652 |
}
|
|
653 |
} else {
|
|
654 |
throw new JSONDataException("Invalid JSON");
|
|
655 |
}
|
|
656 |
}
|
|
657 |
|
|
658 |
@Override
|
|
659 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
660 |
return new JSONPrimitive(df.format((Date) data));
|
|
661 |
}
|
|
662 |
}
|
55994
|
663 |
|
|
664 |
private static final class MapMapper implements JSONMapper {
|
|
665 |
|
|
666 |
@Override
|
56002
|
667 |
public Map<String, Object> toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
55994
|
668 |
if (jsonValue instanceof JSONObject) {
|
|
669 |
JSONObject obj = (JSONObject) jsonValue;
|
|
670 |
Map<String, Object> result = new HashMap<>(obj.size());
|
|
671 |
for (String k : result.keySet()) {
|
|
672 |
JSONElement elem = obj.get(k);
|
|
673 |
if (elem instanceof JSONPrimitive) {
|
|
674 |
result.put(k, ((JSONPrimitive) elem).getValue());
|
|
675 |
} else {
|
|
676 |
JSONMapper mapper;
|
|
677 |
if (elem instanceof JSONObject) {
|
|
678 |
mapper = new MapMapper();
|
|
679 |
result.put(k, mapper.toJavaObject(elem));
|
|
680 |
} else if (elem instanceof JSONArray) {
|
|
681 |
mapper = new CollectionMapper();
|
|
682 |
result.put(k, mapper.toJavaObject(elem));
|
|
683 |
} else {
|
|
684 |
throw new JSONDataException("Unable to map : " + elem.getClass());
|
|
685 |
}
|
|
686 |
}
|
|
687 |
}
|
|
688 |
return result;
|
|
689 |
}
|
|
690 |
throw new JSONDataException("Inalid input");
|
|
691 |
}
|
|
692 |
|
|
693 |
@Override
|
|
694 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
695 |
if (data instanceof Map) {
|
|
696 |
JSONObject jobj = new JSONObject();
|
|
697 |
Map<?, ?> input = (Map<?, ?>) data;
|
|
698 |
for (Object k : input.keySet()) {
|
|
699 |
String key = k.toString();
|
|
700 |
final Object value = input.get(k);
|
|
701 |
if (value == null) {
|
56002
|
702 |
jobj.put(key, (JSONElement) null);
|
55994
|
703 |
} else {
|
56002
|
704 |
JSONMapper mapper = JSONMappingFactory.INSTANCE
|
|
705 |
.getTypeMapper(value,false); // Disable repeated type checking
|
55994
|
706 |
if (mapper == null) {
|
|
707 |
throw new JSONMappingException("Unable to map : " + value);
|
|
708 |
}
|
|
709 |
jobj.put(key, mapper.toJsonValue(value));
|
|
710 |
}
|
|
711 |
}
|
|
712 |
return jobj;
|
|
713 |
} else {
|
|
714 |
throw new JSONMappingException("Invalid Input");
|
|
715 |
}
|
|
716 |
}
|
|
717 |
}
|
|
718 |
|
|
719 |
private static final class CollectionMapper implements JSONMapper {
|
|
720 |
|
|
721 |
public CollectionMapper() {
|
|
722 |
}
|
|
723 |
|
|
724 |
@Override
|
|
725 |
@SuppressWarnings("unchecked")
|
|
726 |
public Object toJavaObject(JSONElement jsonValue) throws JSONDataException {
|
|
727 |
if (jsonValue instanceof JSONArray) {
|
|
728 |
JSONArray jarr = (JSONArray) jsonValue;
|
|
729 |
List<Object> result = new ArrayList<>(jarr.size());
|
|
730 |
for (JSONElement elem : jarr) {
|
|
731 |
if (elem instanceof JSONPrimitive) {
|
|
732 |
result.add(((JSONPrimitive) elem).getValue());
|
|
733 |
} else {
|
|
734 |
JSONMapper mapper;
|
|
735 |
if (elem instanceof JSONObject) {
|
|
736 |
mapper = new MapMapper();
|
|
737 |
result.add(mapper.toJavaObject(elem));
|
|
738 |
} else if (elem instanceof JSONArray) {
|
|
739 |
mapper = new CollectionMapper();
|
|
740 |
result.add(mapper.toJavaObject(elem));
|
|
741 |
} else {
|
|
742 |
throw new JSONDataException("Unable to map : " + elem.getClass());
|
|
743 |
}
|
|
744 |
}
|
|
745 |
}
|
|
746 |
return result;
|
|
747 |
}
|
|
748 |
throw new JSONDataException("Inalid input");
|
|
749 |
}
|
|
750 |
|
|
751 |
@Override
|
|
752 |
@SuppressWarnings("unchecked")
|
|
753 |
public JSONElement toJsonValue(Object data) throws JSONMappingException {
|
|
754 |
if (data instanceof Collection) {
|
|
755 |
JSONArray jarr = new JSONArray();
|
56002
|
756 |
Collection<?> c = (Collection<?>) data;
|
|
757 |
Iterator<?> itr = c.iterator();
|
55994
|
758 |
while (itr.hasNext()) {
|
|
759 |
Object next = itr.next();
|
56002
|
760 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.
|
|
761 |
getTypeMapper(next,false); // Disable repeated type checking for collection
|
55994
|
762 |
if (typeMapper == null) {
|
|
763 |
throw JSONMappingException.UNABLE_TO_MAP;
|
|
764 |
}
|
|
765 |
jarr.add(typeMapper.toJsonValue(next));
|
|
766 |
}
|
|
767 |
return jarr;
|
|
768 |
} else {
|
|
769 |
throw new JSONMappingException("Invalid Input");
|
|
770 |
}
|
|
771 |
}
|
|
772 |
}
|
55985
|
773 |
}
|