JSONObject extends JSONElement { public static Object wrap(Object obj) { if(obj == null || obj.equals(NULL)){ return NULL; } if(isPrimitive(obj)){ return obj; } if(obj instanceof Map){ return new JSONObject((Map)obj); } if(obj instanceof Collection){ return new JSONArray((Collection)obj); } if(obj.getClass().isArray()){ return wrapArray(obj); } return new JSONObject(); } JSONObject(String string);  JSONObject(Map<String, Object> map);  JSONObject();   JSONObject(JsonElement jsonElement); static String quote(String s); static Writer quote(String s, Writer writer); static Object stringToValue(String str); static Object wrap(Object obj); static String doubleToString(double d); static String numberToString(Number number); static String valueToString(Object o); static String[] getNames(JSONObject jsonObject); static String[] getNames(Object o); @Override String toString(); String toString(int i); boolean similar(Object o); boolean has(String key); int length(); Object get(String key); JSONObject getJSONObject(String key); JSONObject optJSONObject(String key); JSONArray getJSONArray(String key); JSONArray optJSONArray(String key); String getString(String key); String optString(String key); String optString(String key, String defaultValue); double getDouble(String key); double optDouble(String key); double optDouble(String key, double defaultValue); float getFloat(String key); float optFloat(String key); float optFloat(String key, float defaultValue); long getLong(String key); long optLong(String key); long optLong(String key, long defaultValue); Number getNumber(String key); Number optNumber(String key); Number optNumber(String key, Number defaultValue); int getInt(String key); int optInt(String key); int optInt(String key, int defaultValue); BigInteger getBigInteger(String key); BigInteger optBigInteger(String key, BigInteger defaultValue); BigDecimal getBigDecimal(String key); BigDecimal optBigDecimal(String key, BigDecimal defaultValue); boolean getBoolean(String key); boolean optBoolean(String key); boolean optBoolean(String key, boolean defaultValue); T getEnum(Class<T> enumClass, String key); T optEnum(Class<T> enumClass, String key); T optEnum(Class<T> enumClass, String key, T defaultValue); JSONObject put(String key, JSONObject object); JSONObject put(String key, JSONArray array); JSONObject put(String key, boolean value); JSONObject put(String key, Number value); JSONObject put(String key, double value); JSONObject put(String key, float value); JSONObject put(String key, long value); JSONObject put(String key, int value); JSONObject put(String key, String value); JSONObject put(String key, Collection value); JSONObject put(String key, Map value); JSONObject put(String key, T enumvalue); Object remove(String key); JSONObject accumulate(String key, Object additionalValue); JSONObject append(String key, Object value); JSONObject increment(String key); JSONObject putOnce(String key, Object value); JSONObject put(String key, Object value); JSONObject putOpt(String key, Object value); Set<String> keySet(); Iterator<String> keys(); Map<String, Object> toMap(); JSONArray names(); JSONArray toJSONArray(JSONArray names); @Override boolean equals(Object other); @Override int hashCode(); Object opt(String key); boolean isEmpty(); boolean isNull(String key); static final Object NULL; }
@Test public void wrapPrimitives() throws IOException { assertEquals(42, JSONObject.wrap(42)); assertEquals(42.5, JSONObject.wrap(42.5)); assertSame(JSONObject.NULL, JSONObject.wrap(null)); assertEquals(true, JSONObject.wrap(true)); }