try {
for (PropertyDescriptor pd : Introspector.getBeanInfo(
value.getClass()).getPropertyDescriptors()) {
- String fieldName = pd.getName();
Class<?> fieldType = pd.getPropertyType();
- if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
+ String fieldName = getTransportFieldName(pd);
+ if (fieldName == null) {
continue;
}
Method getterMethod = pd.getReadMethod();
return jsonMap;
}
+ /**
+ * Returns the name that should be used as field name in the JSON. We strip
+ * "set" from the setter, keeping the result - this is easy to do on both
+ * server and client, avoiding some issues with cASE. E.g setZIndex()
+ * becomes "ZIndex". Also ensures that both getter and setter are present,
+ * returning null otherwise.
+ *
+ * @param pd
+ * @return the name to be used or null if both getter and setter are not
+ * found.
+ */
+ private static String getTransportFieldName(PropertyDescriptor pd) {
+ if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
+ return null;
+ }
+ return pd.getWriteMethod().getName().substring(3);
+ }
+
private static Object decodeObject(String type,
JSONObject serializedObject, Application application)
throws JSONException {
Object decodedObject = cls.newInstance();
for (PropertyDescriptor pd : Introspector.getBeanInfo(cls)
.getPropertyDescriptors()) {
- if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
+
+ String fieldName = getTransportFieldName(pd);
+ if (fieldName == null) {
continue;
}
-
- String fieldName = pd.getName();
JSONArray encodedObject = serializedObject
.getJSONArray(fieldName);
pd.getWriteMethod().invoke(decodedObject,
for (JMethod setterMethod : getSetters(beanType)) {
String setterName = setterMethod.getName();
- String capitalizedFieldName = setterName.substring(3);
- String fieldName = decapitalize(capitalizedFieldName);
+ String fieldName = setterName.substring(3); // setZindex() -> ZIndex
String getterName = findGetter(beanType, setterMethod);
if (getterName == null) {
+ beanQualifiedSourceName + ".class);");
for (JMethod method : getSetters(beanType)) {
String setterName = method.getName();
- String capitalizedFieldName = setterName.substring(3);
- String fieldName = decapitalize(capitalizedFieldName);
+ String fieldName = setterName.substring(3); // setZIndex() -> ZIndex
JType setterParameterType = method.getParameterTypes()[0];
logger.log(Type.DEBUG, "* Processing field " + fieldName + " in "
+ beanQualifiedSourceName + " (" + beanType.getName() + ")");
- String jsonFieldName = "json" + capitalizedFieldName;
+ String jsonFieldName = "json" + fieldName;
// JSONArray jsonHeight = (JSONArray) jsonValue.get("height");
sourceWriter.println("JSONArray " + jsonFieldName
+ " = (JSONArray) jsonValue.get(\"" + fieldName + "\");");
private String findGetter(JClassType beanType, JMethod setterMethod) {
JType setterParameterType = setterMethod.getParameterTypes()[0];
- String capitalizedFieldName = setterMethod.getName().substring(3);
+ String fieldName = setterMethod.getName().substring(3);
if (setterParameterType.getQualifiedSourceName().equals(
boolean.class.getName())) {
- return "is" + capitalizedFieldName;
+ return "is" + fieldName;
} else {
- return "get" + capitalizedFieldName;
+ return "get" + fieldName;
}
}