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)
| 1764 | * @return The wrapped value |
| 1765 | */ |
| 1766 | static protected Object wrap(Object object) { |
| 1767 | try { |
| 1768 | if (object == null) { |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | if (object instanceof JSONObject || object instanceof JSONArray || |
| 1772 | NULL.equals(object) || /*object instanceof JSONString ||*/ |
| 1773 | object instanceof Byte || object instanceof Character || |
| 1774 | object instanceof Short || object instanceof Integer || |
| 1775 | object instanceof Long || object instanceof Boolean || |
| 1776 | object instanceof Float || object instanceof Double || |
| 1777 | object instanceof String) { |
| 1778 | return object; |
| 1779 | } |
| 1780 | |
| 1781 | if (object instanceof Collection) { |
| 1782 | return new JSONArray(object); |
| 1783 | } |
| 1784 | if (object.getClass().isArray()) { |
| 1785 | return new JSONArray(object); |
| 1786 | } |
| 1787 | if (object instanceof Map) { |
| 1788 | return new JSONObject(object); |
| 1789 | } |
| 1790 | Package objectPackage = object.getClass().getPackage(); |
| 1791 | String objectPackageName = objectPackage != null |
| 1792 | ? objectPackage.getName() |
| 1793 | : ""; |
| 1794 | if ( |
| 1795 | objectPackageName.startsWith("java.") || |
| 1796 | objectPackageName.startsWith("javax.") || |
| 1797 | object.getClass().getClassLoader() == null |
| 1798 | ) { |
| 1799 | return object.toString(); |
| 1800 | } |
| 1801 | return new JSONObject(object); |
| 1802 | } catch(Exception exception) { |
| 1803 | return null; |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | // /** |
no test coverage detected