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(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 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(JSONObject jsonObj,
                     boolean bValue,
                     String[] keys)Normalizes the JSON object by removing the objects which contain specified
 boolean value. | 
| static void | removeNegativesFromJSON(JSONObject jsonObj,
                       String[] keys)Normalizes the JSON object by removing the objects which contain value: -1. | 
| static void | removeNullsFromJSON(JSONObject jsonObj,
                   String[] keys)Normalizes the JSON object by removing the objects which contain null's. | 
| static void | replaceNullObjectWithNullLiteral(JSONObject root)An utility method to replace {".null":true} values with JSONObject.NULL
 literal. | 
| 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.ExceptionfromJSON(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 deserializedExceptionJSONDeserializable, 
PostJSONDeserializable, 
JSONBeanSerializationHelper, 
ISO8601DateTimeUtilpublic static Object toJSON(Object obj)
                     throws Exception
Please refer to the following for more advanced ideas:
obj - Object to be serializedExceptionJSONSerializable, 
PostJSONSerializablepublic static JSONObject getNullJSONObj() throws Exception
Exceptionpublic static boolean isObjectNull(Object json)
public static void removeNullsFromJSON(JSONObject jsonObj, String[] keys)
public static void replaceNullObjectWithNullLiteral(JSONObject root)
MAF treats null objects as special case values, and, represents them with a special JSON object {".null":true}. But sometimes it is required to have null literal in the JSON string for some backend REST services. For example: "some-key":null
By default, adding a null value will create {"some-key":{".null":true}}
This method can be used to convert all {".null":true} into null literals in entire tree of given JSON object.
Example code.
 Map dep = new HashMap();
 dep.put("id", "1");
 dep.put("name", null);
 JSONObject json = (JSONObject) JSONBeanSerializationHelper.toJSON(dep);
 System.out.println(json); // This prints {"id":"1", "name":{".null":true}}
 replaceNullObjectWithNullLiteral(json);
 System.out.println(json); // This prints {"id":"1", "name":null}
 root - the root JSON object that needs to be normalized.public static void removeNegativesFromJSON(JSONObject jsonObj, String[] keys)
public static void removeBooleanFromJSON(JSONObject jsonObj, boolean bValue, String[] keys)
public static int getIdFromJSONObject(JSONObject jsonObj) throws oracle.adfmf.json.JSONException
oracle.adfmf.json.JSONExceptionpublic static boolean getBoolean(Object object)
                          throws Exception
object - to coerceExceptionpublic static byte getByte(Object object)
object - to coerceExceptionpublic static char getCharacter(Object object)
object - to coerceExceptionpublic static double getDouble(Object object)
object - to coerceExceptionpublic static float getFloat(Object object)
object - to coerceExceptionpublic static int getInteger(Object object)
object - to coerceExceptionpublic static long getLong(Object object)
object - to coerceExceptionpublic static short getShort(Object object)
object - to coerceExceptionpublic static String getString(Object object)
object - to coerceException