30

我有这个枚举

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

  private final int code;

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

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

在我的请求类中,我有这个字段:private RequestStatus status.

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

"status": "OK"

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

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

2 回答 2

22

你可以使用这样的东西:

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

或更简单地说(正如杰西威尔逊所指出的):

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;
         }
    }

}
于 2012-04-24T12:01:30.037 回答
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));
}
于 2016-09-06T19:21:52.523 回答