Last active
January 15, 2016 17:22
-
-
Save rickyngk/04e6bfc42a7fe1a2ecef to your computer and use it in GitHub Desktop.
EntityX export json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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