private JSONObject _convertToJSONObject(Object fromValue) throws JSONException, InvocationTargetException, IllegalAccessException { JSONObject jsonObject = new JSONObject(); Field[] fields = fromValue.getClass().getFields(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i]; if (Modifier.isStatic(field.getModifiers())) { continue; } JsonProperty property = field.getAnnotation(JsonProperty.class); if (property != null) { // AutoBox here ... Object value = field.get(fromValue); Class clazz = field.getType(); if (value != null) { clazz = value.getClass(); } String name = field.getName(); if (property.required() && value == null) { value = JSONObject.NULL; } else if (value == JSONObject.NULL) { // Leave it as null in this case. } else { value = getJsonValue(value, clazz, field); } jsonObject.put(name, value); } } return jsonObject; }