it.negro.jajb
Class JAJB

java.lang.Object
  extended by it.negro.jajb.JAJB

public final class JAJB
extends java.lang.Object

JAJB is the main access point for binding between JSON and Java Object. It exposes 4 ways to realize binding:

1) You can use mapping way writing a configuration file for JAJB and one or more mapping files that describe all the entities you want to use; it's possible to describe converters, factories, complex inheritance relations, specific constructors and other features for all the objects. If you use this way, you can use the Collection types and array types as you want; you can have the same features using the second way through the annotation configuration and SimpleJAJB class; otherwise, if you use the third way, you can't use collection types, but only array types.

2) If you describe your beans using EntityAnnotation and FieldAnnotation, you will have all configuration features (converters, factories, inheritance, etc.). Remember that if your bean not have the EntityAnnotation, all of its FieldAnnotation will be ignored.

3) You can use JAJB without mapping: using this way you lose all the special features of the mapping way (conversions, factories, etc.), but if json input structure is exactly equals respect to correspondent Java Bean and you don't need the mapping way features, you can use this easier way. If you don't use any mapping way , you can't use the Collection types, you must use the arrays.

4) You can use the three ways described before all together, combining xml mapping, annotated mapping and no mapping! Remember that you cannot use Collection types for all of your not mapped bean! Each bean can be referred by more ids and refIds:
1)For XML the ids must start with '$' and can continue as you want.
2) For annotation the ids must start with '@' and must continue with the correct full class name of the bean.
3) If you don't use any mapping the ids must start with '#' and must continue with the correct full class name of the bean.

Author:
Gabriele Negro

Method Summary
 it.dangelo.domjson.JSON getJSON(java.io.File file)
          Reads the passed parameter file and uses it to create a JSON object o array.
 it.dangelo.domjson.JSON getJSON(java.io.InputStream inputStream)
          Reads the passed parameter inputStream content and uses it to create a JSON object o array.
 it.dangelo.domjson.JSON getJSON(java.io.Reader reader)
          Reads the passed parameter reader content and uses it to create a JSON object o array.
 it.dangelo.domjson.JSON getJSON(java.lang.String jsonString)
          Converts passed parameter jsonString into a JSON entity.
 java.lang.String marshall(java.lang.Object object)
          Makes the same work of marshall (Object, String) method but considering the mapping runtime found: if class type of passed object parameter has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.
 java.lang.String marshall(java.lang.Object object, java.lang.String id)
          This method serializes passed object parameter into a json string according to passed id parameter that represents the id of passed object into relative mapping file.
 java.lang.String marshallSimpleArray(java.lang.Object array)
          Converts an array into a String representing a JSONArray.
 it.dangelo.domjson.JSONArray marshallSimpleArrayToJSONArray(java.lang.Object array)
          Converts an array into a JSONArray.
 java.lang.String marshallSimpleCollection(java.util.Collection<?> collection, java.lang.Class<?> componentType)
          Converts a collection into a String representing a JSONArray.
 it.dangelo.domjson.JSONArray marshallSimpleCollectionToJSONArray(java.util.Collection<?> collection, java.lang.Class<?> componentType)
          Converts a collection into a JSONArray.
 it.dangelo.domjson.JSONElement marshallToJSONElement(java.util.Date date, java.text.SimpleDateFormat simpleDateFormat)
          Convert a Date into a JSONElement using passed SimpleDateFormat if not null, otherwise using the default SimpleDateFormat (yyyy-MM-dd'T'HH:mm:ss'Z'ZZ);
 it.dangelo.domjson.JSONElement marshallToJSONElement(java.lang.Object object)
          Makes the same work of marshallToJSONElement (Object, String) method but considering the mapping runtime found: if class type of passed object parameter has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.
 it.dangelo.domjson.JSONElement marshallToJSONElement(java.lang.Object object, java.lang.String id)
          This method converts passed object parameter into a JSONElement according to passed id parameter that represents the id of passed object into relative mapping file or annotated id or simple mapping id.
static JAJB newInstance()
          This method returns a JAJB instance.
static JAJB newInstance(java.io.InputStream inputStream)
          This method returns a JAJB instance built parsing the configuration file located at the path specified by passed inputStream parameter.
static JAJB newInstance(java.util.Properties properties)
          Builds a new jajb instance configured with desired ProxyResolver class you have to put into passed properties parameter.
 java.lang.Object unmarshall(it.dangelo.domjson.JSONElement jsonElement, java.lang.Class<?> clazz)
          Makes the same work of unmarshall(JSONElement, String) method but considering the mapping runtime found: if clazz has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.
 java.lang.Object unmarshall(it.dangelo.domjson.JSONElement jsonElement, java.lang.String id)
          This method makes binding between passed jsonElement parameter and the specific java bean that you should have described in one of your mapping files with the id passed parameter.
 java.lang.Object unmarshall(java.lang.String jsonString, java.lang.Class<?> clazz)
          Makes the same work of unmarshall(String, String) method but considering the mapping runtime found: if clazz has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.
 java.lang.Object unmarshall(java.lang.String jsonString, java.lang.String id)
          This method makes binding between passed jsonString parameter and the specific java bean that you should have described in one of your mapping files (be careful to instantiation method used to get JAJB instance) with the id passed parameter or with id of annotated bean (if it starts with '@') or with simple id (if it starts with '#').
 java.lang.Object unmarshallSimpleArray(it.dangelo.domjson.JSONArray jsonArray, java.lang.Class<?> arrayClass)
          Converts a JSONArray into Java Array.
 java.lang.Object unmarshallSimpleArray(java.lang.String jsonArrayString, java.lang.Class<?> arrayClass)
          Converts a string representing a JSONArray into Java Array.
 java.util.Collection<?> unmarshallSimpleCollection(it.dangelo.domjson.JSONArray jsonArray, java.lang.Class<?> collectionClass, java.lang.Class<?> componentType)
          Converts a JSONArray into a Collection of the specifies collectionClass (for example ArrayList.class) containing specified componentType component.
 java.util.Collection<?> unmarshallSimpleCollection(java.lang.String jsonArrayString, java.lang.Class<?> collectionClass, java.lang.Class<?> componentType)
          Converts a string representing a JSONArray into a Collection of the specifies collectionClass (for example ArrayList.class) containing specified componentType component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newInstance

public static JAJB newInstance(java.io.InputStream inputStream)
                        throws JAJBException
This method returns a JAJB instance built parsing the configuration file located at the path specified by passed inputStream parameter.

Parameters:
inputStream - an InputStream built with your JAJB configuration file.
Returns:
JAJB a JAJB instance with all mapping configuration informations initialized.
Throws:
JAJBException - this exception maybe caused by an IOException or SAXExcpetion (make sure of configuration file informations like path, sintax, etc.).

newInstance

public static JAJB newInstance()
                        throws JAJBException
This method returns a JAJB instance. Using this instance you cannot use xml mapping.

Returns:
JAJB a JAJB instance.
Throws:
JAJBException

newInstance

public static JAJB newInstance(java.util.Properties properties)
                        throws JAJBException
Builds a new jajb instance configured with desired ProxyResolver class you have to put into passed properties parameter. You should put into properties a property with 'proxyResolver' like key and with your desired ProxyResolver implementation class name.

Parameters:
properties - the properties in witch you should put the ProxyResolver class name you want to use
Returns:
JAJB instance
Throws:
JAJBException

unmarshall

public java.lang.Object unmarshall(java.lang.String jsonString,
                                   java.lang.String id)
                            throws JAJBUnMarshallException
This method makes binding between passed jsonString parameter and the specific java bean that you should have described in one of your mapping files (be careful to instantiation method used to get JAJB instance) with the id passed parameter or with id of annotated bean (if it starts with '@') or with simple id (if it starts with '#'). If your jsonString represents a JSONArray, you should pass into id parameter, the id of java bean that are components of expected resulting array.

Parameters:
jsonString - the json string that represents the java bean that you want to obtain invoking this method.
id - id of specific java bean.
Returns:
an instance of java bean mapped with the specified id.
Throws:
JAJBUnMarshallException

unmarshall

public java.lang.Object unmarshall(java.lang.String jsonString,
                                   java.lang.Class<?> clazz)
                            throws JAJBUnMarshallException
Makes the same work of unmarshall(String, String) method but considering the mapping runtime found: if clazz has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.

Parameters:
jsonString - the json string that represents the java bean that you want to obtain as result.
clazz - the class type of the bean you want to obtain as result
Returns:
an instance of java bean with class as clazz parameter and populated using jsonString parameter
Throws:
JAJBUnMarshallException

unmarshall

public java.lang.Object unmarshall(it.dangelo.domjson.JSONElement jsonElement,
                                   java.lang.String id)
                            throws JAJBUnMarshallException
This method makes binding between passed jsonElement parameter and the specific java bean that you should have described in one of your mapping files with the id passed parameter. If your josonElement represents a JSONArray, you should pass into id parameter, the id of java bean that are components of expected resulting array. A correct id starts with '$' or '#' or '@'. Using '#' or '@' prefix, id must continue with full class name of java object you refer.

Parameters:
jsonElement - the JSONElement that represents the java bean that you want to obtain invoking this method.
id - id of specific java bean that you should have described in one of your mapping files
Returns:
an instance of java bean mapped with the specified id in your mapping files.
Throws:
JAJBUnMarshallException

unmarshall

public java.lang.Object unmarshall(it.dangelo.domjson.JSONElement jsonElement,
                                   java.lang.Class<?> clazz)
                            throws JAJBUnMarshallException
Makes the same work of unmarshall(JSONElement, String) method but considering the mapping runtime found: if clazz has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.

Parameters:
jsonElement - the JSONElement that represents the java bean that you want to obtain invoking this method.
clazz - the class type of the bean you want to obtain as result
Returns:
an instance of java bean with class as clazz parameter and populated using jsonString parameter
Throws:
JAJBUnMarshallException

marshall

public java.lang.String marshall(java.lang.Object object,
                                 java.lang.String id)
                          throws JAJBMarshallException
This method serializes passed object parameter into a json string according to passed id parameter that represents the id of passed object into relative mapping file. If the object parameter is a collection o an array the id parameter should be the id of the java beans that are components of the array.

Parameters:
object - the collection or array or java bean you want to serialize.
id - id of specific java bean that you should have described in one of your mapping files
Returns:
- a String in JSON format that represents passed object parameter.
Throws:
JAJBMarshallException

marshall

public java.lang.String marshall(java.lang.Object object)
                          throws JAJBMarshallException
Makes the same work of marshall (Object, String) method but considering the mapping runtime found: if class type of passed object parameter has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.

Parameters:
object - the collection or array or java bean you want to serialize.
Returns:
- a String in JSON format that represents passed object parameter.
Throws:
JAJBMarshallException

marshallToJSONElement

public it.dangelo.domjson.JSONElement marshallToJSONElement(java.lang.Object object,
                                                            java.lang.String id)
                                                     throws JAJBMarshallException,
                                                            JAJBException
This method converts passed object parameter into a JSONElement according to passed id parameter that represents the id of passed object into relative mapping file or annotated id or simple mapping id. If the object parameter is a collection o an array the id parameter should be the id of the java beans that are components of the array.

Parameters:
object - the collection or array or java bean you want to serialize.
id - id of specific java bean that you should have described in one of your mapping files
Returns:
- a String in JSON format that represents passed object parameter.
Throws:
JAJBMarshallException
JAJBException

marshallToJSONElement

public it.dangelo.domjson.JSONElement marshallToJSONElement(java.util.Date date,
                                                            java.text.SimpleDateFormat simpleDateFormat)
                                                     throws JAJBMarshallException
Convert a Date into a JSONElement using passed SimpleDateFormat if not null, otherwise using the default SimpleDateFormat (yyyy-MM-dd'T'HH:mm:ss'Z'ZZ);

Parameters:
date - the date to convert
simpleDateFormat - the format for conversion
Returns:
- a JSONElement representing passed date.
Throws:
JAJBMarshallException

marshallToJSONElement

public it.dangelo.domjson.JSONElement marshallToJSONElement(java.lang.Object object)
                                                     throws JAJBMarshallException
Makes the same work of marshallToJSONElement (Object, String) method but considering the mapping runtime found: if class type of passed object parameter has an EntityAnnotation makes binding using its annotated mapping, otherwise without any mapping.

Parameters:
object - the collection or array or java bean you want to serialize.
id - id of specific java bean that you should have described in one of your mapping files
Returns:
- a String in JSON format that represents passed object parameter.
Throws:
JAJBMarshallException

unmarshallSimpleArray

public java.lang.Object unmarshallSimpleArray(java.lang.String jsonArrayString,
                                              java.lang.Class<?> arrayClass)
                                       throws JAJBUnMarshallException
Converts a string representing a JSONArray into Java Array. Use it only for simple component type (wrapper, primitive or Date

Parameters:
jsonArrayString - the string to convert into Java Array
arrayClass - the array class for result (for example 'int[].class', 'String[].class', etc.
Returns:
- an array like specified in arrayClass parameter
Throws:
JAJBUnMarshallException

unmarshallSimpleArray

public java.lang.Object unmarshallSimpleArray(it.dangelo.domjson.JSONArray jsonArray,
                                              java.lang.Class<?> arrayClass)
                                       throws JAJBUnMarshallException
Converts a JSONArray into Java Array. Use it only for simple component type (wrapper, primitive or Date

Parameters:
jsonArray - the JSONArray to convert
arrayClass - the array class for result (for example 'int[].class', 'String[].class', etc.
Returns:
- an array like specified in arrayClass parameter
Throws:
JAJBUnMarshallException

unmarshallSimpleCollection

public java.util.Collection<?> unmarshallSimpleCollection(java.lang.String jsonArrayString,
                                                          java.lang.Class<?> collectionClass,
                                                          java.lang.Class<?> componentType)
                                                   throws JAJBUnMarshallException
Converts a string representing a JSONArray into a Collection of the specifies collectionClass (for example ArrayList.class) containing specified componentType component.

Parameters:
jsonArayString - the string to convert
collectionClass - the collection class for result (for example 'ArrayList.class', 'Vector.class', etc.
componentType - the class of collection component
Returns:
- a collection like specified in collectionClass parameter
Throws:
JAJBUnMarshallException

unmarshallSimpleCollection

public java.util.Collection<?> unmarshallSimpleCollection(it.dangelo.domjson.JSONArray jsonArray,
                                                          java.lang.Class<?> collectionClass,
                                                          java.lang.Class<?> componentType)
                                                   throws JAJBUnMarshallException
Converts a JSONArray into a Collection of the specifies collectionClass (for example ArrayList.class) containing specified componentType component.

Parameters:
jsonArray - the JSONArray to convert
collectionClass - the collection class for result (for example 'ArrayList.class', 'Vector.class', etc.
componentType - the class of collection component
Returns:
- a collection like specified in collectionClass parameter
Throws:
JAJBUnMarshallException

marshallSimpleArray

public java.lang.String marshallSimpleArray(java.lang.Object array)
                                     throws JAJBMarshallException
Converts an array into a String representing a JSONArray. Converts only Date[] or primitive array (int[], char[], etc) or wrapper array (String[], Boolean[], etc.)

Parameters:
array - the array to convert
Returns:
- passed array parameter converted into JSONArray
Throws:
JAJBMarshallException

marshallSimpleArrayToJSONArray

public it.dangelo.domjson.JSONArray marshallSimpleArrayToJSONArray(java.lang.Object array)
                                                            throws JAJBMarshallException
Converts an array into a JSONArray. Converts only Date[] or primitive array (int[], char[], etc) or wrapper array (String[], Boolean[], etc.)

Parameters:
array - the array to convert
Returns:
- passed array parameter converted into JSONArray
Throws:
JAJBMarshallException

marshallSimpleCollection

public java.lang.String marshallSimpleCollection(java.util.Collection<?> collection,
                                                 java.lang.Class<?> componentType)
                                          throws JAJBMarshallException
Converts a collection into a String representing a JSONArray. Component type must be a wrapper type (String, Long, etc.) or a Date

Parameters:
collection -
componentType -
Returns:
JSONArray
Throws:
JAJBMarshallException

marshallSimpleCollectionToJSONArray

public it.dangelo.domjson.JSONArray marshallSimpleCollectionToJSONArray(java.util.Collection<?> collection,
                                                                        java.lang.Class<?> componentType)
                                                                 throws JAJBMarshallException
Converts a collection into a JSONArray. Component type must be a wrapper type (String, Long, etc.) or a Date

Parameters:
collection -
componentType -
Returns:
JSONArray
Throws:
JAJBMarshallException

getJSON

public it.dangelo.domjson.JSON getJSON(java.lang.String jsonString)
                                throws JAJBException
Converts passed parameter jsonString into a JSON entity. So will be possible to consult and navigate the jsonString content through the JSON object or array. Obviously, passed parameter jsonString, must be "well formed": it must respect json specifications. If it isn't so, an exception will be thrown.

Parameters:
jsonString -
Returns:
JSON view of jsonString
Throws:
JAJBException

getJSON

public it.dangelo.domjson.JSON getJSON(java.io.File file)
                                throws JAJBException
Reads the passed parameter file and uses it to create a JSON object o array. Obviously, passed parameter file, must contain a "well formed" content: it must respect json specifications. If it isn't so, an exception will be thrown.

Parameters:
file -
Returns:
JSON view of file
Throws:
JAJBException

getJSON

public it.dangelo.domjson.JSON getJSON(java.io.InputStream inputStream)
                                throws JAJBException
Reads the passed parameter inputStream content and uses it to create a JSON object o array. Obviously, passed parameter inputStream, must contain a "well formed" content: it must respect json specifications. If it isn't so, an exception will be thrown.

Parameters:
inputStream -
Returns:
JSON view of input stream
Throws:
JAJBException

getJSON

public it.dangelo.domjson.JSON getJSON(java.io.Reader reader)
                                throws JAJBException
Reads the passed parameter reader content and uses it to create a JSON object o array. Obviously, passed parameter reader, must read a "well formed" content: it must respect json specifications. If it isn't so, an exception will be thrown.

Parameters:
reader -
Returns:
JSON view of file
Throws:
JAJBException


Copyright © 2008. All Rights Reserved.