public class JSONBeanSerializationHelper
extends Object
The following classes are used to support this functionality:
Supported types:
JSONSerializable
,
PostJSONSerializable
,
JSONDeserializable
,
PostJSONDeserializable
,
Example of usage:
public class MyBean
{
private String string;
private Integer integer;
public String getString()
{
return string;
}
public void setString(String str)
{
this.string = str;
}
public Integer getInteger()
{
return integer;
}
public void setInteger(Integer i)
{
this.integer = i;
}
public String toString()
{
String retStr = "";
if (null != this.string && this.string.length() > 0)
{
retStr = retStr + "string = " + this.string + "; ";
}
retStr = retStr + "integer = " + String.valueOf(this.integer) + ";";
return retStr;
}
}
public void simpleExampleOfSerializationDeserialization()
{
Object jsonObj = null;
try
{
MyBean beanObj = new MyBean();
beanObj.setString("Test");
beanObj.setInteger(new Integer(30));
// Convert String to JSON form.
jsonObj = JSONBeanSerializationHelper.toJSON(beanObj);
System.out.println("JSON Object received after serializing is: " + jsonObj.toString());
// This will print => JSON Object received after serializing is: {".type":"oracle.adfmf.test.MyBean","integer":30,"string":"Test"}
// Convert JSON back to String form.
MyBean beanObjReceived = (MyBean) JSONBeanSerializationHelper.fromJSON(MyBean.class, jsonObj);
System.out.println("MyBean class object received after deserializing is: " + beanObjReceived.toString());
// This will print => MyBean class object received after deserializing is: string = Test; integer = 30;
}
catch (Exception e)
{
// Handle exception
}
}
Constructor and Description |
---|
JSONBeanSerializationHelper() |
Modifier and Type | Method and Description |
---|---|
static Object |
fromJSON(Class type,
Object json)
Method used to deserialize the passed-in Object to Java object.
|
static Object |
fromJSON(Class type,
String jsonString)
Method used to deserialize the passed-in Object to Java object.
|
static boolean |
getBoolean(Object object)
Used to coerce an object into a boolean if possible.
|
static byte |
getByte(Object object)
Used to coerce an object into a byte if possible.
|
static char |
getCharacter(Object object)
Used to coerce an object into a char if possible.
|
static double |
getDouble(Object object)
Used to coerce an object into a double if possible.
|
static float |
getFloat(Object object)
Used to coerce an object into a float if possible.
|
static int |
getIdFromJSONObject(oracle.adfmf.json.JSONObject jsonObj)
Gets id from the json object and returns the corresponding int value
Internal Utility Function
|
static int |
getInteger(Object object)
Used to coerce an object into a int if possible.
|
static long |
getLong(Object object)
Used to coerce an object into a long if possible.
|
static oracle.adfmf.json.JSONObject |
getNullJSONObj()
Method to create and return JSON object representing null object.
|
static short |
getShort(Object object)
Used to coerce an object into a short if possible.
|
static String |
getString(Object object)
Used to coerce an object into a String if possible.
|
static boolean |
isObjectNull(Object json)
Utility method to determine if the JSON object is null.
|
static void |
removeBooleanFromJSON(oracle.adfmf.json.JSONObject jsonObj,
boolean bValue,
String[] keys)
Normalizes the JSON object by removing the objects which contain specified
boolean value.
|
static void |
removeNegativesFromJSON(oracle.adfmf.json.JSONObject jsonObj,
String[] keys)
Normalizes the JSON object by removing the objects which contain value: -1.
|
static void |
removeNullsFromJSON(oracle.adfmf.json.JSONObject jsonObj,
String[] keys)
Normalizes the JSON object by removing the objects which contain null's.
|
static Object |
toJSON(Object obj)
Method used to serialize the passed in Java object to JSON.
|
public static Object fromJSON(Class type, String jsonString) throws Exception
type
- Class to which JSON Object should be deserialized tojsonString
- String representing a JSON object.Exception
fromJSON(Class type, Object json)
public static Object fromJSON(Class type, Object json) throws Exception
Please refer to the following for more advanced ideas:
type
- Class to which JSON Object should be deserialized tojson
- Object to be deserializedException
JSONDeserializable
,
PostJSONDeserializable
,
JSONBeanSerializationHelper
,
ISO8601DateTimeUtil
public static Object toJSON(Object obj) throws Exception
Please refer to the following for more advanced ideas:
obj
- Object to be serializedException
JSONSerializable
,
PostJSONSerializable
public static oracle.adfmf.json.JSONObject getNullJSONObj() throws Exception
Exception
public static boolean isObjectNull(Object json)
public static void removeNullsFromJSON(oracle.adfmf.json.JSONObject jsonObj, String[] keys)
public static void removeNegativesFromJSON(oracle.adfmf.json.JSONObject jsonObj, String[] keys)
public static void removeBooleanFromJSON(oracle.adfmf.json.JSONObject jsonObj, boolean bValue, String[] keys)
public static int getIdFromJSONObject(oracle.adfmf.json.JSONObject jsonObj) throws oracle.adfmf.json.JSONException
oracle.adfmf.json.JSONException
public static boolean getBoolean(Object object) throws Exception
object
- to coerceException
public static byte getByte(Object object)
object
- to coerceException
public static char getCharacter(Object object)
object
- to coerceException
public static double getDouble(Object object)
object
- to coerceException
public static float getFloat(Object object)
object
- to coerceException
public static int getInteger(Object object)
object
- to coerceException
public static long getLong(Object object)
object
- to coerceException
public static short getShort(Object object)
object
- to coerceException
public static String getString(Object object)
object
- to coerceException