作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设 Java 类中有以下枚举:
enum AccessMode {
READ_WRITE,
READ_ONLY,
WRITE_ONLY
};
只要 JSON 包含枚举字段的有效值,JSON 反序列化就可以与 Gson 一起正常工作,例如:
"access": "READ_WRITE"
不幸的是,fromJson() 似乎确实检测到 JSON 中的无效枚举值,例如:
"access": "READ_XXX"
如何在使用 Gson 反序列化 JSON 文件时添加枚举值检查?
最佳答案
从 2.8.2 版本开始,Gson 不支持这样的用例。我认为作为一种特殊的 GsonBuilder
配置方法,作为建议提交给 Gson 开发团队是值得的。您现在最多可以编写一个自定义枚举类型适配器,它几乎复制了 com.google.gson.internal.bind.EnumTypeAdapter
功能,但添加了名称检查。
final class StrictEnumTypeAdapterFactory
implements TypeAdapterFactory {
private static final TypeAdapterFactory allStrictEnumTypeAdapterFactory = new StrictEnumTypeAdapterFactory(enumClass -> true);
private final Predicate<? super Class<? extends Enum<?>>> isStrictEnumClass;
private StrictEnumTypeAdapterFactory(final Predicate<? super Class<? extends Enum<?>>> isStrictEnumClass) {
this.isStrictEnumClass = isStrictEnumClass;
}
static TypeAdapterFactory get(final Predicate<? super Class<? extends Enum<?>>> isStrictEnumClass) {
return new StrictEnumTypeAdapterFactory(isStrictEnumClass);
}
static TypeAdapterFactory get() {
return allStrictEnumTypeAdapterFactory;
}
@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
final Class<? super T> rawType = typeToken.getRawType();
// Skip non-enums
if ( !Enum.class.isAssignableFrom(rawType) ) {
return null;
}
// Check if the enum is supported by the "strict" policy
@SuppressWarnings("unchecked")
final Class<? extends Enum<?>> enumRawType = (Class<? extends Enum<?>>) rawType;
if ( !isStrictEnumClass.test(enumRawType) ) {
return null;
}
// Trivial rawtypes/unchecked casts
@SuppressWarnings({ "rawtypes", "unchecked" })
final TypeAdapter<? extends Enum<?>> strictEnumTypeAdapter = StrictEnumTypeAdapter.get((Class) enumRawType);
@SuppressWarnings("unchecked")
final TypeAdapter<T> castTypeAdapter = (TypeAdapter<T>) strictEnumTypeAdapter;
return castTypeAdapter;
}
private static final class StrictEnumTypeAdapter<E extends Enum<E>>
extends TypeAdapter<E> {
private final Class<E> enumClass;
private final Map<String, E> nameToEnumConstant;
private final Map<E, String> enumConstantToName;
private StrictEnumTypeAdapter(final Class<E> enumClass, final Map<String, E> nameToEnumConstant, final Map<E, String> enumConstantToName) {
this.enumClass = enumClass;
this.nameToEnumConstant = nameToEnumConstant;
this.enumConstantToName = enumConstantToName;
}
private static <E extends Enum<E>> TypeAdapter<E> get(final Class<E> enumClass) {
final Map<String, E> nameToEnumConstant = new HashMap<>();
final Map<E, String> enumConstantToName = new HashMap<>();
final Map<String, E> enumNameToEnumConstant = Stream.of(enumClass.getEnumConstants())
.collect(Collectors.toMap(Enum::name, Function.identity()));
Stream.of(enumClass.getFields())
// It can be either a simple enum constant, or an enum constant that overrides
.filter(field -> enumClass.isAssignableFrom(field.getType()))
.forEach(field -> {
final E enumConstant = enumNameToEnumConstant.get(field.getName());
// For compatibility with the original type adapter, we have to respect the @SeriaizedName annotation
final SerializedName serializedName = field.getAnnotation(SerializedName.class);
if ( serializedName == null ) {
nameToEnumConstant.put(field.getName(), enumConstant);
enumConstantToName.put(enumConstant, field.getName());
} else {
nameToEnumConstant.put(serializedName.value(), enumConstant);
enumConstantToName.put(enumConstant, serializedName.value());
for ( final String alternate : serializedName.alternate() ) {
nameToEnumConstant.put(alternate, enumConstant);
}
}
});
return new StrictEnumTypeAdapter<>(enumClass, nameToEnumConstant, enumConstantToName)
.nullSafe(); // A convenient method to handle nulls
}
@Override
public void write(final JsonWriter out, final E value)
throws IOException {
out.value(enumConstantToName.get(value));
}
@Override
public E read(final JsonReader in)
throws IOException {
final String key = in.nextString();
// This is what the original type adapter probably misses
if ( !nameToEnumConstant.containsKey(key) ) {
throw new JsonParseException(enumClass + " does not have an enum named " + key + " at " + in);
}
return nameToEnumConstant.get(key);
}
}
}
简单测试:
private static final Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(StrictEnumTypeAdapterFactory.get())
.create();
public static void main(final String... args)
throws IOException {
try ( final JsonReader jsonReader = Resources.getPackageResourceJsonReader(Q49572505.class, "good.json") ) {
System.out.println(gson.<Status>fromJson(jsonReader, Status.class).access);
}
try ( final JsonReader jsonReader = Resources.getPackageResourceJsonReader(Q49572505.class, "bad.json") ) {
try {
gson.<Status>fromJson(jsonReader, Status.class);
throw new AssertionError();
} catch ( final JsonParseException ex ) {
System.out.println(ex.getMessage());
}
}
}
输出:
READ_WRITE
class q49572505.AccessMode does not have an enum named READ_XXX at JsonReader at line 2 column 22 path $.access
关于java - Gson 在反序列化期间检查枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49572505/
我是一名优秀的程序员,十分优秀!