net.sf.json
Class JSONSerializer

java.lang.Object
  extended by net.sf.json.JSONSerializer

public class JSONSerializer
extends Object

Transforms java objects into JSON and back.
Transformation from java to JSON is pretty straightforward, but the other way around needs certain configuration, otherwise the java objects produced will be DynaBeans and Lists, because the JSON notation does not carry any information on java classes.
Use the provided property setters before calling toJava().

Author:
Andres Almiray

Field Summary
static int MODE_LIST
           
static int MODE_OBJECT_ARRAY
           
 
Constructor Summary
JSONSerializer()
          Default constructor
JSONSerializer(Class rootClass, Map classMap, int arrayMode)
          Constructs a new JSONSerializer with specific values for conversion.
 
Method Summary
 int getArrayMode()
          Returns the current array mode conversion
 Map getClassMap()
          Returns the current attribute/class Map
 Class getRootClass()
          Returns the current root Class.
 void reset()
          Resets this serializer to default values.
 void setArrayMode(int arrayMode)
          Sets the current array mode for conversion.
 void setClassMap(Map classMap)
          Sets the current attribute/Class Map
 void setRootClass(Class rootClass)
          Sets the current root Class
 Object toJava(JSON json)
          Transform a JSON value to a java object.
static JSON toJSON(Object object)
          Creates a JSONObject, JSONArray or a JSONNull from object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODE_LIST

public static final int MODE_LIST
See Also:
Constant Field Values

MODE_OBJECT_ARRAY

public static final int MODE_OBJECT_ARRAY
See Also:
Constant Field Values
Constructor Detail

JSONSerializer

public JSONSerializer()
Default constructor


JSONSerializer

public JSONSerializer(Class rootClass,
                      Map classMap,
                      int arrayMode)
Constructs a new JSONSerializer with specific values for conversion.

Parameters:
rootClass - the target class for conversion
classMap - Map of classes, every key identifies a property or a regexp
arrayMode - array mode for conversion, either MODE_OBJECT_ARRAY or MODE_LIST
Method Detail

toJSON

public static JSON toJSON(Object object)
Creates a JSONObject, JSONArray or a JSONNull from object.

Parameters:
object - any java Object
Throws:
JSONException - if the object can not be converted

getArrayMode

public int getArrayMode()
Returns the current array mode conversion

Returns:
either MODE_OBJECT_ARRAY or MODE_LIST

getClassMap

public Map getClassMap()
Returns the current attribute/class Map

Returns:
a Map of classes, every key identifies a property or a regexp

getRootClass

public Class getRootClass()
Returns the current root Class.

Returns:
the target class for conversion

reset

public void reset()
Resets this serializer to default values.
rootClass = null
classMap = null
arrayMode = MODE_LIST


setArrayMode

public void setArrayMode(int arrayMode)
Sets the current array mode for conversion.
If the value is not MODE_LIST neither MODE_OBJECT_ARRAY, then MODE_LIST will be used.

Parameters:
arrayMode - array mode for conversion

setClassMap

public void setClassMap(Map classMap)
Sets the current attribute/Class Map

Parameters:
classMap - a Map of classes, every key identifies a property or a regexp

setRootClass

public void setRootClass(Class rootClass)
Sets the current root Class

Parameters:
rootClass - the target class for conversion

toJava

public Object toJava(JSON json)
Transform a JSON value to a java object.
Depending on the configured values for conversion this will return a DynaBean, a bean, a List, or and array.

Parameters:
json - a JSON value
Returns:
depends on the nature of the source object (JSONObject, JSONArray, JSONNull) and the configured rootClass, classMap and arrayMode