Class JSObject

java.lang.Object
org.bzdev.util.JSObject
All Implemented Interfaces:
JSOps
Direct Known Subclasses:
ExpressionParser.ESPObject, NJSObject

public class JSObject extends Object implements JSOps
Simplified JavaScript-like object class. Formats such as JSON are syntactically similar to the source code for JavaScript arrays and JavaScript objects that contain properties but not methods, to the source code for JavaScript arrays, and the source code for numbers, strings, boolean values, and (of course) the value null. The types of the values that can be inserted into this object are JSObject, JSArray, Boolean, Number, and String. the value null. This class represents an object with values that are associated with keys.

The class JSUtilities.JSON can be used to create instances of JSObject and JSArray, and instances of these two classes in turn can be used to configure a named-object factory, which can then create a named object.

This class is based on Map (a subset of that Map's methods), but with some run-time type checking. In addition to primitive types (boolean numbers, and strings), entries in the map can be other instances of this class or instances of JSArray, allowing trees or directed graphs to be constructed. Iterators will provide the keys in the order in which they were inserted.

See Also:
  • Constructor Details

    • JSObject

      public JSObject()
      Constructor.
    • JSObject

      protected JSObject(JSObject base)
      Constructor sharing the same tables. This is used by NJSObject.
      Parameters:
      base - the JSObject whose tables should be used
  • Method Details

    • identity

      public long identity()
      Description copied from interface: JSOps
      Get an integer identifying an instance of a class.
      Specified by:
      identity in interface JSOps
      Returns:
      the identity
    • put

      public Object put(String key, JSObject object)
      Insert an instance of JSObject into this map.
      Parameters:
      key - the key
      object - another instance of JSObject providing the value for the key
      Returns:
      the previous object; null if there was none
    • put

      public Object put(String key, JSArray array)
      Insert an instance of JSArray into this map.
      Parameters:
      key - the key
      array - the value
      Returns:
      the previous object; null if there was none
    • put

      public Object put(String key, Number value)
      Insert a number into this map.
      Parameters:
      key - the key
      value - the value
      Returns:
      the previous object; null if there was none
    • put

      public Object put(String key, String string)
      Insert a string into this map.
      Parameters:
      key - the key
      string - the string
      Returns:
      the previous object; null if there was none
    • put

      public Object put(String key, Boolean value)
      Insert a boolean value into this map.
      Parameters:
      key - the key
      value - the boolean value (true or false)
      Returns:
      the previous object; null if there was none
    • put

      public Object put(String key, Object object) throws IllegalArgumentException
      Put an object into this map. The object may be null. If it is not null, it may be an instance of String, Boolean, Number, JSArray, or JSObject.
      Parameters:
      key - the key
      object - the object
      Returns:
      the previous object; null if there was none
      Throws:
      IllegalArgumentException
    • putObject

      public Object putObject(String key, Object object)
      Insert any object. When this method is used, the object may not be suitable for use with YAML or JSON, and may not work properly with toKeyMap().
      Parameters:
      key - the key for this entry
      object - the object to store for the specified key
      Returns:
      the previous object; null if there was none
    • size

      public int size()
      Get the number of elements contained in this object.
      Specified by:
      size in interface JSOps
      Returns:
      the number of elements
    • keySet

      public Set<String> keySet()
      Get a set of the keys for this object
      Returns:
      a set of keys
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Get an entry set for this object, each containing a key, value pair.
      Returns:
      the entry set
    • containsKey

      public boolean containsKey(String key)
      Determine if a key exists for this object.
      Parameters:
      key - the key
      Returns:
      true if the key has a value; false otherwise.
    • remove

      public Object remove(String key)
      Remove the value associated with a key
      Parameters:
      key - the key
      Returns:
      the value removed; null if there is none
    • get

      public Object get(String key)
      Get the object associated with a key for this map.
      Parameters:
      key - the key
      Returns:
      the value for the specified key
    • get

      public <T> T get(String key, Class<T> clasz)
      Get the object associated with a key for this map, cast to a specified type.
      Type Parameters:
      T - the type for the argument clasz
      Parameters:
      key - the key
      clasz - the class of the object that will be returned.
      Returns:
      the value for the specified key
    • toKeyMap

      Create the key map equivalent to this object. This method returns a key map containing the same keyword-value pairs, but with values that are instances of JSArray converted to key-map lists. If an element in the list is not an instance of JSObject, that element is replaced with a key map containing a single entry: one whose key is the key corresponding to the JSArray, and whose value is the list element. If the element in the list is a JSObject, that JSObject is converted to a key list (by calling this method).

      The single-entry maps are inserted in those cases where an entry in a list would be another list.

      If creating a JSON representation of this object, the key map should be modified to properly quote strings.

      Returns:
      the key map
      Throws:
      JSArray.ConversionException - a JSArray contained another JSArray
      See Also: