public class JsonObject extends Object implements JSONAble
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.
| Constructor and Description |
|---|
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. |
| Modifier and Type | Method and Description |
|---|---|
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. |
public JsonObject()
ArrayList.public JsonObject(JSONConfig cfg)
ArrayList.cfg - A config object to be used with the toJSON or toString methods.public JsonObject(int size)
size - The size of the property array.public JsonObject(int size,
JSONConfig cfg)
size - The size of the property array.cfg - A config object to be used with the toJSON or toString methods.public JsonObject add(Object name, Object value)
name - The name of the property.value - The value of the property.public int size()
public void clear()
public void trimToSize()
ArrayList.trimToSize() on its backing storage. Otherwise, do
nothing.public JSONConfig getJSONConfig()
public void setJSONConfig(JSONConfig cfg)
cfg - the JSONConfig to setpublic String toString()
public String toJSON()
public String toJSON(JSONConfig jsonConfig)
public void toJSON(Writer json) throws IOException
Writer using
the configuration options set in this object or defaults if they are not
set.toJSON in interface JSONAblejson - A writer for the output.IOException - If there is an error on output.public void toJSON(JSONConfig jsonConfig, Writer json) throws IOException
Writer using
the given configuration options.toJSON in interface JSONAblejsonConfig - 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.IOException - If there is an error on output.Copyright © 2016. All rights reserved.