JSONObject extends JSONElement { public static String valueToString(Object o) throws JSONException { if(o == null){ return "null"; } if(o instanceof JSONString){ return ((JSONString)o).toJSONString(); } if(o instanceof JSONElement){ return o.toString(); } return new Gson().toJson(o); } 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 valueToString() { assertEquals("null", JSONObject.valueToString(null)); assertEquals("42", JSONObject.valueToString(42)); assertEquals("42.5643", JSONObject.valueToString(42.5643f)); assertEquals("\"Hello World\"", JSONObject.valueToString("Hello World")); assertEquals(ImmaJson.HI_MOM, JSONObject.valueToString(new ImmaJson())); assertEquals("{\"bar\":\"me\"}", JSONObject.valueToString(new Foo("me"))); assertEquals("{}", JSONObject.valueToString(new JSONObject())); assertEquals("[]", JSONObject.valueToString(new JSONArray())); }