/** * Gets a map property deserialized values from the cache or from the underlying JSONObject * * @param property the property name * @param typeClass the type of JSONResponse contained in the property * @return the map of json response style objects associated to this property in the JSON response */ @SuppressWarnings("unchecked") private Map<String, ?> getMap(String property, Class<?> typeClass) { Map<String, Object> map = null; if (!propertyMap.containsKey(property)) { JSONObject jsonMap = delegate.optJSONObject(property); if (jsonMap != null) { map = new LinkedHashMap<String, Object>(jsonMap.length()); while (jsonMap.keys().hasNext()) { String key = (String) jsonMap.keys().next(); JSONObject jsonObject = jsonMap.optJSONObject(key); try { Object response = typeClass.newInstance(); ((JSONResponse) response).fromJSON(jsonObject); map.put(key, response); } catch (JSONException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } propertyMap.put(property, map); } } else { map = (Map<String, Object>) propertyMap.get(property); } return map; }
/** * Gets a list property deserialized values from the cache or from the underlying JSONObject * * @param property the property name * @param typeClass the type of JSONResponse contained in the array * @return the list of objects associated to this property in the JSON response */ @SuppressWarnings("unchecked") private List<?> getList(String property, Class<?> typeClass) { List<Object> list = null; if (!propertyMap.containsKey(property)) { JSONArray array = delegate.optJSONArray(property); if (array != null) { list = new ArrayList<Object>(array.length()); for (int i = 0; i < array.length(); i++) { JSONObject jsonObject = array.optJSONObject(i); try { Object response = typeClass.newInstance(); ((JSONResponse) response).fromJSON(jsonObject); list.add(response); } catch (JSONException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } propertyMap.put(property, list); } } else { list = (List<Object>) propertyMap.get(property); } return list; }
/** * Gets a property deserialized value from the cache or from the underlying JSONObject * * @param property the property name * @param typeClass the type of JSONResponse contained in the property */ @SuppressWarnings("unchecked") private Object getObject(String property, Class<?> typeClass) { Object object = null; if (!propertyMap.containsKey(property)) { JSONObject jsonObject = delegate.optJSONObject(property); if (jsonObject != null) { try { object = typeClass.newInstance(); ((JSONResponse) object).fromJSON(jsonObject); } catch (JSONException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } propertyMap.put(property, object); } else { object = propertyMap.get(property); } return object; }