Author: dkatayev
Date: 2010-07-26 08:00:42 -0400 (Mon, 26 Jul 2010)
New Revision: 2811
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/JsonUtils.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-190 deserealisation added.
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-07-23
09:26:38 UTC (rev 2810)
+++
ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorImpl.java 2010-07-26
12:00:42 UTC (rev 2811)
@@ -18,17 +18,6 @@
*/
package org.exoplatform.ws.frameworks.json.impl;
-import org.exoplatform.ws.frameworks.json.JsonGenerator;
-import org.exoplatform.ws.frameworks.json.impl.JsonUtils.Types;
-import org.exoplatform.ws.frameworks.json.value.JsonValue;
-import org.exoplatform.ws.frameworks.json.value.impl.ArrayValue;
-import org.exoplatform.ws.frameworks.json.value.impl.BooleanValue;
-import org.exoplatform.ws.frameworks.json.value.impl.DoubleValue;
-import org.exoplatform.ws.frameworks.json.value.impl.LongValue;
-import org.exoplatform.ws.frameworks.json.value.impl.NullValue;
-import org.exoplatform.ws.frameworks.json.value.impl.ObjectValue;
-import org.exoplatform.ws.frameworks.json.value.impl.StringValue;
-
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -41,6 +30,17 @@
import java.util.Map;
import java.util.Set;
+import org.exoplatform.ws.frameworks.json.JsonGenerator;
+import org.exoplatform.ws.frameworks.json.impl.JsonUtils.Types;
+import org.exoplatform.ws.frameworks.json.value.JsonValue;
+import org.exoplatform.ws.frameworks.json.value.impl.ArrayValue;
+import org.exoplatform.ws.frameworks.json.value.impl.BooleanValue;
+import org.exoplatform.ws.frameworks.json.value.impl.DoubleValue;
+import org.exoplatform.ws.frameworks.json.value.impl.LongValue;
+import org.exoplatform.ws.frameworks.json.value.impl.NullValue;
+import org.exoplatform.ws.frameworks.json.value.impl.ObjectValue;
+import org.exoplatform.ws.frameworks.json.value.impl.StringValue;
+
/**
* @author <a href="mailto:andrew00x@gmail.com">Andrey
Parfonov</a>
* @version $Id: JsonGeneratorImpl.java 34417 2009-07-23 14:42:56Z dkatayev $
@@ -51,6 +51,132 @@
/**
* {@inheritDoc}
*/
+ public JsonValue createJsonArray(Collection<?> collection) throws JsonException
+ {
+ if (collection == null)
+ return new NullValue();
+
+ JsonValue jsonArray = new ArrayValue();
+ for (Object o : collection)
+ {
+ // If :
+ // 1. Known types (primitive, String, array of primitive or String)
+ // 2. Array of any object (expect for Java Bean)
+ // 3. Collection<?>
+ // 4. Map<String, ?>
+ if (JsonUtils.getType(o) != null)
+ jsonArray.addElement(createJsonValue(o));
+ else
+ jsonArray.addElement(createJsonObject(o));
+ }
+ return jsonArray;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public JsonValue createJsonArray(Object array) throws JsonException
+ {
+ if (array == null)
+ return new NullValue();
+
+ Types t = JsonUtils.getType(array);
+ JsonValue jsonArray = new ArrayValue();
+ int length = Array.getLength(array);
+ if (t == Types.ARRAY_BOOLEAN)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new BooleanValue(Array.getBoolean(array, i)));
+ }
+ else if (t == Types.ARRAY_BYTE)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new LongValue(Array.getByte(array, i)));
+ }
+ else if (t == Types.ARRAY_SHORT)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new LongValue(Array.getShort(array, i)));
+ }
+ else if (t == Types.ARRAY_INT)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new LongValue(Array.getInt(array, i)));
+ }
+ else if (t == Types.ARRAY_LONG)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new LongValue(Array.getLong(array, i)));
+ }
+ else if (t == Types.ARRAY_FLOAT)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new DoubleValue(Array.getFloat(array, i)));
+ }
+ else if (t == Types.ARRAY_DOUBLE)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new DoubleValue(Array.getDouble(array, i)));
+ }
+ else if (t == Types.ARRAY_CHAR)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new StringValue(Character.toString(Array.getChar(array,
i))));
+ }
+ else if (t == Types.ARRAY_STRING)
+ {
+ for (int i = 0; i < length; i++)
+ jsonArray.addElement(new StringValue((String)Array.get(array, i)));
+ }
+ else if (t == Types.ARRAY_OBJECT)
+ {
+ for (int i = 0; i < length; i++)
+ {
+ Object el = Array.get(array, i);
+ // If :
+ // 1. Known types (primitive, String, array of primitive or String)
+ // 2. Array of any object (expect for Java Bean)
+ // 3. Collection<?>
+ // 4. Map<String, ?>
+ if (JsonUtils.getType(el) != null)
+ jsonArray.addElement(createJsonValue(el));
+ else
+ jsonArray.addElement(createJsonObject(el));
+ }
+ }
+ else
+ {
+ throw new JsonException("Invalid argument, must be array.");
+ }
+ return jsonArray;
+ }
+
+ public JsonValue createJsonObject(Map<String, Object> map) throws JsonException
+ {
+ if (map == null)
+ return new NullValue();
+
+ JsonValue jsonObject = new ObjectValue();
+ Set<String> keys = map.keySet();
+ for (String k : keys)
+ {
+ Object o = map.get(k);
+ // If :
+ // 1. Known types (primitive, String, array of primitive or String)
+ // 2. Array of any object (expect for Java Bean)
+ // 3. Collection<?>
+ // 4. Map<String, ?>
+ if (JsonUtils.getType(o) != null)
+ jsonObject.addElement(k, createJsonValue(o));
+ else
+ jsonObject.addElement(k, createJsonObject(o));
+ }
+ return jsonObject;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public JsonValue createJsonObject(Object object) throws JsonException
{
Method[] methods = object.getClass().getMethods();
@@ -93,6 +219,11 @@
// Get result of invoke method get...
Object invokeResult = method.invoke(object, new Object[0]);
+ // If :
+ // 1. Known types (primitive, String, array of primitive or String)
+ // 2. Array of any object (expect for Java Bean)
+ // 3. Collection<?>
+ // 4. Map<String, ?>
if (JsonUtils.getType(invokeResult) != null)
{
jsonRootValue.addElement(key, createJsonValue(invokeResult));
@@ -125,7 +256,7 @@
* @throws JsonException if any errors occurs.
*/
@SuppressWarnings("unchecked")
- protected JsonValue createJsonValue(Object object) throws JsonException
+ private JsonValue createJsonValue(Object object) throws JsonException
{
Types t = JsonUtils.getType(object);
switch (t)
@@ -300,16 +431,14 @@
* @param clazz the class.
* @return list of fields which must be skiped.
*/
- private static List<String> getTransientFields(Class<?> clazz)
+ private List<String> getTransientFields(Class<?> clazz)
{
List<String> l = new ArrayList<String>();
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields)
{
if (Modifier.isTransient(f.getModifiers()))
- {
l.add(f.getName());
- }
}
return l;
}
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-07-23
09:26:38 UTC (rev 2810)
+++
ws/trunk/exo.ws.frameworks.json/src/main/java/org/exoplatform/ws/frameworks/json/impl/JsonUtils.java 2010-07-26
12:00:42 UTC (rev 2811)
@@ -30,128 +30,79 @@
public final class JsonUtils
{
- /**
- * Must not be created.
- */
+ /** Must not be created. */
private JsonUtils()
{
}
static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
- /**
- * Known types.
- */
public enum Types {
- /**
- * Byte.
- */
+ /** Byte. */
BYTE,
- /**
- * Short.
- */
+ /** Short. */
SHORT,
- /**
- * Integer.
- */
+ /** Integer. */
INT,
- /**
- * Long.
- */
+ /** Long. */
LONG,
- /**
- * Float.
- */
+ /** Float. */
FLOAT,
- /**
- * Double.
- */
+ /** Double. */
DOUBLE,
- /**
- * Boolean.
- */
+ /** Boolean. */
BOOLEAN,
- /**
- * Char.
- */
+ /** Char. */
CHAR,
- /**
- * String.
- */
+ /** String. */
STRING,
- /**
- * Corresponding to null value.
- */
+ /** Corresponding to null value. */
NULL,
- /**
- * Array of Bytes.
- */
+ /** Array of bytes. */
ARRAY_BYTE,
- /**
- * Array of Shorts.
- */
+ /** Array of shorts. */
ARRAY_SHORT,
- /**
- * Array of Integers.
- */
+ /** Array of ints. */
ARRAY_INT,
- /**
- * Array of Longs.
- */
+ /** Array of longs. */
ARRAY_LONG,
- /**
- * Array of Floats.
- */
+ /** Array of floats. */
ARRAY_FLOAT,
- /**
- * Array of Doubles.
- */
+ /** Array of doubles. */
ARRAY_DOUBLE,
- /**
- * Array of Booleans.
- */
+ /** Array of booleans. */
ARRAY_BOOLEAN,
- /**
- * Array of Chars.
- */
+ /** Array of chars. */
ARRAY_CHAR,
- /**
- * Array of Strings.
- */
+ /** Array of strings. */
ARRAY_STRING,
- /**
- * Array of Java Objects (beans).
- */
+ /** Array of Java Objects (beans). */
ARRAY_OBJECT,
- /**
- * Collection.
- */
+ /** Collection. */
COLLECTION,
- /**
- * Map.
- */
+ /** Map. */
MAP,
/**
@@ -164,24 +115,16 @@
* Types of Json tokens.
*/
public enum JsonToken {
- /**
- * JSON object, "key":{value1, ... } .
- */
+ /** JSON object, "key":{value1, ... } . */
object,
- /**
- * JSON array "key":[value1, ... ] .
- */
+ /** JSON array "key":[value1, ... ] . */
array,
- /**
- * Key.
- */
+ /** Key. */
key,
- /**
- * Value.
- */
+ /** Value. */
value
}
@@ -289,36 +232,55 @@
}
/**
- * Check is given Object is known.
+ * Check is given object has on of types:
+ * <ol>
+ * <li><code>null</code></li>
+ * <li>Primitive type</li>
+ * <li>Primitive type wrapper</li>
+ * <li>String</li>
+ * <li>Array of T where T satisfies 2 or 3 or 4</>
+ * </ol>
*
- * @param o Object.
- * @return true if Object is known, false otherwise.
+ * @param o Object
+ * @return <code>true</code> if Object has one of described above type,
+ * <code>false</code> otherwise
*/
public static boolean isKnownType(Object o)
{
- if (o == null)
- return true;
- return isKnownType(o.getClass());
+ return o == null || isKnownType(o.getClass());
}
/**
- * Check is given Class is known.
+ * Check is given class object represents:
+ * <ol>
+ * <li>Primitive type</li>
+ * <li>Primitive type wrapper</li>
+ * <li>String</li>
+ * <li>Array of T where T satisfies 1 or 2 or 3</>
+ * </ol>
*
- * @param clazz Class.
- * @return true if Class is known, false otherwise.
+ * @param clazz class.
+ * @return <code>true</code> if class object represent one of described
+ * above, <code>false</code> otherwise
*/
public static boolean isKnownType(Class<?> clazz)
{
- if (KNOWN_TYPES.get(clazz.getName()) != null)
- return true;
- return false;
+ return KNOWN_TYPES.get(clazz.getName()) != null;
}
/**
- * Get 'type' of Object. @see {@link KNOWN_TYPES} .
+ * Get corresponding {@link Types} for specified Object. If object is NOT :
+ * <ol>
+ * <li>Known type (primitive, String, array of primitive or String)</li>
+ * <li>Array</li>
+ * <li>Collection<?></li>
+ * <li>Map<String, ?></li>
+ * </ol>
+ * then <code>null</null> will be returned
*
* @param o Object.
- * @return 'type'.
+ * @return {@link Types} or <code>null</code> (see above)
+ * @see {@link KNOWN_TYPES}.
*/
public static Types getType(Object o)
{
@@ -338,10 +300,19 @@
}
/**
- * Get 'type' of Class. @see {@link KNOWN_TYPES} .
+ * Get corresponding {@link Types} for specified class. If class object is
+ * NOT :
+ * <ol>
+ * <li>Known type (primitive, String, array of primitive or String)</li>
+ * <li>Array</li>
+ * <li>Collection</li>
+ * <li>Map</li>
+ * </ol>
+ * then <code>null</null> will be returned
*
- * @param clazz Class.
- * @return 'type'.
+ * @param o Object.
+ * @return {@link Types} or <code>null</code> (see above)
+ * @see {@link KNOWN_TYPES}.
*/
public static Types getType(Class<?> clazz)
{
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-07-23
09:26:38 UTC (rev 2810)
+++
ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonGeneratorTest.java 2010-07-26
12:00:42 UTC (rev 2811)
@@ -30,6 +30,7 @@
import org.exoplatform.ws.frameworks.json.JavaMapBean;
import org.exoplatform.ws.frameworks.json.StringEnum;
import org.exoplatform.ws.frameworks.json.value.JsonValue;
+import org.exoplatform.ws.frameworks.json.value.impl.ArrayValue;
import java.util.ArrayList;
import java.util.Arrays;
@@ -151,7 +152,7 @@
assertEquals(l.get(2).getTitle(),
iterator.next().getElement("title").getStringValue());
}
- public void test2() throws Exception
+ public void testBeanWithCollections() throws Exception
{
JavaMapBean mb = new JavaMapBean();
Map<String, Book> m = new HashMap<String, Book>();
@@ -201,7 +202,7 @@
assertNotNull(jsonValue.getElement("mapList"));
assertEquals("JUnit in Action",
jsonValue.getElement("mapList").getElement("3").getElements().next().getElement(
"title").getStringValue());
- // System.out.println(jsonValue);
+ System.out.println(jsonValue);
}
public void testBeanWithTransientField() throws Exception
@@ -227,7 +228,7 @@
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);
+ System.out.println(jsonValue);
assertEquals("name",
jsonValue.getElement("name").getStringValue());
@@ -260,7 +261,7 @@
BeanWithBookEnum be = new BeanWithBookEnum();
be.setBook(BookEnum.JUNIT_IN_ACTION);
JsonValue jsonValue = new JsonGeneratorImpl().createJsonObject(be);
- //System.out.println(jsonValue);
+ 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-07-23
09:26:38 UTC (rev 2810)
+++
ws/trunk/exo.ws.frameworks.json/src/test/java/org/exoplatform/ws/frameworks/json/impl/JsonParserTest.java 2010-07-26
12:00:42 UTC (rev 2811)
@@ -31,6 +31,7 @@
import org.exoplatform.ws.frameworks.json.JsonParser;
import org.exoplatform.ws.frameworks.json.StringEnum;
import org.exoplatform.ws.frameworks.json.value.JsonValue;
+import org.exoplatform.ws.frameworks.json.value.impl.ArrayValue;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
@@ -272,5 +273,17 @@
BeanWithBookEnum o = (BeanWithBookEnum)new
BeanBuilder().createObject(BeanWithBookEnum.class, jsonValue);
assertEquals(BookEnum.BEGINNING_C, o.getBook());
}
+
+ public void testArray() throws Exception
+ {
+ String source =
"{\"array\":[\"a\",\"b\",\"c\"]}";
+ JsonParser parser = new JsonParserImpl();
+ JsonHandler jsonHandler = new JsonDefaultHandler();
+ parser.parse(new ByteArrayInputStream(source.getBytes()), jsonHandler);
+ JsonValue jsonValue = jsonHandler.getJsonObject();
+ System.out.println(jsonValue.getElement("array").isArray());
+
+
+ }
}