/** * Convert a JSON value to a configuration value. */ private static Object jsonToConfig(Object json) { if (JSONObject.NULL.equals(json)) { return null; } else if (json instanceof CharSequence) { return json.toString(); } else if (json instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json; ImmutableList.Builder<Object> values = ImmutableList.builder(); for (int i = 0, j = jsonArray.length(); i < j; ++i) { values.add(jsonToConfig(jsonArray.opt(i))); } return values.build(); } else if (json instanceof JSONObject) { return jsonToMap((JSONObject) json); } // A (boxed) primitive. return json; }
/** * Convert a JSON value to a configuration value. */ private static Object jsonToConfig(Object json) { if (JSONObject.NULL.equals(json)) { return null; } else if (json instanceof CharSequence) { return json.toString(); } else if (json instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json; ImmutableList.Builder<Object> values = ImmutableList.builder(); for (int i = 0, j = jsonArray.length(); i < j; ++i) { values.add(jsonToConfig(jsonArray.opt(i))); } return values.build(); } else if (json instanceof JSONObject) { return jsonToMap((JSONObject) json); } // A (boxed) primitive. return json; }
/** * Convert a JSON value to a configuration value. */ private static Object jsonToConfig(Object json) { if (JSONObject.NULL.equals(json)) { return null; } else if (json instanceof CharSequence) { return json.toString(); } else if (json instanceof JSONArray) { JSONArray jsonArray = (JSONArray) json; ImmutableList.Builder<Object> values = ImmutableList.builder(); for (int i = 0, j = jsonArray.length(); i < j; ++i) { values.add(jsonToConfig(jsonArray.opt(i))); } return values.build(); } else if (json instanceof JSONObject) { return jsonToMap((JSONObject) json); } // A (boxed) primitive. return json; }
private static Map<String, Object> jsonToMap(JSONObject json) { String[] keys = JSONObject.getNames(json); if (keys == null) { return ImmutableMap.of(); } Map<String, Object> values = new HashMap<String, Object>(json.length(), 1); for (String key : keys) { Object val = jsonToConfig(json.opt(key)); //If this is a string see if its a pointer to an external resource, and if so, load the resource if (val instanceof String) { String stringVal = (String) val; if (stringVal.startsWith(ResourceLoader.RESOURCE_PREFIX) || stringVal.startsWith(ResourceLoader.FILE_PREFIX)) { try { val = IOUtils.toString(ResourceLoader.open(stringVal), "UTF-8"); } catch (IOException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.logp(Level.WARNING, classname, "jsonToMap", MessageKeys.READING_CONFIG, e); } } } } values.put(key, val); } return Collections.unmodifiableMap(values); }
private static Map<String, Object> jsonToMap(JSONObject json) { String[] keys = JSONObject.getNames(json); if (keys == null) { return ImmutableMap.of(); } Map<String, Object> values = new HashMap<String, Object>(json.length(), 1); for (String key : keys) { Object val = jsonToConfig(json.opt(key)); //If this is a string see if its a pointer to an external resource, and if so, load the resource if (val instanceof String) { String stringVal = (String) val; if (stringVal.startsWith(ResourceLoader.RESOURCE_PREFIX) || stringVal.startsWith(ResourceLoader.FILE_PREFIX)) { try { val = IOUtils.toString(ResourceLoader.open(stringVal), "UTF-8"); } catch (IOException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.logp(Level.WARNING, classname, "jsonToMap", MessageKeys.READING_CONFIG, e); } } } } values.put(key, val); } return Collections.unmodifiableMap(values); }
private static Map<String, Object> jsonToMap(JSONObject json) { String[] keys = JSONObject.getNames(json); if (keys == null) { return ImmutableMap.of(); } Map<String, Object> values = new HashMap<String, Object>(json.length(), 1); for (String key : keys) { Object val = jsonToConfig(json.opt(key)); //If this is a string see if its a pointer to an external resource, and if so, load the resource if (val instanceof String) { String stringVal = (String) val; if (stringVal.startsWith(ResourceLoader.RESOURCE_PREFIX) || stringVal.startsWith(ResourceLoader.FILE_PREFIX)) { try { val = IOUtils.toString(ResourceLoader.open(stringVal), "UTF-8"); } catch (IOException e) { if (LOG.isLoggable(Level.WARNING)) { LOG.logp(Level.WARNING, classname, "jsonToMap", MessageKeys.READING_CONFIG, e); } } } } values.put(key, val); } return Collections.unmodifiableMap(values); }