Package org.exoplatform.ws.frameworks.json.impl.JsonUtils

Examples of org.exoplatform.ws.frameworks.json.impl.JsonUtils.Types


      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)
                     {
View Full Code Here


    * @return Object.
    * @throws JsonException if type is unknown.
    */
   private static Object createObjectKnownTypes(Class<?> clazz, JsonValue jsonValue) throws JsonException
   {
      Types t = JsonUtils.getType(clazz);
      switch (t)
      {
         case NULL :
            return null;
         case BOOLEAN :
View Full Code Here

    * @throws JsonException if any errors occurs.
    */
   @SuppressWarnings("unchecked")
   protected JsonValue createJsonValue(Object object) throws JsonException
   {
      Types t = JsonUtils.getType(object);
      switch (t)
      {
         case NULL :
            return new NullValue();
         case BOOLEAN :
View Full Code Here

            {
               method.invoke(object, new Object[]{createObjectKnownTypes(methodParameterClazz, childJsonValue)});
            }
            else
            {
               Types type = JsonUtils.getType(methodParameterClazz);
               // other type Collection, Map or Object[].
               if (type != null)
               {
                  switch (type)
                  {
View Full Code Here

    * @return Object.
    * @throws JsonException if type is unknown.
    */
   private Object createObjectKnownTypes(Class<?> clazz, JsonValue jsonValue) throws JsonException
   {
      Types t = JsonUtils.getType(clazz);
      switch (t)
      {
         case NULL :
            return null;
         case BOOLEAN :
View Full Code Here

      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)
                     {
View Full Code Here

    * @return Object.
    * @throws JsonException if type is unknown.
    */
   private static Object createObjectKnownTypes(Class<?> clazz, JsonValue jsonValue) throws JsonException
   {
      Types t = JsonUtils.getType(clazz);
      switch (t)
      {
         case NULL :
            return null;
         case BOOLEAN :
View Full Code Here

   {
      if (array == null)
      {
         return new NullValue();
      }
      Types t = JsonUtils.getType(array);
      if (t == Types.ARRAY_BOOLEAN || t == Types.ARRAY_BYTE || t == Types.ARRAY_SHORT || t == Types.ARRAY_INT
         || t == Types.ARRAY_LONG || t == Types.ARRAY_FLOAT || t == Types.ARRAY_DOUBLE || t == Types.ARRAY_CHAR
         || t == Types.ARRAY_STRING || t == Types.ARRAY_OBJECT)
      {
         return createJsonValue(array);
View Full Code Here

    * @throws JsonException if any errors occurs.
    */
   @SuppressWarnings({"unchecked", "rawtypes"})
   private JsonValue createJsonValue(Object object) throws JsonException
   {
      Types type = JsonUtils.getType(object);
      switch (type)
      {
         case NULL :
            return new NullValue();
         case BOOLEAN :
View Full Code Here

TOP

Related Classes of org.exoplatform.ws.frameworks.json.impl.JsonUtils.Types

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.