Gson:如何改变Enum的输出

时间:2012-04-24 11:50:22

标签: java gson

我有枚举

enum RequestStatus {
  OK(200), NOT_FOUND(400);

  private final int code;

  RequestStatus(int code) {
    this.code = code;
  }

  public int getCode() {
    return this.code;
  }
};

在我的Request-class中,我有这个字段:private RequestStatus status

当使用Gson将Java对象转换为JSON时,结果如下:

"status": "OK"

如何更改我的GsonBuilder或我的Enum对象,以便为我提供如下输出:

"status": {
  "value" : "OK",
  "code" : 200
}

2 个答案:

答案 0 :(得分:18)

您可以使用以下内容:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapterFactory(new MyEnumAdapterFactory());

或更简单(如Jesse Wilson所说):

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(RequestStatus.class, new MyEnumTypeAdapter());

public class MyEnumAdapterFactory implements TypeAdapterFactory {

    @Override
    public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
            Class<? super T> rawType = type.getRawType();
            if (rawType == RequestStatus.class) {
                return new MyEnumTypeAdapter<T>();
            }
            return null;
    }

    public class MyEnumTypeAdapter<T> extends TypeAdapter<T> {

         public void write(JsonWriter out, T value) throws IOException {
              if (value == null) {
                   out.nullValue();
                   return;
              }
              RequestStatus status = (RequestStatus) value;
              // Here write what you want to the JsonWriter. 
              out.beginObject();
              out.name("value");
              out.value(status.name());
              out.name("code");
              out.value(status.getCode());
              out.endObject();
         }

         public T read(JsonReader in) throws IOException {
              // Properly deserialize the input (if you use deserialization)
              return null;
         }
    }

}

答案 1 :(得分:2)

除了Polet的回答,如果你需要一个通用的Enum序列化器,你可以通过反思实现它:

public class EnumAdapterFactory implements TypeAdapterFactory
{

    @Override
    public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type)
    {
        Class<? super T> rawType = type.getRawType();
        if (rawType.isEnum())
        {
            return new EnumTypeAdapter<T>();
        }
        return null;
    }

    public class EnumTypeAdapter<T> extends TypeAdapter<T>
    {
        @Override
        public void write(JsonWriter out, T value) throws IOException
        {
            if (value == null || !value.getClass().isEnum())
            {
                out.nullValue();
                return;
            }

            try
            {
                out.beginObject();
                out.name("value");
                out.value(value.toString());
                Arrays.stream(Introspector.getBeanInfo(value.getClass()).getPropertyDescriptors())
                      .filter(pd -> pd.getReadMethod() != null && !"class".equals(pd.getName()) && !"declaringClass".equals(pd.getName()))
                      .forEach(pd -> {
                          try
                          {
                              out.name(pd.getName());
                              out.value(String.valueOf(pd.getReadMethod().invoke(value)));
                          } catch (IllegalAccessException | InvocationTargetException | IOException e)
                          {
                              e.printStackTrace();
                          }
                      });
                out.endObject();
            } catch (IntrospectionException e)
            {
                e.printStackTrace();
            }
        }

        public T read(JsonReader in) throws IOException
        {
            // Properly deserialize the input (if you use deserialization)
            return null;
        }
    }
}

用法:

@Test
public void testEnumGsonSerialization()
{
    List<ReportTypes> testEnums = Arrays.asList(YourEnum.VALUE1, YourEnum.VALUE2);
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new EnumAdapterFactory());
    Gson gson = builder.create();
    System.out.println(gson.toJson(reportTypes));
}