You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/04/22 01:18:42 UTC
svn commit: r936563 - in /pivot/trunk:
core/src/org/apache/pivot/beans/BeanAdapter.java
core/src/org/apache/pivot/json/JSONSerializer.java
tests/src/org/apache/pivot/tests/JSONBeanTest.java
Author: gbrown
Date: Wed Apr 21 23:18:42 2010
New Revision: 936563
URL: http://svn.apache.org/viewvc?rev=936563&view=rev
Log:
Resolve PIVOT-467.
Added:
pivot/trunk/tests/src/org/apache/pivot/tests/JSONBeanTest.java
Modified:
pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java
pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
Modified: pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java?rev=936563&r1=936562&r2=936563&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BeanAdapter.java Wed Apr 21 23:18:42 2010
@@ -98,6 +98,10 @@ public class BeanAdapter implements Map<
int propertyOffset = prefix.length();
nextProperty = Character.toLowerCase(methodName.charAt(propertyOffset))
+ methodName.substring(propertyOffset + 1);
+
+ if (nextProperty.equals("class")) {
+ nextProperty = null;
+ }
}
if (nextProperty != null
@@ -210,7 +214,7 @@ public class BeanAdapter implements Map<
try {
value = field.get(bean);
} catch (IllegalAccessException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
}
} else {
@@ -220,9 +224,9 @@ public class BeanAdapter implements Map<
try {
value = getterMethod.invoke(bean, new Object[] {});
} catch (IllegalAccessException exception) {
- // No-op
+ throw new RuntimeException(exception);
} catch (InvocationTargetException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
}
}
@@ -606,7 +610,7 @@ public class BeanAdapter implements Map<
field = null;
}
} catch (NoSuchFieldException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
return field;
@@ -632,14 +636,14 @@ public class BeanAdapter implements Map<
try {
getterMethod = beanClass.getMethod(GET_PREFIX + key, new Class<?>[] {});
} catch (NoSuchMethodException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
if (getterMethod == null) {
try {
getterMethod = beanClass.getMethod(IS_PREFIX + key, new Class<?>[] {});
} catch (NoSuchMethodException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
}
@@ -670,7 +674,7 @@ public class BeanAdapter implements Map<
try {
setterMethod = beanClass.getMethod(methodName, new Class<?>[] {valueType});
} catch (NoSuchMethodException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
if (setterMethod == null) {
@@ -689,12 +693,12 @@ public class BeanAdapter implements Map<
try {
setterMethod = beanClass.getMethod(methodName, new Class<?>[] {primitiveValueType});
} catch (NoSuchMethodException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
} catch (NoSuchFieldException exception) {
- // No-op; not a wrapper type
+ throw new RuntimeException(exception);
} catch (IllegalAccessException exception) {
- // No-op
+ throw new RuntimeException(exception);
}
}
Modified: pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java?rev=936563&r1=936562&r2=936563&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java Wed Apr 21 23:18:42 2010
@@ -30,6 +30,7 @@ import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
+import org.apache.pivot.beans.BeanAdapter;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.HashMap;
import org.apache.pivot.collections.List;
@@ -653,8 +654,14 @@ public class JSONSerializer implements S
}
writer.append("]");
- } else if (object instanceof Map<?, ?>) {
- Map<String, Object> map = (Map<String, Object>)object;
+ } else {
+ Map<String, Object> map;
+ if (object instanceof Map<?, ?>) {
+ map = (Map<String, Object>)object;
+ } else {
+ map = new BeanAdapter(object);
+ }
+
writer.append("{");
int i = 0;
@@ -701,9 +708,6 @@ public class JSONSerializer implements S
}
writer.append("}");
- } else {
- throw new SerializationException(object.getClass()
- + " is not a supported type.");
}
writer.flush();
Added: pivot/trunk/tests/src/org/apache/pivot/tests/JSONBeanTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/JSONBeanTest.java?rev=936563&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/JSONBeanTest.java (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/JSONBeanTest.java Wed Apr 21 23:18:42 2010
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.tests;
+
+import org.apache.pivot.json.JSONSerializer;
+
+public class JSONBeanTest {
+ public static class A {
+ public String greeting = "Hello World";
+
+ private int number = 100;
+ private B b = new B();
+
+ public int getNumber() {
+ return number;
+ }
+
+ public B getB() {
+ return b;
+ }
+ }
+
+ public static class B {
+ private boolean flag = false;
+
+ public boolean getFlag() {
+ return flag;
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ A a = new A();
+ System.out.println(JSONSerializer.toString(a));
+ }
+}
+