[exo-jcr-commits] exo-jcr SVN: r2142 - in ws/trunk/exo.ws.frameworks.json/src: test/java/org/exoplatform/ws/frameworks/json and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Mar 25 12:33:34 EDT 2010


Author: aparfonov
Date: 2010-03-25 12:33:32 -0400 (Thu, 25 Mar 2010)
New Revision: 2142

Added:
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithBookEnum.java
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithSimpleEnum.java
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BookEnum.java
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/StringEnum.java
Modified:
   ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java
   ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java
   ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonUtils.java
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/Book.java
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorTest.java
   ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonParserTest.java
Log:
EXOJCR-602 : add java enum serialization

Modified: ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java	2010-03-25 16:14:57 UTC (rev 2141)
+++ ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/BeanBuilder.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -46,14 +46,22 @@
 
    /**
     * Create Java Bean from Json Source.
-    * 
+    *
     * @param clazz the Class of target Object.
     * @param jsonValue the Json representation.
     * @return Object.
     * @throws Exception if any errors occurs.
     */
+   @SuppressWarnings("unchecked")
    public Object createObject(Class<?> clazz, JsonValue jsonValue) throws Exception
    {
+      if (JsonUtils.getType(clazz) == Types.ENUM)
+      {
+         // Enum is not instantiable via CLass.getInstance().
+         // This is used when enum is member of array or collection.
+         Class c = clazz;
+         return Enum.valueOf(c, jsonValue.getStringValue());
+      }
       Object object = clazz.newInstance();
       Method[] methods = clazz.getMethods();
 
@@ -67,8 +75,10 @@
             Class<?>[] parameterTypes = method.getParameterTypes();
 
             if (parameterTypes.length != 1)
+            {
                throw new JsonException("Each set method must have one parameters, but method " + clazz.getName() + "#"
                   + method.getName() + " has " + parameterTypes.length);
+            }
 
             Class<?> methodParameterClazz = parameterTypes[0];
             String key = methodName.substring("set".length());
@@ -77,16 +87,22 @@
 
             // Bug : WS-53
             if (jsonValue.isNull())
+            {
                return null;
+            }
 
             if (!jsonValue.isObject())
+            {
                throw new JsonException("Unsupported type of jsonValue for parameter of method " + clazz.getName() + "#"
                   + method.getName());
+            }
 
             JsonValue childJsonValue = jsonValue.getElement(key);
 
             if (childJsonValue == null)
+            {
                continue;
+            }
 
             // if one of known primitive type or array of primitive type
             if (JsonUtils.isKnownType(methodParameterClazz))
@@ -101,6 +117,12 @@
                {
                   switch (type)
                   {
+                     case ENUM : {
+                        Class c = methodParameterClazz;
+                        Enum<?> en = Enum.valueOf(c, childJsonValue.getStringValue());
+                        method.invoke(object, new Object[]{en});
+                     }
+                        break;
                      case ARRAY_OBJECT : {
                         Object array = createArray(methodParameterClazz, childJsonValue);
                         method.invoke(object, new Object[]{array});
@@ -174,8 +196,10 @@
                            constructor = methodParameterClazz.getConstructor(new Class[]{Collection.class});
                         }
                         if (constructor == null)
+                        {
                            throw new JsonException("Can't find satisfied constructor for : " + methodParameterClazz
                               + ", method : " + clazz.getName() + "#" + method.getName());
+                        }
 
                         ArrayList<Object> sourceCollection = new ArrayList<Object>(childJsonValue.size());
 
@@ -185,9 +209,13 @@
                         {
                            JsonValue v = values.next();
                            if (!JsonUtils.isKnownType(parameterizedTypeClass))
+                           {
                               sourceCollection.add(createObject(parameterizedTypeClass, v));
+                           }
                            else
+                           {
                               sourceCollection.add(createObjectKnownTypes(parameterizedTypeClass, v));
+                           }
                         }
 
                         constructor.newInstance(sourceCollection);
@@ -204,8 +232,10 @@
                               ParameterizedType parameterizedType = (ParameterizedType)genericParameterTypes[0];
                               if (!String.class
                                  .isAssignableFrom((Class<?>)parameterizedType.getActualTypeArguments()[0]))
+                              {
                                  throw new JsonException("Unsupported parameter in method " + clazz.getName() + "#"
                                     + method.getName() + ". Key of Map must be String.");
+                              }
                               try
                               {
                                  parameterizedTypeClass = (Class<?>)parameterizedType.getActualTypeArguments()[1];
@@ -263,8 +293,10 @@
                         }
 
                         if (constructor == null)
+                        {
                            throw new JsonException("Can't find satisfied constructor for : " + methodParameterClazz
                               + ", method : " + clazz.getName() + "#" + method.getName());
+                        }
 
                         HashMap<String, Object> sourceMap = new HashMap<String, Object>(childJsonValue.size());
 
@@ -275,9 +307,13 @@
                            String k = keys.next();
                            JsonValue v = childJsonValue.getElement(k);
                            if (!JsonUtils.isKnownType(parameterizedTypeClass))
+                           {
                               sourceMap.put(k, createObject(parameterizedTypeClass, v));
+                           }
                            else
+                           {
                               sourceMap.put(k, createObjectKnownTypes(parameterizedTypeClass, v));
+                           }
                         }
 
                         method.invoke(object, constructor.newInstance(sourceMap));
@@ -303,8 +339,9 @@
    }
 
    /**
-    * Create array of Java Object from Json Source include multi-dimension array.
-    * 
+    * Create array of Java Object from Json Source include multi-dimension
+    * array.
+    *
     * @param clazz the Class of target Object.
     * @param jsonValue the Json representation.
     * @return Object.
@@ -350,7 +387,7 @@
 
    /**
     * Create Objects of known types.
-    * 
+    *
     * @param clazz class.
     * @param jsonValue JsonValue , @see {@link JsonValue}
     * @return Object.
@@ -387,7 +424,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getBooleanValue();
+            }
             return params;
          }
          case ARRAY_BYTE : {
@@ -395,7 +434,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getByteValue();
+            }
             return params;
          }
          case ARRAY_SHORT : {
@@ -403,7 +444,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getShortValue();
+            }
             return params;
          }
          case ARRAY_INT : {
@@ -411,7 +454,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getIntValue();
+            }
             return params;
          }
          case ARRAY_LONG : {
@@ -419,7 +464,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getLongValue();
+            }
             return params;
          }
          case ARRAY_FLOAT : {
@@ -427,7 +474,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getFloatValue();
+            }
             return params;
          }
          case ARRAY_DOUBLE : {
@@ -435,7 +484,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getDoubleValue();
+            }
             return params;
          }
          case ARRAY_CHAR : {
@@ -444,7 +495,9 @@
             int i = 0;
             // TODO better checking an transformation string to char
             while (values.hasNext())
+            {
                params[i++] = values.next().getStringValue().charAt(0);
+            }
             return params;
          }
          case ARRAY_STRING : {
@@ -452,7 +505,9 @@
             Iterator<JsonValue> values = jsonValue.getElements();
             int i = 0;
             while (values.hasNext())
+            {
                params[i++] = values.next().getStringValue();
+            }
             return params;
          }
          default :

Modified: ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java	2010-03-25 16:14:57 UTC (rev 2141)
+++ ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -94,9 +94,13 @@
                   Object invokeResult = method.invoke(object, new Object[0]);
 
                   if (JsonUtils.getType(invokeResult) != null)
+                  {
                      jsonRootValue.addElement(key, createJsonValue(invokeResult));
+                  }
                   else
+                  {
                      jsonRootValue.addElement(key, createJsonObject(invokeResult));
+                  }
 
                }
                catch (InvocationTargetException e)
@@ -115,7 +119,7 @@
 
    /**
     * Create JsonValue corresponding to Java object.
-    * 
+    *
     * @param object source object.
     * @return JsonValue.
     * @throws JsonException if any errors occurs.
@@ -146,67 +150,87 @@
             return new StringValue(Character.toString((Character)object));
          case STRING :
             return new StringValue((String)object);
+         case ENUM:
+            return new StringValue(((Enum)object).name());
          case ARRAY_BOOLEAN : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new BooleanValue(Array.getBoolean(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_BYTE : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new LongValue(Array.getByte(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_SHORT : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new LongValue(Array.getShort(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_INT : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new LongValue(Array.getInt(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_LONG : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new LongValue(Array.getLong(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_FLOAT : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new DoubleValue(Array.getFloat(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_DOUBLE : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new DoubleValue(Array.getDouble(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_CHAR : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new StringValue(Character.toString(Array.getChar(object, i))));
+            }
             return jsonArray;
          }
          case ARRAY_STRING : {
             JsonValue jsonArray = new ArrayValue();
             int length = Array.getLength(object);
             for (int i = 0; i < length; i++)
+            {
                jsonArray.addElement(new StringValue((String)Array.get(object, i)));
+            }
             return jsonArray;
          }
          case ARRAY_OBJECT : {
@@ -216,9 +240,13 @@
             {
                Object el = Array.get(object, i);
                if (JsonUtils.getType(el) != null)
+               {
                   jsonArray.addElement(createJsonValue(el));
+               }
                else
+               {
                   jsonArray.addElement(createJsonObject(el));
+               }
             }
 
             return jsonArray;
@@ -229,9 +257,13 @@
             for (Object o : list)
             {
                if (JsonUtils.getType(o) != null)
+               {
                   jsonArray.addElement(createJsonValue(o));
+               }
                else
+               {
                   jsonArray.addElement(createJsonObject(o));
+               }
             }
 
             return jsonArray;
@@ -244,9 +276,13 @@
             {
                Object o = map.get(k);
                if (JsonUtils.getType(o) != null)
+               {
                   jsonObject.addElement(k, createJsonValue(o));
+               }
                else
+               {
                   jsonObject.addElement(k, createJsonObject(o));
+               }
             }
 
             return jsonObject;
@@ -258,9 +294,9 @@
    }
 
    /**
-    * Check fields in class which marked as 'transient'. Transient fields will be
-    * not serialized in JSON representation.
-    * 
+    * Check fields in class which marked as 'transient'. Transient fields will
+    * be not serialized in JSON representation.
+    *
     * @param clazz the class.
     * @return list of fields which must be skiped.
     */

Modified: ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonUtils.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonUtils.java	2010-03-25 16:14:57 UTC (rev 2141)
+++ ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonUtils.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -152,7 +152,12 @@
       /**
        * Map.
        */
-      MAP
+      MAP,
+
+      /**
+       * Enum.
+       */
+      ENUM
    }
 
    /**
@@ -321,6 +326,8 @@
          return Types.NULL;
       if (KNOWN_TYPES.get(o.getClass().getName()) != null)
          return KNOWN_TYPES.get(o.getClass().getName());
+      if (o instanceof Enum)
+         return Types.ENUM;
       if (o instanceof Object[])
          return Types.ARRAY_OBJECT;
       if (o instanceof Collection)
@@ -340,6 +347,8 @@
    {
       if (KNOWN_TYPES.get(clazz.getName()) != null)
          return KNOWN_TYPES.get(clazz.getName());
+      if (Enum.class.isAssignableFrom(clazz))
+         return Types.ENUM;
       if (clazz.isArray())
          return Types.ARRAY_OBJECT;
       if (Collection.class.isAssignableFrom(clazz))

Added: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithBookEnum.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithBookEnum.java	                        (rev 0)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithBookEnum.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -0,0 +1,41 @@
+/**
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.ws.frameworks.json;
+
+/**
+ * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class BeanWithBookEnum
+{
+
+   private BookEnum book;
+
+   public BookEnum getBook()
+   {
+      return book;
+   }
+
+   public void setBook(BookEnum book)
+   {
+      this.book = book;
+   }
+
+}


Property changes on: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithBookEnum.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithSimpleEnum.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithSimpleEnum.java	                        (rev 0)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithSimpleEnum.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -0,0 +1,78 @@
+/**
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.ws.frameworks.json;
+
+import java.util.List;
+
+
+/**
+ * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public class BeanWithSimpleEnum
+{
+   private String name;
+
+   private StringEnum count;
+
+   private StringEnum[] counts;
+
+   private List<StringEnum> countList;
+
+   public StringEnum getCount()
+   {
+      return count;
+   }
+
+   public List<StringEnum> getCountList()
+   {
+      return countList;
+   }
+
+   public StringEnum[] getCounts()
+   {
+      return counts;
+   }
+
+   public String getName()
+   {
+      return name;
+   }
+
+   public void setCount(StringEnum count)
+   {
+      this.count = count;
+   }
+
+   public void setCountList(List<StringEnum> countList)
+   {
+      this.countList = countList;
+   }
+
+   public void setCounts(StringEnum[] counts)
+   {
+      this.counts = counts;
+   }
+
+   public void setName(String name)
+   {
+      this.name = name;
+   }
+}
\ No newline at end of file


Property changes on: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BeanWithSimpleEnum.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/Book.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/Book.java	2010-03-25 16:14:57 UTC (rev 2141)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/Book.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -39,6 +39,21 @@
 
    private boolean delivery;
 
+   public Book()
+   {
+   }
+
+   public Book(String author, String title, double price, long isdn, int pages, boolean availability, boolean delivery)
+   {
+      this.author = author;
+      this.title = title;
+      this.price = price;
+      this.isdn = isdn;
+      this.pages = pages;
+      this.availability = availability;
+      this.delivery = delivery;
+   }
+
    public void setAuthor(String s)
    {
       author = s;

Added: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BookEnum.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BookEnum.java	                        (rev 0)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BookEnum.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -0,0 +1,45 @@
+/**
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.ws.frameworks.json;
+
+
+/**
+ * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public enum BookEnum {
+
+   JUNIT_IN_ACTION(new Book("Vincent Masson", "JUnit in Action", 19.37, 93011099534534L, 386, true, false)), //
+   BEGINNING_C(new Book("Christian Gross", "Beginning C# 2008 from novice to professional", 23.56, 9781590598696L, 511,
+      false, false));
+
+   private final Book book;
+
+   private BookEnum(Book book)
+   {
+      this.book = book;
+   }
+
+   @Override
+   public String toString()
+   {
+      return book.toString();
+   }
+}


Property changes on: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/BookEnum.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/StringEnum.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/StringEnum.java	                        (rev 0)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/StringEnum.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -0,0 +1,44 @@
+/**
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.ws.frameworks.json;
+
+/**
+ * @author <a href="mailto:andrew00x at gmail.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public enum StringEnum {
+
+   ONE("one"), //
+   TWO("two"), //
+   TREE("tree");
+
+   private final String count;
+
+   private StringEnum(String count)
+   {
+      this.count = count;
+   }
+
+   @Override
+   public String toString()
+   {
+      return count;
+   }
+}
\ No newline at end of file


Property changes on: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/StringEnum.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorTest.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorTest.java	2010-03-25 16:14:57 UTC (rev 2141)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorTest.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -20,14 +20,19 @@
 
 import junit.framework.TestCase;
 
+import org.exoplatform.ws.frameworks.json.BeanWithBookEnum;
+import org.exoplatform.ws.frameworks.json.BeanWithSimpleEnum;
 import org.exoplatform.ws.frameworks.json.BeanWithTransientField;
 import org.exoplatform.ws.frameworks.json.Book;
+import org.exoplatform.ws.frameworks.json.BookEnum;
 import org.exoplatform.ws.frameworks.json.BookStorage;
 import org.exoplatform.ws.frameworks.json.BookWrapper;
 import org.exoplatform.ws.frameworks.json.JavaMapBean;
+import org.exoplatform.ws.frameworks.json.StringEnum;
 import org.exoplatform.ws.frameworks.json.value.JsonValue;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -214,4 +219,49 @@
       }
    }
 
+   public void testEnumSerialization() throws Exception
+   {
+      BeanWithSimpleEnum be = new BeanWithSimpleEnum();
+      be.setName("name");
+      be.setCount(StringEnum.TWO);
+      be.setCounts(new StringEnum[]{StringEnum.ONE, StringEnum.TWO});
+      be.setCountList(Arrays.asList(StringEnum.ONE, StringEnum.TWO, StringEnum.TREE));
+      JsonValue jsonValue = new JsonGeneratorImpl().createJsonObject(be);
+      //System.out.println(jsonValue);
+
+      assertEquals("name", jsonValue.getElement("name").getStringValue());
+
+      assertEquals(StringEnum.TWO.name(), jsonValue.getElement("count").getStringValue());
+
+      JsonValue countValues = jsonValue.getElement("counts");
+      List<String> tmp = new ArrayList<String>();
+      for (Iterator<JsonValue> counts = countValues.getElements(); counts.hasNext();)
+      {
+         tmp.add(counts.next().getStringValue());
+      }
+      assertEquals(2, tmp.size());
+      assertTrue(tmp.contains(StringEnum.ONE.name()));
+      assertTrue(tmp.contains(StringEnum.TWO.name()));
+
+      JsonValue countListValues = jsonValue.getElement("countList");
+      tmp = new ArrayList<String>();
+      for (Iterator<JsonValue> counts = countListValues.getElements(); counts.hasNext();)
+      {
+         tmp.add(counts.next().getStringValue());
+      }
+      assertEquals(3, tmp.size());
+      assertTrue(tmp.contains(StringEnum.ONE.name()));
+      assertTrue(tmp.contains(StringEnum.TWO.name()));
+      assertTrue(tmp.contains(StringEnum.TREE.name()));
+   }
+
+   public void testEnumSerialization2() throws Exception
+   {
+      BeanWithBookEnum be = new BeanWithBookEnum();
+      be.setBook(BookEnum.JUNIT_IN_ACTION);
+      JsonValue jsonValue = new JsonGeneratorImpl().createJsonObject(be);
+      //System.out.println(jsonValue);
+      assertEquals(BookEnum.JUNIT_IN_ACTION.name(), jsonValue.getElement("book").getStringValue());
+   }
+
 }

Modified: ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonParserTest.java
===================================================================
--- ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonParserTest.java	2010-03-25 16:14:57 UTC (rev 2141)
+++ ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonParserTest.java	2010-03-25 16:33:32 UTC (rev 2142)
@@ -20,17 +20,22 @@
 
 import junit.framework.TestCase;
 
+import org.exoplatform.ws.frameworks.json.BeanWithBookEnum;
+import org.exoplatform.ws.frameworks.json.BeanWithSimpleEnum;
 import org.exoplatform.ws.frameworks.json.Book;
+import org.exoplatform.ws.frameworks.json.BookEnum;
 import org.exoplatform.ws.frameworks.json.BookStorage;
 import org.exoplatform.ws.frameworks.json.JavaCollectionBean;
 import org.exoplatform.ws.frameworks.json.JavaMapBean;
 import org.exoplatform.ws.frameworks.json.JsonHandler;
 import org.exoplatform.ws.frameworks.json.JsonParser;
+import org.exoplatform.ws.frameworks.json.StringEnum;
 import org.exoplatform.ws.frameworks.json.value.JsonValue;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -226,4 +231,46 @@
          .getElements().next().getElement("title").getStringValue());
    }
 
+   public void testEnumSerialization() throws Exception
+   {
+      String source =
+         "{\"countList\":[\"ONE\",\"TWO\",\"TREE\"], \"name\":\"andrew\",\"count\":\"TREE\",\"counts\":[\"TWO\",\"TREE\"]}";
+      JsonParser parser = new JsonParserImpl();
+      JsonHandler jsonHandler = new JsonDefaultHandler();
+      parser.parse(new ByteArrayInputStream(source.getBytes()), jsonHandler);
+      JsonValue jsonValue = jsonHandler.getJsonObject();
+      //      System.out.println(jsonValue);
+
+      BeanWithSimpleEnum o = (BeanWithSimpleEnum)new BeanBuilder().createObject(BeanWithSimpleEnum.class, jsonValue);
+
+      assertEquals("andrew", o.getName());
+
+      assertEquals(StringEnum.TREE, o.getCount());
+
+      StringEnum[] counts = o.getCounts();
+      assertEquals(2, counts.length);
+
+      List<StringEnum> tmp = Arrays.asList(counts);
+      assertTrue(tmp.contains(StringEnum.TWO));
+      assertTrue(tmp.contains(StringEnum.TREE));
+
+      tmp = o.getCountList();
+      assertEquals(3, tmp.size());
+      assertTrue(tmp.contains(StringEnum.ONE));
+      assertTrue(tmp.contains(StringEnum.TWO));
+      assertTrue(tmp.contains(StringEnum.TREE));
+   }
+
+   public void testEnumSerialization2() throws Exception
+   {
+      String source = "{\"book\":\"BEGINNING_C\"}";
+      JsonParser parser = new JsonParserImpl();
+      JsonHandler jsonHandler = new JsonDefaultHandler();
+      parser.parse(new ByteArrayInputStream(source.getBytes()), jsonHandler);
+      JsonValue jsonValue = jsonHandler.getJsonObject();
+      //System.out.println(jsonValue);
+      BeanWithBookEnum o = (BeanWithBookEnum)new BeanBuilder().createObject(BeanWithBookEnum.class, jsonValue);
+      assertEquals(BookEnum.BEGINNING_C, o.getBook());
+   }
+
 }



More information about the exo-jcr-commits mailing list