/**
   * 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));
      }
    }
  }
 /**
  * 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;
 }
  /**
   * Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace
   * is added.
   *
   * <p>Warning: This method assumes that the data structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean commanate = false;
      Iterator keys = this.keys();
      writer.write('{');

      while (keys.hasNext()) {
        if (commanate) {
          writer.write(',');
        }
        Object key = keys.next();
        writer.write(quote(key.toString()));
        writer.write(':');
        Object value = this.map.get(key);
        if (value instanceof JSONObject) {
          ((JSONObject) value).write(writer);
        } else if (value instanceof JSONArray) {
          ((JSONArray) value).write(writer);
        } else {
          writer.write(valueToString(value));
        }
        commanate = true;
      }
      writer.write('}');
      return writer;
    } catch (IOException exception) {
      throw new JSONException(exception);
    }
  }
 /**
  * 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 {
       putOnce(names[i], jo.opt(names[i]));
     } catch (Exception ignore) {
     }
   }
 }