if (jsonValue == null || jsonValue.isNull())
{
return null;
}
Types type = JsonUtils.getType(clazz);
if (type == Types.ENUM)
{
// Enum is not instantiable via CLass.getInstance().
// This is used when enum is member of array or collection.
Class c = clazz;
return (T)Enum.valueOf(c, jsonValue.getStringValue());
}
if (!jsonValue.isObject())
{
throw new JsonException("Unsupported type of jsonValue. ");
}
T object = null;
try
{
object = clazz.newInstance();
}
catch (ExceptionInInitializerError e)
{
throw new JsonException("Unable instantiate object. " + e.getMessage(), e);
}
catch (SecurityException e)
{
throw new JsonException("Unable instantiate object. " + e.getMessage(), e);
}
catch (IllegalAccessException e)
{
throw new JsonException("Unable instantiate object. " + e.getMessage(), e);
}
catch (InstantiationException e)
{
throw new JsonException("Unable instantiate object. " + e.getMessage(), e);
}
Method[] methods = clazz.getMethods();
for (Method method : methods)
{
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
// 3 is length of prefix 'set'
if (!SKIP_METHODS.contains(methodName) && methodName.startsWith("set") && parameterTypes.length == 1
&& methodName.length() > 3)
{
Class<?> methodParameterClass = parameterTypes[0];
// 3 is length of prefix 'set'
String key = methodName.substring(3);
// first letter to lower case
key = (key.length() > 1) ? Character.toLowerCase(key.charAt(0)) + key.substring(1) : key.toLowerCase();
JsonValue childJsonValue = jsonValue.getElement(key);
if (childJsonValue == null)
{
continue;
}
// if one of known primitive type or array of primitive type
try
{
if (JsonUtils.isKnownType(methodParameterClass))
{
method.invoke(object, new Object[]{createObjectKnownTypes(methodParameterClass, childJsonValue)});
}
else
{
Types parameterType = JsonUtils.getType(methodParameterClass);
// other type Collection, Map or Object[].
if (parameterType != null)
{
if (parameterType == Types.ENUM)
{