Exemple #1
0
  /**
   * Construct a JSONObject from a ResourceBundle.
   *
   * @param baseName The ResourceBundle base name.
   * @param locale The Locale to load the ResourceBundle for.
   * @throws JSONException If any JSONExceptions are detected.
   */
  public JSONObject(String baseName, Locale locale) throws JSONException {
    this();
    ResourceBundle bundle =
        ResourceBundle.getBundle(baseName, locale, Thread.currentThread().getContextClassLoader());

    // Iterate through the keys in the bundle.

    Enumeration keys = bundle.getKeys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      if (key instanceof String) {

        // Go through the path, ensuring that there is a nested JSONObject for each
        // segment except the last. Add the value using the last segment's name into
        // the deepest nested JSONObject.

        String[] path = ((String) key).split("\\.");
        int last = path.length - 1;
        JSONObject target = this;
        for (int i = 0; i < last; i += 1) {
          String segment = path[i];
          JSONObject nextTarget = target.optJSONObject(segment);
          if (nextTarget == null) {
            nextTarget = new JSONObject();
            target.put(segment, nextTarget);
          }
          target = nextTarget;
        }
        target.put(path[last], bundle.getString((String) key));
      }
    }
  }
Exemple #2
0
 /**
  * Get an array of field names from a JSONObject.
  *
  * @return An array of field names, or null if there are no names.
  */
 public static String[] getNames(JSONObject jo) {
   int length = jo.length();
   if (length == 0) {
     return null;
   }
   Iterator iterator = jo.keys();
   String[] names = new String[length];
   int i = 0;
   while (iterator.hasNext()) {
     names[i] = (String) iterator.next();
     i += 1;
   }
   return names;
 }
Exemple #3
0
 static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent)
     throws JSONException, IOException {
   if (value == null || value.equals(null)) {
     writer.write("null");
   } else if (value instanceof JSONObject) {
     ((JSONObject) value).write(writer, indentFactor, indent);
   } else if (value instanceof JSONArray) {
     ((JSONArray) value).write(writer, indentFactor, indent);
   } else if (value instanceof Map) {
     new JSONObject((Map) value).write(writer, indentFactor, indent);
   } else if (value instanceof Collection) {
     new JSONArray((Collection) value).write(writer, indentFactor, indent);
   } else if (value.getClass().isArray()) {
     new JSONArray(value).write(writer, indentFactor, indent);
   } else if (value instanceof Number) {
     writer.write(numberToString((Number) value));
   } else if (value instanceof Boolean) {
     writer.write(value.toString());
   } else if (value instanceof JSONString) {
     Object o;
     try {
       o = ((JSONString) value).toJSONString();
     } catch (Exception e) {
       throw new JSONException(e);
     }
     writer.write(o != null ? o.toString() : quote(value.toString()));
   } else {
     quote(value.toString(), writer);
   }
   return writer;
 }
Exemple #4
0
 /**
  * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to
  * identify the keys that should be copied. Missing keys are ignored.
  *
  * @param jo A JSONObject.
  * @param names An array of strings.
  * @throws JSONException
  * @exception JSONException If a value is a non-finite number or if a name is duplicated.
  */
 public JSONObject(JSONObject jo, String[] names) {
   this();
   for (int i = 0; i < names.length; i += 1) {
     try {
       this.putOnce(names[i], jo.opt(names[i]));
     } catch (Exception ignore) {
     }
   }
 }