@Test public void testAddToJson() throws Exception{ JSONObject object = JsonTestUtil.parse("{}"); JsonService.addToJson(object, "number", new JSONNumber(123)); Assert.assertEquals(123.0, object.get("number").isNumber().doubleValue(), 0.0001); }
public static void addToJson(JSONObject jsonObject, String key, Object value) throws JSONException { if (jsonObject == null) { throw new JSONException("Can't add key '" + key + "' to a null object."); } if (key == null) { throw new JSONException("Can't add null key."); } JSONValue jsonValue = null; if (value != null) { if (value instanceof Date) { jsonValue = new JSONString(JsonService.format((Date) value)); } else if (value instanceof JSONValue) { jsonValue = (JSONValue) value; } else { jsonValue = new JSONString(value.toString()); } } jsonObject.put(key, jsonValue); }
JsonService { public static void addToJson(JSONObject jsonObject, String key, Object value) throws JSONException { if (jsonObject == null) { throw new JSONException("Can't add key '" + key + "' to a null object."); } if (key == null) { throw new JSONException("Can't add null key."); } JSONValue jsonValue = null; if (value != null) { if (value instanceof Date) { jsonValue = new JSONString(JsonService.format((Date) value)); } else if (value instanceof JSONValue) { jsonValue = (JSONValue) value; } else { jsonValue = new JSONString(value.toString()); } } jsonObject.put(key, jsonValue); } }
JsonService { public static void addToJson(JSONObject jsonObject, String key, Object value) throws JSONException { if (jsonObject == null) { throw new JSONException("Can't add key '" + key + "' to a null object."); } if (key == null) { throw new JSONException("Can't add null key."); } JSONValue jsonValue = null; if (value != null) { if (value instanceof Date) { jsonValue = new JSONString(JsonService.format((Date) value)); } else if (value instanceof JSONValue) { jsonValue = (JSONValue) value; } else { jsonValue = new JSONString(value.toString()); } } jsonObject.put(key, jsonValue); } private  JsonService(); }
JsonService { public static void addToJson(JSONObject jsonObject, String key, Object value) throws JSONException { if (jsonObject == null) { throw new JSONException("Can't add key '" + key + "' to a null object."); } if (key == null) { throw new JSONException("Can't add null key."); } JSONValue jsonValue = null; if (value != null) { if (value instanceof Date) { jsonValue = new JSONString(JsonService.format((Date) value)); } else if (value instanceof JSONValue) { jsonValue = (JSONValue) value; } else { jsonValue = new JSONString(value.toString()); } } jsonObject.put(key, jsonValue); } private  JsonService(); static String format(Date date); static JavaScriptObject eval(String jsonString); static JSONValue parse(String jsonString); static JSONObject getChild(JSONObject jsonObject, String key); static JSONArray getChildArray(JSONObject jsonObject, String key); static String getStringValue(JSONObject jsonObject, String key); static Character getCharValue(JSONObject jsonObject, String key); static Date getDateValue(JSONObject jsonObject, String key); static Geometry getGeometryValue(JSONObject jsonObject, String key); static Integer getIntValue(JSONObject jsonObject, String key); static Double getDoubleValue(JSONObject jsonObject, String key); static Long getLongValue(JSONObject jsonObject, String key); static Short getShortValue(JSONObject jsonObject, String key); static void addToJson(JSONObject jsonObject, String key, Object value); static boolean isNullObject(JSONObject jsonObject, String key); static Boolean getBooleanValue(JSONObject jsonObject, String key); }
JsonService { public static void addToJson(JSONObject jsonObject, String key, Object value) throws JSONException { if (jsonObject == null) { throw new JSONException("Can't add key '" + key + "' to a null object."); } if (key == null) { throw new JSONException("Can't add null key."); } JSONValue jsonValue = null; if (value != null) { if (value instanceof Date) { jsonValue = new JSONString(JsonService.format((Date) value)); } else if (value instanceof JSONValue) { jsonValue = (JSONValue) value; } else { jsonValue = new JSONString(value.toString()); } } jsonObject.put(key, jsonValue); } private  JsonService(); static String format(Date date); static JavaScriptObject eval(String jsonString); static JSONValue parse(String jsonString); static JSONObject getChild(JSONObject jsonObject, String key); static JSONArray getChildArray(JSONObject jsonObject, String key); static String getStringValue(JSONObject jsonObject, String key); static Character getCharValue(JSONObject jsonObject, String key); static Date getDateValue(JSONObject jsonObject, String key); static Geometry getGeometryValue(JSONObject jsonObject, String key); static Integer getIntValue(JSONObject jsonObject, String key); static Double getDoubleValue(JSONObject jsonObject, String key); static Long getLongValue(JSONObject jsonObject, String key); static Short getShortValue(JSONObject jsonObject, String key); static void addToJson(JSONObject jsonObject, String key, Object value); static boolean isNullObject(JSONObject jsonObject, String key); static Boolean getBooleanValue(JSONObject jsonObject, String key);  }