/** * Access a {@link JSONObject} value. * * @param key The key to access the value for. * @return Returns {@code null} if there is no such key or if it is not a {@link JSONObject}. */ public JSONObject getJSONObject(String key) { return getJSONObject(key, null); }
@Test public void testGetJsonObjectKeyExist() throws Exception { final Map<String, String> value = new HashMap<>(); value.put("key", "value"); final Map<String, Object> params = new HashMap<>(); params.put("key", value); final JSONObject json = new JSONObject(value); ParseConfig config = new ParseConfig(params); assertEquals(json, config.getJSONObject("key"), JSONCompareMode.NON_EXTENSIBLE); assertEquals(json, config.getJSONObject("key", new JSONObject()), JSONCompareMode.NON_EXTENSIBLE); }
@Test public void testGetJsonObjectKeyExistValueNull() throws Exception { final JSONObject json = new JSONObject(); json.put("key", "value"); final Map<String, Object> params = new HashMap<>(); params.put("key", JSONObject.NULL); params.put("keyAgain", null); ParseConfig config = new ParseConfig(params); assertNull(config.getJSONObject("key")); assertNull(config.getJSONObject("key", json)); assertNull(config.getJSONObject("keyAgain")); assertNull(config.getJSONObject("keyAgain", json)); }
@Test public void testGetJsonObjectKeyExistValueNotJsonObject() throws Exception { final JSONObject json = new JSONObject(); json.put("key", "value"); final Map<String, Object> params = new HashMap<>(); params.put("key", 1); ParseConfig config = new ParseConfig(params); assertNull(config.getJSONObject("key")); //TODO(mengyan) ParseConfig.getJSONObject should return json, but due to the error in // ParseConfig.getJsonObject, this returns null right now. Revise when we correct the logic // for ParseConfig.getJsonObject. assertNull(config.getJSONObject("key", json)); }
@Test public void testGetJsonObjectKeyNotExist() throws Exception { final JSONObject json = new JSONObject(); json.put("key", "value"); final JSONObject jsonAgain = new JSONObject(); jsonAgain.put("keyAgain", "valueAgain"); final Map<String, Object> params; params = new HashMap<>(); params.put("key", json); ParseConfig config = new ParseConfig(params); assertNull(config.getJSONObject("wrongKey")); //TODO(mengyan) ParseConfig.getJSONObject should return jsonAgain, but due to the error in // ParseConfig.getJsonObject, this returns null right now. Revise when we correct the logic // for ParseConfig.getJsonObject. assertNull(config.getJSONObject("wrongKey", jsonAgain)); }