Wrap an object, if necessary. If the object is null, return the NULL object. If it is an array or collection, wrap it in a JSONArray. If it is a map, wrap it in a JSONObject. If it is a standard property (Double, String, et al) then it is already wrapped. Otherwise, if it comes from one of the java
(Object object)
| 1553 | * @return The wrapped value |
| 1554 | */ |
| 1555 | public static Object wrap(Object object) { |
| 1556 | try { |
| 1557 | if (object == null) { |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | if (object instanceof JSONObject || object instanceof JSONArray || |
| 1561 | NULL.equals(object) || object instanceof JSONString || |
| 1562 | object instanceof Byte || object instanceof Character || |
| 1563 | object instanceof Short || object instanceof Integer || |
| 1564 | object instanceof Long || object instanceof Boolean || |
| 1565 | object instanceof Float || object instanceof Double || |
| 1566 | object instanceof String) { |
| 1567 | return object; |
| 1568 | } |
| 1569 | |
| 1570 | if (object instanceof Collection) { |
| 1571 | return new JSONArray((Collection)object); |
| 1572 | } |
| 1573 | if (object.getClass().isArray()) { |
| 1574 | return new JSONArray(object); |
| 1575 | } |
| 1576 | if (object instanceof Map) { |
| 1577 | return new JSONObject((Map)object); |
| 1578 | } |
| 1579 | Package objectPackage = object.getClass().getPackage(); |
| 1580 | String objectPackageName = ( objectPackage != null ? objectPackage.getName() : "" ); |
| 1581 | if (objectPackageName.startsWith("java.") || |
| 1582 | objectPackageName.startsWith("javax.") || |
| 1583 | object.getClass().getClassLoader() == null) { |
| 1584 | return object.toString(); |
| 1585 | } |
| 1586 | return new JSONObject(object); |
| 1587 | } catch(Exception exception) { |
| 1588 | return null; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | |
| 1593 | /** |
no test coverage detected