I am trying to convert an object into JSON using the GSON library on Google App Engine. For some reason, it throws this exception and I don\'t understand how to solve this.
If App Engine does not support Reflection, then we are pretty much left to write our own toJSON method. This can be done as follows (not a big deal but someone might find it useful):
public SampleObject {
//...
/**
* Convert this object to a JSON object for representation
*/
public JSONObject toJSON() {
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("id", this.id);
jsonobj.put("name", this.name);
return jsonobj;
} catch(Exception e) {
return null;
}
}
}
Then, you can use a toString method on this object to print out the JSON representation. Not the best I agree but some workaround for now.