gt:       throw new NullPointerException("object is null");
context: /*
   ******************************************************************************
   * Copyright (c) 2013, 2015 EclipseSource.
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in all
   * copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   *****************************************************************************
   */
  package json;
  
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.Reader;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
  
  import json.JsonObject.Member;
  
  
  /**
   * Represents a JSON object, a set of name/value pairs, where the names are strings and the values
   * are JSON values.
   * <p>
   * Members can be added using the <code>add(String, ...)</code> methods which accept instances of
   * {@link JsonValue}, strings, primitive numbers, and boolean values. To modify certain values of an
   * object, use the <code>set(String, ...)</code> methods. Please note that the <code>add</code>
   * methods are faster than <code>set</code> as they do not search for existing members. On the other
   * hand, the <code>add</code> methods do not prevent adding multiple members with the same name.
   * Duplicate names are discouraged but not prohibited by JSON.
   * </p>
   * <p>
   * Members can be accessed by their name using {@link #get(String)}. A list of all names can be
   * obtained from the method {@link #names()}. This class also supports iterating over the members in
   * document order using an {@link #iterator()} or an enhanced for loop:
   * </p>
   * <pre>
   * for (Member member : jsonObject) {
   *   String name = member.getName();
   *   JsonValue value = member.getValue();
   *   ...
   * }
   * </pre>
   * <p>
   * Even though JSON objects are unordered by definition, instances of this class preserve the order
   * of members to allow processing in document order and to guarantee a predictable output.
   * </p>
   * <p>
   * Note that this class is <strong>not thread-safe</strong>. If multiple threads access a
   * <code>JsonObject</code> instance concurrently, while at least one of these threads modifies the
   * contents of this object, access to the instance must be synchronized externally. Failure to do so
   * may lead to an inconsistent state.
   * </p>
   * <p>
   * This class is <strong>not supposed to be extended</strong> by clients.
   * </p>
   */
  @SuppressWarnings("serial") // use default serial UID
  public class JsonObject extends JsonValue implements Iterable<Member> {
  
    private final List<String> names;
    private final List<JsonValue> values;
    private transient HashIndexTable table;
  
    /**
     * Creates a new empty JsonObject.
     */
    public JsonObject() {
      names = new ArrayList<String>();
      values = new ArrayList<JsonValue>();
      table = new HashIndexTable();
    }
  
    /**
     * Creates a new JsonObject, initialized with the contents of the specified JSON object.
     *
     * @param object
     *          the JSON object to get the initial contents from, must not be <code>null</code>
     */
    public JsonObject(JsonObject object) {
      this(object, false);
    }
  
    private JsonObject(JsonObject object, boolean unmodifiable) {
      if (object == null) {
        throw new NullPointerException("object is null");
      }
      if (unmodifiable) {
        names = Collections.unmodifiableList(object.names);
        values = Collections.unmodifiableList(object.values);
      } else {
        names = new ArrayList<String>(object.names);
        values = new ArrayList<JsonValue>(object.values);
      }
      table = new HashIndexTable();
      updateHashIndex();
    }
  
    /**
     * Reads a JSON object from the given reader.
     * <p>
     * Characters are read in chunks and buffered internally, therefore wrapping an existing reader in
     * an additional <code>BufferedReader</code> does <strong>not</strong> improve reading
     * performance.
     * </p>
     *
     * @param reader
     *          the reader to read the JSON object from
     * @return the JSON object that has been read
     * @throws IOException
     *           if an I/O error occurs in the reader
     * @throws ParseException
     *           if the input is not valid JSON
     * @throws UnsupportedOperationException
     *           if the input does not contain a JSON object
     * @deprecated Use {@link Json#parse(Reader)}{@link JsonValue#asObject() .asObject()} instead
     */
    @Deprecated
    public static JsonObject readFrom(Reader reader) throws IOException {
      return JsonValue.readFrom(reader).asObject();
    }
  
    /**
     * Reads a JSON object from the given string.
     *
     * @param string
     *          the string that contains the JSON object
     * @return the JSON object that has been read
     * @throws ParseException
     *           if the input is not valid JSON
     * @throws UnsupportedOperationException
     *           if the input does not contain a JSON object
     * @deprecated Use {@link Json#parse(String)}{@link JsonValue#asObject() .asObject()} instead
     */
    @Deprecated
    public static JsonObject readFrom(String string) {
      return JsonValue.readFrom(string).asObject();
    }
  
    /**
     * Returns an unmodifiable JsonObject for the specified one. This method allows to provide
     * read-only access to a JsonObject.
     * <p>
     * The returned JsonObject is backed by the given object and reflect changes that happen to it.
     * Attempts to modify the returned JsonObject result in an
     * <code>UnsupportedOperationException</code>.
     * </p>
     *
     * @param object
     *          the JsonObject for which an unmodifiable JsonObject is to be returned
     * @return an unmodifiable view of the specified JsonObject
     */
    public static JsonObject unmodifiableObject(JsonObject object) {
      return new JsonObject(object, true);
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the JSON
     * representation of the specified <code>int</code> value.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, int value) {
      add(name, Json.value(value));
      return this;
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the JSON
     * representation of the specified <code>long</code> value.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, long value) {
      add(name, Json.value(value));
      return this;
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the JSON
     * representation of the specified <code>float</code> value.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, float value) {
      add(name, Json.value(value));
      return this;
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the JSON
     * representation of the specified <code>double</code> value.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, double value) {
      add(name, Json.value(value));
      return this;
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the JSON
     * representation of the specified <code>boolean</code> value.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, boolean value) {
      add(name, Json.value(value));
      return this;
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the JSON
     * representation of the specified string.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, String value) {
      add(name, Json.value(value));
      return this;
    }
  
    /**
     * Appends a new member to the end of this object, with the specified name and the specified JSON
     * value.
     * <p>
     * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
     * that already exists in the object will append another member with the same name. In order to
     * replace existing members, use the method <code>set(name, value)</code> instead. However,
     * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
     * search for existing members). Therefore <em>add</em> should be preferred when constructing new
     * objects.
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add, must not be <code>null</code>
     * @return the object itself, to enable method chaining
     */
    public JsonObject add(String name, JsonValue value) {
      if (name == null) {
        throw new NullPointerException("name is null");
      }
      if (value == null) {
        throw new NullPointerException("value is null");
      }
      table.add(name, names.size());
      names.add(name);
      values.add(value);
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the JSON representation of the
     * specified <code>int</code> value. If this object does not contain a member with this name, a
     * new member is added at the end of the object. If this object contains multiple members with
     * this name, only the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to replace
     * @param value
     *          the value to set to the member
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, int value) {
      set(name, Json.value(value));
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the JSON representation of the
     * specified <code>long</code> value. If this object does not contain a member with this name, a
     * new member is added at the end of the object. If this object contains multiple members with
     * this name, only the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to replace
     * @param value
     *          the value to set to the member
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, long value) {
      set(name, Json.value(value));
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the JSON representation of the
     * specified <code>float</code> value. If this object does not contain a member with this name, a
     * new member is added at the end of the object. If this object contains multiple members with
     * this name, only the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, float value) {
      set(name, Json.value(value));
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the JSON representation of the
     * specified <code>double</code> value. If this object does not contain a member with this name, a
     * new member is added at the end of the object. If this object contains multiple members with
     * this name, only the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, double value) {
      set(name, Json.value(value));
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the JSON representation of the
     * specified <code>boolean</code> value. If this object does not contain a member with this name,
     * a new member is added at the end of the object. If this object contains multiple members with
     * this name, only the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, boolean value) {
      set(name, Json.value(value));
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the JSON representation of the
     * specified string. If this object does not contain a member with this name, a new member is
     * added at the end of the object. If this object contains multiple members with this name, only
     * the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, String value) {
      set(name, Json.value(value));
      return this;
    }
  
    /**
     * Sets the value of the member with the specified name to the specified JSON value. If this
     * object does not contain a member with this name, a new member is added at the end of the
     * object. If this object contains multiple members with this name, only the last one is changed.
     * <p>
     * This method should <strong>only be used to modify existing objects</strong>. To fill a new
     * object with members, the method <code>add(name, value)</code> should be preferred which is much
     * faster (as it does not need to search for existing members).
     * </p>
     *
     * @param name
     *          the name of the member to add
     * @param value
     *          the value of the member to add, must not be <code>null</code>
     * @return the object itself, to enable method chaining
     */
    public JsonObject set(String name, JsonValue value) {
      if (name == null) {
        throw new NullPointerException("name is null");
      }
      if (value == null) {
        throw new NullPointerException("value is null");
      }
      int index = indexOf(name);
      if (index != -1) {
        values.set(index, value);
      } else {
        table.add(name, names.size());
        names.add(name);
        values.add(value);
      }
      return this;
    }
  
    /**
     * Removes a member with the specified name from this object. If this object contains multiple
     * members with the given name, only the last one is removed. If this object does not contain a
     * member with the specified name, the object is not modified.
     *
     * @param name
     *          the name of the member to remove
     * @return the object itself, to enable method chaining
     */
    public JsonObject remove(String name) {
      if (name == null) {
        throw new NullPointerException("name is null");
      }
      int index = indexOf(name);
      if (index != -1) {
        table.remove(index);
        names.remove(index);
        values.remove(index);
      }
      return this;
    }
  
    /**
     * Copies all members of the specified object into this object. When the specified object contains
     * members with names that also exist in this object, the existing values in this object will be
     * replaced by the corresponding values in the specified object.
     *
     * @param object
     *          the object to merge
     * @return the object itself, to enable method chaining
     */
    public JsonObject merge(JsonObject object) {
      if (object == null) {
  