org.apache.wink.json4j
Class OrderedJSONObject

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by org.apache.wink.json4j.JSONObject
              extended by org.apache.wink.json4j.OrderedJSONObject
All Implemented Interfaces:
Serializable, Cloneable, Map, JSONArtifact

public class OrderedJSONObject
extends JSONObject

Extension of the basic JSONObject. This class allows control of the serialization order of attributes. The order in which items are put into the instance controls the order in which they are serialized out. For example, the last item put is the last item serialized.

JSON-able values are: null, and instances of String, Boolean, Number, JSONObject and JSONArray.

Instances of this class are not thread-safe.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
 
Fields inherited from class org.apache.wink.json4j.JSONObject
NULL
 
Constructor Summary
OrderedJSONObject()
          Create a new instance of this class.
OrderedJSONObject(InputStream is)
          Create a new instance of this class from the data provided from the input stream.
OrderedJSONObject(InputStream is, boolean strict)
          Create a new instance of this class from the data provided from the input stream.
OrderedJSONObject(Map map)
          Create a new instance of this class using the contents of the provided map.
OrderedJSONObject(Reader rdr)
          Create a new instance of this class from the data provided from the reader.
OrderedJSONObject(Reader rdr, boolean strict)
          Create a new instance of this class from the data provided from the reader.
OrderedJSONObject(String str)
          Create a new instance of this class from the provided JSON object string.
OrderedJSONObject(String str, boolean strict)
          Create a new instance of this class from the provided JSON object string.
 
Method Summary
 void clear()
          (non-Javadoc)
 Object clone()
          Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
 Iterator getOrder()
          Method to obtain the order in which the items will be serialized.
 Object put(Object key, Object value)
          Method to put a JSON'able object into the instance.
 Object remove(Object key)
          Method to remove an entry from the OrderedJSONObject instance.
 
Methods inherited from class org.apache.wink.json4j.JSONObject
append, get, getBoolean, getDouble, getInt, getJSONArray, getJSONObject, getLong, getNames, getShort, getString, has, isNull, isValidObject, isValidType, keys, length, names, opt, optBoolean, optBoolean, optDouble, optDouble, optInt, optInt, optJSONArray, optJSONArray, optJSONObject, optJSONObject, optLong, optLong, optShort, optShort, optString, optString, put, put, put, put, put, put, put, put, put, put, put, put, put, putOnce, putOpt, sortedKeys, toJSONArray, toString, toString, toString, write, write, write, write, write, write, write, write, write
 
Methods inherited from class java.util.HashMap
containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

OrderedJSONObject

public OrderedJSONObject()
Create a new instance of this class.


OrderedJSONObject

public OrderedJSONObject(String str)
                  throws JSONException
Create a new instance of this class from the provided JSON object string. Note: This is the same as calling new OrderedJSONObject(str, false).

Parameters:
str - The String of JSON to parse
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

OrderedJSONObject

public OrderedJSONObject(String str,
                         boolean strict)
                  throws JSONException
Create a new instance of this class from the provided JSON object string.

Parameters:
str - The String of JSON to parse
strict - Boolean flag indicating if strict mode should be used. Strict mode means comments and unquoted strings are not allowed.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

OrderedJSONObject

public OrderedJSONObject(Reader rdr)
                  throws JSONException
Create a new instance of this class from the data provided from the reader. The reader content must be a JSON object string. Note: The reader will not be closed, that is left to the caller. Note: This is the same as calling new OrderedJSONObject(rdr, false).

Parameters:
rdr - The reader from which to read the JSON to parse
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

OrderedJSONObject

public OrderedJSONObject(Reader rdr,
                         boolean strict)
                  throws JSONException
Create a new instance of this class from the data provided from the reader. The reader content must be a JSON object string. Note: The reader will not be closed, that is left to the caller.

Parameters:
rdr - The reader from which to read the JSON to parse
strict - Boolean flag indicating if strict mode should be used. Strict mode means comments and unquoted strings are not allowed.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

OrderedJSONObject

public OrderedJSONObject(InputStream is)
                  throws JSONException
Create a new instance of this class from the data provided from the input stream. The stream content must be a JSON object string. Note: The input stream content is assumed to be UTF-8 encoded. Note: The InputStream will not be closed, that is left to the caller. Note: This is the same as calling new OrderedJSONObject(is, false).

Parameters:
is - The InputStream from which to read the JSON to parse
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

OrderedJSONObject

public OrderedJSONObject(InputStream is,
                         boolean strict)
                  throws JSONException
Create a new instance of this class from the data provided from the input stream. The stream content must be a JSON object string. Note: The input stream content is assumed to be UTF-8 encoded. Note: The InputStream will not be closed, that is left to the caller.

Parameters:
is - The InputStream from which to read the JSON to parse
strict - Boolean flag indicating if strict mode should be used. Strict mode means comments and unquoted strings are not allowed.
Throws:
JSONException - Thrown when the string passed is null, or malformed JSON..

OrderedJSONObject

public OrderedJSONObject(Map map)
                  throws JSONException
Create a new instance of this class using the contents of the provided map. The contents of the map should be values considered JSONable.

Parameters:
map - The map of key/value pairs to insert into this JSON object
Throws:
JSONException - Thrown when contents in the map cannot be made JSONable.
NullPointerException - Thrown if the map is null, or a key in the map is null..
Method Detail

put

public Object put(Object key,
                  Object value)
Method to put a JSON'able object into the instance. Note that the order of initial puts controls the order of serialization. Meaning that the first time an item is put into the object determines is position of serialization. Subsequent puts with the same key replace the existing entry value and leave serialization position alone. For moving the position, the object must be removed, then re-put.

Specified by:
put in interface Map
Overrides:
put in class JSONObject
See Also:
HashMap.put(java.lang.Object, java.lang.Object)

remove

public Object remove(Object key)
Method to remove an entry from the OrderedJSONObject instance.

Specified by:
remove in interface Map
Overrides:
remove in class HashMap
See Also:
HashMap.remove(java.lang.Object)

clear

public void clear()
(non-Javadoc)

Specified by:
clear in interface Map
Overrides:
clear in class HashMap
See Also:
HashMap.clear()

clone

public Object clone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

Overrides:
clone in class HashMap

getOrder

public Iterator getOrder()
Method to obtain the order in which the items will be serialized.

Returns:
An iterator that represents the attribute names in the order that they will be serialized.


Copyright © 2009-2012 The Apache Software Foundation. All Rights Reserved.