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)
| 1710 | * @return The wrapped value |
| 1711 | */ |
| 1712 | static protected Object wrap(Object object) { |
| 1713 | try { |
| 1714 | if (object == null) { |
| 1715 | return NULL; |
| 1716 | } |
| 1717 | if (object instanceof JSONObject || object instanceof JSONArray || |
| 1718 | NULL.equals(object) || /*object instanceof JSONString ||*/ |
| 1719 | object instanceof Byte || object instanceof Character || |
| 1720 | object instanceof Short || object instanceof Integer || |
| 1721 | object instanceof Long || object instanceof Boolean || |
| 1722 | object instanceof Float || object instanceof Double || |
| 1723 | object instanceof String) { |
| 1724 | return object; |
| 1725 | } |
| 1726 | |
| 1727 | if (object instanceof Collection) { |
| 1728 | return new JSONArray(object); |
| 1729 | } |
| 1730 | if (object.getClass().isArray()) { |
| 1731 | return new JSONArray(object); |
| 1732 | } |
| 1733 | if (object instanceof Map) { |
| 1734 | return new JSONObject(object); |
| 1735 | } |
| 1736 | Package objectPackage = object.getClass().getPackage(); |
| 1737 | String objectPackageName = objectPackage != null |
| 1738 | ? objectPackage.getName() |
| 1739 | : ""; |
| 1740 | if ( |
| 1741 | objectPackageName.startsWith("java.") || |
| 1742 | objectPackageName.startsWith("javax.") || |
| 1743 | object.getClass().getClassLoader() == null |
| 1744 | ) { |
| 1745 | return object.toString(); |
| 1746 | } |
| 1747 | return new JSONObject(object); |
| 1748 | } catch(Exception exception) { |
| 1749 | return null; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | |
| 1754 | // /** |
no test coverage detected