java - 将具有属性的 Java 枚举序列化为 json 对象
问题描述
在 Java 中,将元属性添加到枚举类很简单:
public enum ItemType {
NORMAL("Normal Item", 10, false),
SPECIAL("Special Item", 20, false),
RARE("Rare Item", 30, true);
private final String description;
private final int points;
private final boolean magical;
private ItemType(String description, int points, boolean magical) {
this.description = description;
this.points = points;
this.magical = magical;
}
@Override
public String toString() {
return this.description;
}
public String getDescription() {
return description;
}
public int getPoints() {
return points;
}
public boolean isMagical() {
return magical;
}
}
我想序列化这些,但仅在某些休息端点按需序列化(即,在枚举名称转换为字符串的情况下,正常序列化仍应适用:NORMAL
、、SPECIAL
或RARE
):
{
"_enum": "NORMAL",
"description": "Normal Item",
"points": 10,
"magical": false
}
有没有办法注释我的枚举,以便 gson 或 moshi 可以生成这样的 json 对象?还有其他解决方案吗?
解决方案
我认为没有涵盖所有情况的通用解决方案。所有库,但这将是 gson 的解决方案
class ItemTypeAdapter extends TypeAdapter<ItemType> {
private final String ENUM_ID = "_enum";
@Override
public void write(JsonWriter writer, ItemType itemType) throws IOException {
writer.beginObject();
writer.name(ENUM_ID).value(itemType.name());
writer.name("description").value(itemType.getDescription());
writer.name("points").value(itemType.getPoints());
writer.name("magical").value(itemType.isMagical());
writer.endObject();
}
@Override
public ItemType read(JsonReader reader) throws IOException {
String itemType = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals(ENUM_ID)) {
itemType = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
if (itemType != null) {
return ItemType.valueOf(itemType);
} else {
throw new JsonParseException("Missing '" + ENUM_ID + "' value");
}
}
}
或者,您可以使用自定义JsonSerializer
响应。JsonDeserializer
. 要使用它,您需要在构建 gson 对象时将其注册为自定义类型适配器。其他库/框架可能有类似的解决方案
推荐阅读
- docker - 里面有 minikube 的 docker 镜像
- .htaccess - Symfony 与 web_server_bundle - 如何激活 .htaccess
- asp.net-core - 不能将“Microsoft.AspNet.OData.Routing.ODataRoute”与端点路由一起使用。ASP Net Core 2.2 的异常
- dynamics-crm - 如何在动态 crm 中隐藏业务流程?
- express - 有没有办法知道数据是否正在通过管道传输到快速响应中?
- c# - 如何从匹配条件的行集中获取不同的值
- vue.js - 如何按特定顺序加载 vuejs 组件?
- groovy - getProjectByName(projectname)通过命令行运行时在groovy中抛出空指针异常
- sql - Oracle - 将时间戳与日期结合并删除重复项
- azure-service-fabric - 为什么 ServiceRuntime.RegisterServiceAsync 在 serviceFactory 函数完成之前返回?