Convert thrift java bean to JSON object

Today we have a java bean generated by Thrift with schema like:

struct T {
   1: string address

Normally we can translate the java bean to a org.json.JSON object like:


T bean = new T();
JSONObject json = new JSONObject();
Map<String, Object> dataMap = org.apache.commons.beanutils.BeanUtils.describe(bean);
for (Entry<String, Object> entry : dataMap.entrySet()) {
    json.put(entry.getKey(), entry.getValue());

// or translate with JSONObject simple
JSONObject json = new JSONObject(bean);

But for beans which generated by thrift, Both we’ll get some no-used properties prefixes with set*:



After some googled, we are get some wiki API method, see also

 * Convert the generic TBase<?, ?> entity to JSON object.
 * @param tobj
 * @author Allex Wang
 * @return
public JSONObject convertBeanToJSON(final TBase<?, ?> tobj) {
    TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
    try {
        String json = serializer.toString(tobj, "utf8");
        return new JSONObject(json);
    } catch (TException ex) {
        LOGGER.error("Convert TBase object to JSON fails: " + ex.getMessage());
    } catch (JSONException ex) {
    return null;

Now we are get the JSON entity as expected: