BeanSerializerState beanState;
try {
beanState = (BeanSerializerState) state.get(BeanSerializerState.class);
} catch (Exception e) {
e.printStackTrace();
throw new MarshallException("bean serializer internal error : " + e.getMessage());
}
Integer identity = System.identityHashCode(o);
if (beanState.beanSet.contains(identity)) {
throw new MarshallException("circular reference");
}
beanState.beanSet.add(identity);
BeanData bd;
try {
bd = getBeanData(o.getClass());
} catch (IntrospectionException e) {
throw new MarshallException(o.getClass().getName() + " is not a bean");
}
JSONObject val = new JSONObject();
if (ser.getMarshallClassHints()) {
val.put("javaClass", o.getClass().getName());
}
Iterator i = bd.readableProps.entrySet().iterator();
Object[] args = new Object[0];
Object result;
while (i.hasNext()) {
Map.Entry ent = (Map.Entry) i.next();
String prop = (String) ent.getKey();
Method getMethod = (Method) ent.getValue();
log.debug("invoking {}()", getMethod.getName());
try {
result = getMethod.invoke(o, args);
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
throw new MarshallException("bean " + o.getClass().getName() + " can't invoke "
+ getMethod.getName() + ": " + e.getMessage());
}
try {
if (result != null || ser.getMarshallNullAttributes()) {
val.put(prop, ser.marshall(state, result));
}
} catch (MarshallException e) {
throw new MarshallException("bean " + o.getClass().getName() + " " + e.getMessage());
}
}
beanState.beanSet.remove(identity);
return val;