org.xerial.util.bean
Class BeanUtil
java.lang.Object
org.xerial.util.bean.BeanUtil
public class BeanUtil
- extends Object
BeanUtil class supports data binding between JSON data and a bean class.
A bean class must have the followings: - A public default (no argument)
constructor - To bind data object to a class, there must be public
getter/seter methods in the target class, prefixd with get/set. -- Allowable
data classes are: ---- primitive types: int/Integer, double/Double,
float/Float, boolean/Boolean, String ---- bean classes ---- Collection of
allowable data classes. e.g. List, Set, etc. ---- Map with
key and value classes. key and value also must be allowable classes. ------
to bind JSON data to map object, putSomething() method is requried. See the
following description.
For example, a Person class with integer id and string name values must have
public int getId() and public String getName(),
and public void setId(int id) and
public void setName(String name) methods.
Usage Example.
class Person {
int id;
String name;
public Person();
public Person(int id, String name)
{ this.id = id; this.name = name; }
public int getId() { return id; }
public String getName() { return name; }
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
}
Person p1 = new Person(1, "leo");
String json = BeanUtil.toJSON(p1); // it will give a json string { "id" : 1, "name" : "leo"}
Person p2 = new Person() // empty data
BeanUtil.populate(p2, json); // fill p2 with a give json data
// p2.id = 1, p2.name = "leo"
In the GWT (Google Web Toolkit) of the current version (1.3.3, Mar 2007), the
generics feature of Jave (since JDK1.5) cannot be used within the client Java
codes. This is a problematic since GWT cannot compile the following method:
void setPersonList(List personList) { this.personList = personList; }
So you have to change the argument of the avobe method as:
void setPersonList(List personList) { ... }
But, with this setter method, we have no information about the element type
contained in the List class. So given a JSON data, e.g.,{ "personList" : [
{"id":1, "name":"leo"}, {"id":2, "name":"taro"}] }, BeanUtil class cannot
instantiate any Person classes.
To resolve this problem, BeanUtil supports data binding via adder methods.
For example, instead of using setters, by using the follwing adder, you can
load several Person class data as a collection:
void addPersonList(Person person) { personList.add(person); }
Note that, in order to use adders correctly, the target of an adder, that is,
a collection class, must be initialized before the invocation of the adder.
For example,
public PersonList {
Vector personList = new Vector(); // it must be initialized.
public PersonList() {}
void addPersonList(Person p) { personList.add(p); }
public getPersonList() { return personList; }
}
Otherwise, BeanUtil.populate method cuases NullPointerException or some other
undisired effects.
In BeanUtil, a setter method, whose argument has no generic type parameter,
e.g., public void setList(List l) never be used to bind JSON
data to a class instance.
In order to use a Bean class with Map objects, you must define
public Map getSomething() and
public void putSomething(KeyType key, ValueType value)
methods, since BeanUtil learns class types K, V from the putSomething(K key,
V value) methods. If a method public void setSomething(Map map)
exists, BeanUtil simply ignores it.
The following is an example of a bean class with Map object.
public MapBean
{
Map entry = new TreeMap();
public MapBean(){}
public void putEntry(Key key, Value v)
{
entry.put(key, value);
}
public Map getEntry()
{
return entry;
}
}
- Author:
- leo
|
Method Summary |
static Object |
createBeanFromJSON(Class beanType,
Reader jsonReader)
|
static Object |
createBeanFromJSON(Class beanType,
String json)
|
static Object |
createInstance(Class c)
|
static
|
createXMLBean(Class<E> valueType,
Reader xmlReader)
|
static Object |
createXMLBean(Class valueType,
String xmlData)
|
static BeanBinderSet |
getBeanLoadRule(Class c)
|
static BeanBinderSet |
getBeanOutputRule(Class c)
|
static Pair<Class,Class> |
getGenericMapTypesOfMethodArgument(Method method,
int argIndex)
|
static ParameterizedType |
getParameterizedType(Type t)
|
static ParameterizedType |
getParentParameterizedType(Type t,
Class target)
|
static JSONValue |
getValue(Object bean,
String propertyName)
|
static org.xerial.util.bean.BinderSet |
inspectSetter(Class beanClass)
|
static
|
loadJSON(Reader jsonReader,
Class<T> beanClass,
BeanHandler<T> beanHandler)
|
protected static void |
populateBean(Object bean,
JSONArray jsonArray)
|
static void |
populateBean(Object bean,
JSONObject json)
fill a bean class with a given JSONObject data |
protected static void |
populateBeanWithJSON(Object bean,
Object jsonValue)
|
static void |
populateBeanWithJSON(Object bean,
Reader jsonReader)
|
static void |
populateBeanWithJSON(Object bean,
String jsonData)
fill a bean class with a given JSON data |
static void |
populateBeanWithMap(Object bean,
Map<?,?> map)
|
static void |
populateBeanWithXML(Object bean,
Element xmlElement)
|
static void |
populateBeanWithXML(Object bean,
Reader xmlReader)
|
static void |
populateBeanWithXML(Object bean,
String xmlData)
|
static String |
toJSON(Object bean)
|
static String |
toJSONFromResultSet(ResultSet resultSet)
|
static JSONObject |
toJSONObject(Collection collection)
|
static JSONObject |
toJSONObject(Object bean)
|
static String |
toXML(String tagName,
Object bean)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
getBeanOutputRule
public static BeanBinderSet getBeanOutputRule(Class c)
throws BeanException
- Throws:
BeanException
getBeanLoadRule
public static BeanBinderSet getBeanLoadRule(Class c)
throws BeanException
- Throws:
BeanException
inspectSetter
public static org.xerial.util.bean.BinderSet inspectSetter(Class beanClass)
throws BeanException
- Throws:
BeanException
getGenericMapTypesOfMethodArgument
public static Pair<Class,Class> getGenericMapTypesOfMethodArgument(Method method,
int argIndex)
getParameterizedType
public static ParameterizedType getParameterizedType(Type t)
getParentParameterizedType
public static ParameterizedType getParentParameterizedType(Type t,
Class target)
toXML
public static String toXML(String tagName,
Object bean)
throws BeanException
- Throws:
BeanException
toJSONFromResultSet
public static String toJSONFromResultSet(ResultSet resultSet)
throws SQLException
- Throws:
SQLException
toJSON
public static String toJSON(Object bean)
throws BeanException
- Throws:
BeanException
toJSONObject
public static JSONObject toJSONObject(Object bean)
throws BeanException
- Throws:
BeanException
toJSONObject
public static JSONObject toJSONObject(Collection collection)
throws BeanException
- Throws:
BeanException
getValue
public static JSONValue getValue(Object bean,
String propertyName)
throws BeanException
- Throws:
BeanException
populateBeanWithMap
public static void populateBeanWithMap(Object bean,
Map<?,?> map)
throws XerialException
- Throws:
XerialException
populateBeanWithXML
public static void populateBeanWithXML(Object bean,
Reader xmlReader)
throws BeanException
- Throws:
BeanException
populateBeanWithXML
public static void populateBeanWithXML(Object bean,
String xmlData)
throws BeanException
- Throws:
BeanException
populateBeanWithXML
public static void populateBeanWithXML(Object bean,
Element xmlElement)
throws BeanException
- Throws:
BeanException
populateBeanWithJSON
public static void populateBeanWithJSON(Object bean,
Reader jsonReader)
throws BeanException,
IOException
- Throws:
BeanException
IOException
populateBeanWithJSON
public static void populateBeanWithJSON(Object bean,
String jsonData)
throws BeanException
- fill a bean class with a given JSON data
- Parameters:
bean - a bean classjsonData - a string representation of a JSON data
- Throws:
InvalidJSONDataException - when the input json data is invalid (cannot interpret as a
JSON object)
InvalidBeanException - when a bean class has invalid structure
BeanException
populateBean
public static void populateBean(Object bean,
JSONObject json)
throws BeanException
- fill a bean class with a given JSONObject data
- Parameters:
bean - a bean classjson - a JSONOBject
- Throws:
InvalidBeanException
InvalidJSONDataException
IllegalAccessException
InstantiationException
BeanException
populateBean
protected static void populateBean(Object bean,
JSONArray jsonArray)
throws BeanException
- Throws:
BeanException
populateBeanWithJSON
protected static void populateBeanWithJSON(Object bean,
Object jsonValue)
throws BeanException
- Throws:
BeanException
createBeanFromJSON
public static Object createBeanFromJSON(Class beanType,
Reader jsonReader)
throws IOException,
BeanException
- Throws:
IOException
BeanException
createBeanFromJSON
public static Object createBeanFromJSON(Class beanType,
String json)
throws BeanException
- Throws:
BeanException
createInstance
public static Object createInstance(Class c)
throws BeanException
- Throws:
BeanException
createXMLBean
public static <E> E createXMLBean(Class<E> valueType,
Reader xmlReader)
throws BeanException,
IOException,
BeanException
- Throws:
BeanException
IOException
createXMLBean
public static Object createXMLBean(Class valueType,
String xmlData)
throws BeanException
- Throws:
BeanException
loadJSON
public static <T> void loadJSON(Reader jsonReader,
Class<T> beanClass,
BeanHandler<T> beanHandler)
throws IOException,
BeanException
- Throws:
IOException
BeanException

This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Japan License.