Skip to content

Instantly share code, notes, and snippets.

@rickyngk
Last active January 15, 2016 17:22
Show Gist options
  • Save rickyngk/04e6bfc42a7fe1a2ecef to your computer and use it in GitHub Desktop.
Save rickyngk/04e6bfc42a7fe1a2ecef to your computer and use it in GitHub Desktop.
EntityX export json
public JSONObject exportToJson() throws Exception{
JSONObject outputJsonObject = new JSONObject();
for (Field field : this.getClass().getDeclaredFields()) {
String fieldName = field.getName();
if (fields.containsKey(fieldName)) {
field.setAccessible(true);
EntityMetaData t = fields.get(fieldName);
Object fieldValue = field.get(this);
if (fieldValue != null) {
if (t.isArray) {
Iterable iterable = (Iterable) fieldValue;
JSONArray array = new JSONArray();
if (t.isDerivedFromEntity) {
for (Object curr : iterable) {
array.put(((EntityX) curr).exportToJson());
}
} else {
for (Object curr : iterable) {
array.put(curr);
}
}
outputJsonObject.put(t.key, array);
} else {
if (t.isDerivedFromEntity) {
outputJsonObject.put(t.key, ((EntityX) fieldValue).exportToJson());
} else {
outputJsonObject.put(t.key, fieldValue);
}
}
}
}
}
return outputJsonObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment