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:
java.io.Serializable, java.lang.Cloneable, java.util.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
java.util.AbstractMap.SimpleEntry<K,V>, java.util.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(java.io.InputStream is)
          Create a new instance of this class from the data provided from the input stream.
OrderedJSONObject(java.io.InputStream is, boolean strict)
          Create a new instance of this class from the data provided from the input stream.
OrderedJSONObject(java.util.Map map)
          Create a new instance of this class using the contents of the provided map.
OrderedJSONObject(java.io.Reader rdr)
          Create a new instance of this class from the data provided from the reader.
OrderedJSONObject(java.io.Reader rdr, boolean strict)
          Create a new instance of this class from the data provided from the reader.
OrderedJSONObject(java.lang.String str)
          Create a new instance of this class from the provided JSON object string.
OrderedJSONObject(java.lang.String str, boolean strict)
          Create a new instance of this class from the provided JSON object string.
 
Method Summary
 void clear()
          (non-Javadoc)
 java.lang.Object clone()
          Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
 java.util.Iterator getOrder()
          Method to obtain the order in which the items will be serialized.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Method to put a JSON'able object into the instance.
 java.lang.Object remove(java.lang.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(java.lang.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(java.lang.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(java.io.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(java.io.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(java.io.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(java.io.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(java.util.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.
java.lang.NullPointerException - Thrown if the map is null, or a key in the map is null..
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.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 java.util.Map
Overrides:
put in class JSONObject
See Also:
HashMap.put(java.lang.Object, java.lang.Object)

remove

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

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

clear

public void clear()
(non-Javadoc)

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

clone

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

Overrides:
clone in class java.util.HashMap

getOrder

public java.util.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-2013 The Apache Software Foundation. All Rights Reserved.