/** * Access a {@link Map} value. * * @param key The key to access the value for. * @return {@code null} if there is no such key or if it cannot be converted to a * {@link Map}. */ public <V> Map<String, V> getMap(String key) { return getMap(key, null); }
/** * Access a {@link JSONObject} value, returning a default value if it doesn't exist. * * @param key The key to access the value for. * @param defaultValue The value to return if the key is not present or has the wrong type. * @return The default value if there is no such key or if it is not a {@link JSONObject}. */ public JSONObject getJSONObject(String key, JSONObject defaultValue) { Map<String, Object> map = getMap(key); Object encoded = (map != null) ? PointerEncoder.get().encode(map) : null; //TODO(mengyan) There are actually two cases, getList(key) will return null // case 1: key not exist, in this situation, we should return JSONArray defaultValue // case 2: key exist but value is Json.NULL, in this situation, we should return null // The following line we only cover case 2. We can not revise it since it may break some // existing app, but we should do it someday. return (encoded == null || encoded instanceof JSONObject) ? (JSONObject) encoded : defaultValue; }
@Test public void testGetMapKeyExistValueNotMap() { Map<String, Object> map = new HashMap<>(); map.put("firstAgain", "fooAgain"); map.put("secondAgain", "barAgain"); map.put("thirdAgain", "bazAgain"); final Map<String, Object> params = new HashMap<>(); params.put("key", 1); ParseConfig config = new ParseConfig(params); assertNull(config.getMap("key")); assertSame(map, config.getMap("key", map)); }
@Test public void testGetMapKeyExistValueNull() { Map<String, Object> map = new HashMap<>(); map.put("firstAgain", "fooAgain"); map.put("secondAgain", "barAgain"); map.put("thirdAgain", "bazAgain"); final Map<String, Object> params = new HashMap<>(); params.put("key", JSONObject.NULL); params.put("keyAgain", null); ParseConfig config = new ParseConfig(params); assertNull(config.getMap("key")); assertNull(config.getMap("key", map)); assertNull(config.getMap("keyAgain")); assertNull(config.getMap("keyAgain", map)); }
@Test public void testGetMapKeyNotExist() { final Map<String, Object> map = new HashMap<>(); map.put("first", "foo"); map.put("second", "bar"); map.put("third", "baz"); Map<String, Object> mapAgain = new HashMap<>(); mapAgain.put("firstAgain", "fooAgain"); mapAgain.put("secondAgain", "barAgain"); mapAgain.put("thirdAgain", "bazAgain"); final Map<String, Object> params = new HashMap<>(); params.put("key", map); ParseConfig config = new ParseConfig(params); assertNull(config.getMap("wrongKey")); assertSame(mapAgain, config.getMap("wrongKey", mapAgain)); }
@Test public void testGetMapKeyExist() { final Map<String, Object> map = new HashMap<>(); map.put("first", "foo"); map.put("second", "bar"); map.put("third", "baz"); Map<String, Object> mapAgain = new HashMap<>(); mapAgain.put("firstAgain", "fooAgain"); mapAgain.put("secondAgain", "barAgain"); mapAgain.put("thirdAgain", "bazAgain"); final Map<String, Object> params = new HashMap<>(); params.put("key", map); ParseConfig config = new ParseConfig(params); Map<String, Object> mapConfig = config.getMap("key"); assertEquals(3, mapConfig.size()); assertEquals("foo", mapConfig.get("first")); assertEquals("bar", mapConfig.get("second")); assertEquals("baz", mapConfig.get("third")); assertSame(mapConfig, config.getMap("key", mapAgain)); }