gpt4 book ai didi

java - 使用 GSON 序列化/反序列化包含特殊枚举(不是字符串枚举)的 POJO

转载 作者:行者123 更新时间:2023-12-02 11:11:25 25 4
gpt4 key购买 nike

我需要序列化/反序列化一个包含特殊枚举的 POJO(不是字符串枚举)。我发现很多带有字符串枚举的示例,但不是我的情况。

我阅读了 Gson 文档,并且我从 implements JsonDeserializer<T>, JsonSerializer<T> 开始了解决方案

public class ApplicationError {

private static final long serialVersionUID = 1L;

private final ErrorCode code;

private final String description;

private final URL infoURL;

....
}

public enum ErrorCode {
INVALID_URL_PARAMETER(HttpStatus.BAD_REQUEST, 20, "Invalid URL parameter value"),
MISSING_BODY(HttpStatus.BAD_REQUEST, 21, "Missing body"),
INVALID_BODY(HttpStatus.BAD_REQUEST, 22, "Invalid body")
}

public class ErrorCodeDeserializer implements JsonDeserializer<ErrorCode> /*, JsonSerializer<ErrorCode> */{

@Override
public ErrorCode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
ErrorCode[] scopes = ErrorCode.values();
for (ErrorCode scope : scopes) {
System.out.println("--------->" + scope + " " + json.getAsString());
if (scope.equals(json.getAsString())) {
return scope;
}
}
return null;
}

/*
@Override
public JsonElement serialize(ErrorCode arg0, Type arg1, JsonSerializationContext arg2) {
???
}*/
}

...
ApplicationError applicationError = new ApplicationError(ErrorCode.INVALID_URL_PARAMETER,
"Application identifier is missing");
....
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ErrorCode.class, new ErrorCodeDeserializer());
Gson gson = gsonBuilder.create();
gson.toJson(applicationError)

我的结果是:

{"code":"INVALID_URL_PARAMETER","description":"缺少应用程序标识符"}

而不是:

{"code":"20", "message":"无效的 URL 参数值", "description":"缺少应用程序标识符"}

编辑 1

我尝试:

@Override
public JsonElement serialize(ErrorCode src, Type typeOfSrc, JsonSerializationContext context) {
JsonArray jsonMerchant = new JsonArray();
jsonMerchant.add("" + src.getCode());
jsonMerchant.add("" + src.getMessage());
return jsonMerchant;
}

但我的结果是:

{"code":["20","Invalid URL parameter value"],"description":"Application identifier is missing"}

编辑2

我尝试:

@Override
public JsonElement serialize(ErrorCode src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("code", new JsonPrimitive(src.getCode()));
result.add("message", new JsonPrimitive(src.getMessage()));
return result;
}

但我的结果是:

{"code":{"code":20,"message":"Invalid URL parameter value"},"description":"Application identifier is missing"}

现在我想要公正的改变"code":{"code":20,"message":"Invalid URL parameter value"}通过"code":20,"message":"Invalid URL parameter value"

最佳答案

总的来说,这是一个坏主意,原因如下:

  • 如果在流模式下读取平面属性(确保属性的顺序保持不变),您的反序列化器(如果需要)需要复杂。
  • 否则,您需要使用 ErrorCode 枚举为每个类编写一个特殊的类型适配器,并且需要一个自定义的 JsonSerializer/JsonDeserializer他们每个人。
  • 反序列化 ErrorCode 对我来说毫无意义。
  • Gson 不允许将对象“压平”到彼此。

在最简单的实现中,我想说您可能想要使用这样的东西:

final class FlatErrorCodeTypeAdapter
extends TypeAdapter<ErrorCode> {

private FlatErrorCodeTypeAdapter() {
}

@Override
public void write(final JsonWriter out, final ErrorCode errorCode)
throws IOException {
// very bad idea - the serializer may be in a bad state and we assume the host object is being written
out.value(errorCode.code);
out.name("message");
out.value(errorCode.message);
}

@Override
public ErrorCode read(final JsonReader in)
throws IOException {
// now fighting with the bad idea being very fragile assuming that:
// * the code field appears the very first property value
// * we ignore the trailing properties and pray the host object does not have "message" itself
// * no matter what "message" is -- it simply does not have sense
final int code = in.nextInt();
return ErrorCode.valueByCode(code);
}

}

然后在你的代码中是这样的:

final class ApplicationError {

@JsonAdapter(FlatErrorCodeTypeAdapter.class)
final ErrorCode code;
final String description;

ApplicationError(final ErrorCode code, final String description) {
this.code = code;
this.description = description;
}

}

使用示例:

private static final Gson gson = new Gson();

...

final ApplicationError before = new ApplicationError(ErrorCode.INVALID_URL_PARAMETER, "Application identifier is missing");
final String json = gson.toJson(before);
System.out.println(json);
final ApplicationError after = gson.fromJson(json, ApplicationError.class);
System.out.println(before.code == after.code);
System.out.println(before.description.equals(after.description));

输出:

{"code":20,"message":"Invalid URL parameter value","description":"Application identifier is missing"}
true
true

我仍然认为这是一个非常脆弱的解决方案,我只是建议您自己重新设计您的 ApplicationError 并“展平”ErrorCode:

final class ApplicationError {

final int code;
final String message;
final String description;

ApplicationError(final ErrorCode errorCode, final String description) {
this.code = errorCode.code;
this.message = errorCode.message;
this.description = description;
}

...

final ErrorCode resolveErrorCode() {
final ErrorCode errorCode = ErrorCode.valueByCode(code);
if ( !errorCode.message.equals(message) ) {
throw new AssertionError('wow...');
}
return errorCode;
}

}

使用后者,您甚至不需要以任何方式配置 Gson

关于java - 使用 GSON 序列化/反序列化包含特殊枚举(不是字符串枚举)的 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50599504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com