org.kopitubruk.util.json
Class JsonObject

java.lang.Object
  extended by org.kopitubruk.util.json.JsonObject
All Implemented Interfaces:
JSONAble

public class JsonObject
extends Object
implements JSONAble

This class provides a way to make an list of properties to be used to create JSON objects. It's an alternative to using Maps. It works effectively like using a LinkedHashMap provided you don't need it to check for duplicate keys.

You add properties as key-value pairs using the add(Object,Object) method which returns this JsonObject so that you can do adds in series for convenience.

This class implements JSONAble and so has all of its toJSON methods available for convenience.

For convenience, this class also takes a reference to a JSONConfig object either in a constructor or with setJSONConfig(JSONConfig). If set, then it will be used with toString(), toJSON() and toJSON(Writer). If toJSON(JSONConfig) or toJSON(JSONConfig, Writer) are sent null config objects, then they will also use the one set in this object if available.

For performance, this object does no checking for duplicate property names. Properties are merely added to a list. If you use a fixed size constructor then the list is backed by an array. If you use another constructor then the list is backed by an ArrayList. With the fixed size version there is no size checking so if you try to add more properties than the size that you gave to the constructor, then you will get an ArrayIndexOutOfBoundsException.

The performance gain vs. a Map is admittedly small and difficult to measure unless you are encoding objects that have large numbers of fields to be serialized.

The fixed size version does save memory if you size it with the exact number of properties that you need to store because the only storage used is the array, the number of items added so far and the key value pairs whereas Maps tend to have a fair amount of extra storage to facilitate faster lookups. Even the dynamically sized version may save memory depending upon how much extra space you end up with in the ArrayList.

Author:
Bill Davidson

Constructor Summary
JsonObject()
          Create a dynamically sized JsonObject backed by an ArrayList.
JsonObject(int size)
          Create a fixed size JsonObject backed by an array.
JsonObject(int size, JSONConfig cfg)
          Create a fixed size JsonObject backed by an array.
JsonObject(JSONConfig cfg)
          Create a dynamically sized JsonObject backed by an ArrayList.
 
Method Summary
 JsonObject add(Object name, Object value)
          Add a property to the property list.
 void clear()
          Clear the property list.
 JSONConfig getJSONConfig()
          Get the JSONConfig or null if there isn't one.
 void setJSONConfig(JSONConfig cfg)
          Set the JSONConfig for this object.
 int size()
          Get the number of elements in this property list.
 String toJSON()
          Return the JSON encoding of this object using the configuration options set in this object or defaults if they are not set.
 String toJSON(JSONConfig jsonConfig)
          Return the JSON encoding of this object using the given configuration options.
 void toJSON(JSONConfig jsonConfig, Writer json)
          Write the JSON encoding of this object to the given Writer using the given configuration options.
 void toJSON(Writer json)
          Write this JSON encoding of this object to the given Writer using the configuration options set in this object or defaults if they are not set.
 String toString()
          Return the JSON encoding of this object using the configuration options set in this object or defaults if they are not set.
 void trimToSize()
          If this is a dynamically sized JsonObject, then call ArrayList.trimToSize() on its backing storage.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonObject

public JsonObject()
Create a dynamically sized JsonObject backed by an ArrayList.


JsonObject

public JsonObject(JSONConfig cfg)
Create a dynamically sized JsonObject backed by an ArrayList.

Parameters:
cfg - A config object to be used with the toJSON or toString methods.

JsonObject

public JsonObject(int size)
Create a fixed size JsonObject backed by an array.

Parameters:
size - The size of the property array.

JsonObject

public JsonObject(int size,
                  JSONConfig cfg)
Create a fixed size JsonObject backed by an array.

Parameters:
size - The size of the property array.
cfg - A config object to be used with the toJSON or toString methods.
Method Detail

add

public JsonObject add(Object name,
                      Object value)
Add a property to the property list.

Parameters:
name - The name of the property.
value - The value of the property.
Returns:
This JsonObject allowing you to do adds in series.

size

public int size()
Get the number of elements in this property list.

Returns:
The number of elements in this property list.

clear

public void clear()
Clear the property list.


trimToSize

public void trimToSize()
If this is a dynamically sized JsonObject, then call ArrayList.trimToSize() on its backing storage. Otherwise, do nothing.


getJSONConfig

public JSONConfig getJSONConfig()
Get the JSONConfig or null if there isn't one.

Returns:
the JSONConfig or null if there isn't one.

setJSONConfig

public void setJSONConfig(JSONConfig cfg)
Set the JSONConfig for this object.

Parameters:
cfg - the JSONConfig to set

toString

public String toString()
Return the JSON encoding of this object using the configuration options set in this object or defaults if they are not set.

Overrides:
toString in class Object
Returns:
the JSON encoding of this object.

toJSON

public String toJSON()
Return the JSON encoding of this object using the configuration options set in this object or defaults if they are not set.

Specified by:
toJSON in interface JSONAble
Returns:
A string of JSON data.

toJSON

public String toJSON(JSONConfig jsonConfig)
Return the JSON encoding of this object using the given configuration options.

Specified by:
toJSON in interface JSONAble
Parameters:
jsonConfig - A configuration object to use to set encoding options. If null, then this JsonObject's config will be used if it is set.
Returns:
A string of JSON data.

toJSON

public void toJSON(Writer json)
            throws IOException
Write this JSON encoding of this object to the given Writer using the configuration options set in this object or defaults if they are not set.

Specified by:
toJSON in interface JSONAble
Parameters:
json - A writer for the output.
Throws:
IOException - If there is an error on output.

toJSON

public void toJSON(JSONConfig jsonConfig,
                   Writer json)
            throws IOException
Write the JSON encoding of this object to the given Writer using the given configuration options.

Specified by:
toJSON in interface JSONAble
Parameters:
jsonConfig - A configuration object to use to set encoding options. If null, then this JsonObject's config will be used if it is set.
json - A writer for the output.
Throws:
IOException - If there is an error on output.


Copyright © 2016. All rights reserved.