pl.bristleback.server.bristle.utils
Class JsonUtil

java.lang.Object
  extended by pl.bristleback.server.bristle.utils.JsonUtil

public final class JsonUtil
extends java.lang.Object

Utility class providing convenient methods for retrieving JSONObject objects from beans and collections.

Created on: 2010-11-21 16:19:03

Author:
Wojciech Niemiec

Method Summary
static org.json.JSONArray getJsonArray(java.util.Collection collection, boolean deepSearch)
          Creates Json array from collection given as parameter.
static org.json.JSONArray getJsonArray(java.lang.Object[] beanArray, boolean deepSearch)
          Creates Json array from array given as parameter.
static org.json.JSONObject getJsonFromBean(java.lang.Object bean, boolean deepSearch)
          Creates Json object from bean.
static org.json.JSONObject getJsonFromBean(java.lang.Object bean, boolean deepSearch, java.util.List<Getter> getters)
          Creates Json object from bean, using getters specified by user.
static org.json.JSONObject getJsonObjectFromMap(java.util.Map<?,?> map, boolean deepSearch)
          Creates Json objects from map given as parameter.
static java.lang.Object processObject(java.lang.Object value, boolean deepSearch)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getJsonFromBean

public static org.json.JSONObject getJsonFromBean(java.lang.Object bean,
                                                  boolean deepSearch)
                                           throws org.json.JSONException
Creates Json object from bean. Every field with getter method in proper Bean convention will be processed. Optionally, deep search of fields can be performed. It means that if collection of objects or another bean will be found inside processed bean, they will be also processed into Json objects and placed in parent Json object. Those operations will be performed recursively, so it may cause infinite loop if not used correctly. See getJsonArray(java.util.Collection, boolean) to know how arrays and collection are processed into JSONArray.

Parameters:
bean - bean to process.
deepSearch - true if nested collections or beans should be processed into Json objects.
Returns:
JSONObject representing bean given as parameter.
Throws:
org.json.JSONException - thrown when Json cannot be created for some reason.

getJsonFromBean

public static org.json.JSONObject getJsonFromBean(java.lang.Object bean,
                                                  boolean deepSearch,
                                                  java.util.List<Getter> getters)
                                           throws org.json.JSONException
Creates Json object from bean, using getters specified by user. Optionally, deep search of fields can be performed. It means that if collection of objects or another bean will be found inside processed bean, they will be also processed into Json objects and placed in parent Json object. Those operations will be performed recursively, so it may cause infinite loop if not used correctly. See getJsonArray(java.util.Collection, boolean) to know how arrays and collection are processed into JSONArray.

Parameters:
bean - bean to process.
deepSearch - true if nested collections or beans should be processed into Json objects.
getters - list of properties that should be processed. Note that if deepSearch parameter is set to false, beans and collections will be not processed even if they are on getter list.
Returns:
JSONObject representing bean given as parameter.
Throws:
org.json.JSONException - thrown when Json cannot be created for some reason.

processObject

public static java.lang.Object processObject(java.lang.Object value,
                                             boolean deepSearch)
                                      throws org.json.JSONException
Throws:
org.json.JSONException

getJsonObjectFromMap

public static org.json.JSONObject getJsonObjectFromMap(java.util.Map<?,?> map,
                                                       boolean deepSearch)
                                                throws org.json.JSONException
Creates Json objects from map given as parameter. Map entries will be processed into corresponding Json objects with its key and value structure. Optionally, deep search of fields can be performed. It means that if collection of objects or another bean will be found inside processed entry value, they will be also processed into Json objects and placed in parent Json object. Those operations will be performed recursively, so it may cause infinite loop if not used correctly.

Parameters:
map - map to process.
deepSearch - true if nested collections or beans should be processed into Json objects.
Returns:
JSONObject with field name given as map keys and field values given as map values.
Throws:
org.json.JSONException - thrown when Json element cannot be created for some reason.

getJsonArray

public static org.json.JSONArray getJsonArray(java.util.Collection collection,
                                              boolean deepSearch)
                                       throws org.json.JSONException
Creates Json array from collection given as parameter. It is very important to know that in case when elements in this collection are beans, this method takes getters only from first element of collection, assuming that every element is an object of the same class. Optionally, deep search of fields can be performed. It means that if collection of objects or another bean will be found inside processed collection value, they will be also processed into Json objects and placed in parent Json object. Those operations will be performed recursively, so it may cause infinite loop if not used correctly.

Parameters:
collection - collection of elements that should be elements of Json array.
deepSearch - true if nested collections or beans should be processed into Json objects.
Returns:
JSONArray with elements represented by elements given in collection.
Throws:
org.json.JSONException - thrown when Json element cannot be created for some reason.

getJsonArray

public static org.json.JSONArray getJsonArray(java.lang.Object[] beanArray,
                                              boolean deepSearch)
                                       throws org.json.JSONException
Creates Json array from array given as parameter. It is very important to know that in case when elements in this array are beans, this method takes getters only from first index of array, assuming that every element is an object of the same class. Optionally, deep search of fields can be performed. It means that if collection of objects or another bean will be found inside processed array value, they will be also processed into Json objects and placed in parent Json object. Those operations will be performed recursively, so it may cause infinite loop if not used correctly.

Parameters:
beanArray - array that should be processed into elements of Json array.
deepSearch - true if nested collections or beans should be processed into Json objects.
Returns:
JSONArray with elements represented by elements given in array.
Throws:
org.json.JSONException - thrown when Json element cannot be created for some reason.


Copyright © 2011. All Rights Reserved.