序列化/反序列化POJO包含GSON

时间:2018-05-30 08:09:25

标签: java json enums gson

我需要序列化/反序列化POJO包含一个speciel枚举(不是字符串枚举)。我发现很多带有Enum of String的样本,但不是我的情况。

我阅读了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”:“无效的网址参数值”,“说明”:“申请标识缺失”}

编辑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"

1 个答案:

答案 0 :(得分:1)

一般来说,出于以下几个原因,这是个坏主意:

  • 如果在流模式下读取平面属性(确保属性顺序保持不变),您的反序列化程序(如果需要)需要复杂。
  • 否则,您需要使用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