package org.vxwo.java.json;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;

class JsonObjParser {
	private Object root;
	private SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	JsonObjParser(Object obj) {
		this.root = obj;
	}

	JsonValue Decode() throws JsonException {
		return ParseValue(root);
	}

	@SuppressWarnings("rawtypes")
	private JsonValue ParseValue(Object obj) throws JsonException {
		if (obj == null)
			return new JsonValue(JsonType.Null, null);

		Class type = obj.getClass();

		if (type.isArray()) {
			JsonValue child, result = new JsonValue(JsonType.Array, null);
			for (Object sub : (Object[]) obj) {
				child = ParseValue(sub);
				if (child != null)
					result.append(child);
			}
			return result;
		}
		if (type.getSimpleName().equalsIgnoreCase("Char"))
			return new JsonValue(JsonType.Int, (Integer) obj);
		if (type.getSimpleName().equalsIgnoreCase("Byte"))
			return new JsonValue(JsonType.Int, (Integer) obj);
		if (type.getSimpleName().equalsIgnoreCase("Boolean"))
			return new JsonValue(JsonType.Boolean, obj);
		if (type.getSimpleName().equalsIgnoreCase("Integer"))
			return new JsonValue(JsonType.Int, obj);
		if (type.getSimpleName().equalsIgnoreCase("Long"))
			return new JsonValue(JsonType.Long, obj);
		if (type.getSimpleName().equalsIgnoreCase("Float"))
			return new JsonValue(JsonType.Double, ((Float) obj).doubleValue());
		if (type.getSimpleName().equalsIgnoreCase("Double"))
			return new JsonValue(JsonType.Double, obj);
		if (type.getSimpleName().equals("String"))
			return new JsonValue(JsonType.String, obj);
		if (type.getSimpleName().equals("Date"))
			return new JsonValue(JsonType.String, dateFormat.format(obj));

		if (!type.getName().startsWith("java.")
				&& !type.getName().startsWith("javax.")) {
			JsonValue child, result = new JsonValue();
			for (Field info : type.getDeclaredFields()) {
				String generic = info.toGenericString();
				if (generic.indexOf(" static ") != -1
						|| generic.indexOf(" final ") != -1)
					continue;
				String name = info.getName();
				try {
					child = ParseValue(info.get(obj));
					if (child != null)
						result.setMember(name, child);
				} catch (IllegalArgumentException e) {
				} catch (IllegalAccessException e) {
				}
			}
			return result;
		}

		throw new JsonException("JsonObjParser not support type: "
				+ type.getSimpleName());
	}
}

@@ -22,16 +22,48 @@
 .json;%0A%0A
+import java.lang.reflect.Array;%0A
 import j
@@ -677,35 +677,60 @@
 or (
-Object sub : (Object%5B%5D) obj
+int index = 0; index %3C Array.getLength(obj); ++index
 ) %7B%0A
@@ -756,11 +756,29 @@
 lue(
-sub
+Array.get(obj, index)
 );%0A%09

Fix bug from byte[] to Object[]