Oracle Fusion Middleware Java API Reference for Oracle Mobile Application Framework
2.0.0.0.0

E36392-01

oracle.adfmf.framework.api
Class JSONBeanSerializationHelper

Object
  extended by oracle.adfmf.framework.api.JSONBeanSerializationHelper

public class JSONBeanSerializationHelper
extends Object

JSONBeanSerializationHelper will (de)serialize objects in and out of a JSON object.

The following classes are used to support this functionality:

Supported types:

See Also:
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 Summary
JSONBeanSerializationHelper()
           
 
Method Summary
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.
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONBeanSerializationHelper

public JSONBeanSerializationHelper()
Method Detail

fromJSON

public static Object fromJSON(Class type,
                              String jsonString)
                       throws Exception
Method used to deserialize the passed-in Object to Java object.

Parameters:
type - Class to which JSON Object should be deserialized to
jsonString - String representing a JSON object.
Throws:
Exception
See Also:
fromJSON(Class type, Object json)

fromJSON

public static Object fromJSON(Class type,
                              Object json)
                       throws Exception
Method used to deserialize the passed-in Object to Java object.

Please refer to the following for more advanced ideas:

The basic deserialize method provide the following out of the box conversions: For additional details, please refer to JSONBeanSerializationHelper for details of all object types.

Parameters:
type - Class to which JSON Object should be deserialized to
json - Object to be deserialized
Returns:
Java Object for the corresponding object passed in. (This java object is of type 'class' passed in.)
Throws:
Exception
See Also:
JSONDeserializable, PostJSONDeserializable, JSONBeanSerializationHelper, ISO8601DateTimeUtil

toJSON

public static Object toJSON(Object obj)
                     throws Exception
Method used to serialize the passed in Java object to JSON.

Please refer to the following for more advanced ideas:

The basic deserialize method provide the following out of the box conversions: For additional details, please refer to JSONBeanSerializationHelper for details of all object types.

Parameters:
obj - Object to be serialized
Returns:
Object
Throws:
Exception
See Also:
JSONSerializable, PostJSONSerializable

getNullJSONObj

public static oracle.adfmf.json.JSONObject getNullJSONObj()
                                                   throws Exception
Method to create and return JSON object representing null object.

Returns:
JSONObject representing null
Throws:
Exception

isObjectNull

public static boolean isObjectNull(Object json)
Utility method to determine if the JSON object is null. Internal Utility Function


removeNullsFromJSON

public static void removeNullsFromJSON(oracle.adfmf.json.JSONObject jsonObj,
                                       String[] keys)
Normalizes the JSON object by removing the objects which contain null's. Internal Utility Function


removeNegativesFromJSON

public static void removeNegativesFromJSON(oracle.adfmf.json.JSONObject jsonObj,
                                           String[] keys)
Normalizes the JSON object by removing the objects which contain value: -1. Internal Utility Function


removeBooleanFromJSON

public 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. Internal Utility Function


getIdFromJSONObject

public static int getIdFromJSONObject(oracle.adfmf.json.JSONObject jsonObj)
                               throws oracle.adfmf.json.JSONException
Gets id from the json object and returns the corresponding int value Internal Utility Function

Throws:
oracle.adfmf.json.JSONException

getBoolean

public static boolean getBoolean(Object object)
                          throws Exception
Used to coerce an object into a boolean if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getByte

public static byte getByte(Object object)
Used to coerce an object into a byte if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getCharacter

public static char getCharacter(Object object)
Used to coerce an object into a char if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getDouble

public static double getDouble(Object object)
Used to coerce an object into a double if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getFloat

public static float getFloat(Object object)
Used to coerce an object into a float if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getInteger

public static int getInteger(Object object)
Used to coerce an object into a int if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getLong

public static long getLong(Object object)
Used to coerce an object into a long if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getShort

public static short getShort(Object object)
Used to coerce an object into a short if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

getString

public static String getString(Object object)
Used to coerce an object into a String if possible.

Parameters:
object - to coerce
Returns:
the coerced value
Throws:
Exception

Oracle Fusion Middleware Java API Reference for Oracle Mobile Application Framework
2.0.0.0.0

E36392-01

Copyright © 2014 Oracle. All Rights Reserved.