java - 如何模拟 GSON 自定义解串器?
问题描述
我正在尝试模拟此“活动代码反序列化器”以进行代码覆盖,但它似乎失败了。此反序列化器在其余客户端的 GSON 构建器中调用
public class ActivityCodeDeserializer implements JsonDeserializer<ActivityCode> {
@Override
public ActivityCode deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return ActivityCode.toSignerActivityCode(jsonElement.getAsInt());
}
}
这反过来调用带有代码和活动代码的枚举类
public enum ActivityCode{
UNKNOWN(-1),
INITIATION_SENT(101),
RECEIVED(102),
private static Map<Integer, ActivityCode> values = new HashMap<>();
private int statusCode = -1;
static {
for (ActivityCode signerActivity : values()) {
values.put(signerActivity.value(), signerActivity);
}
}
ActivityCode(int status) {
this.statusCode = status;
}
public int value() {
return statusCode;
}
public static ActivityCode toSignerActivityCode(int statusCode) {
return values.containsKey(statusCode) ? values.get(statusCode) : UNKNOWN;
}
@Override
public String toString() {
return "Activity{" +
"statusCode=" + statusCode +
", name=" + name() +
'}';
}
}
解决方案
推荐阅读
- python - Python KivyMD:如何圆角 MDCard 小部件?
- java - 无法将字节数组转换为音频 AAC 文件
- typescript - 出现错误:Type 'Usersinterface' 不满足约束文档
- flutter - Flutter:有没有办法知道何时加载列表视图构建器?
- react-native - React Native Navigation 4 - 屏幕没有获取参数
- mysql - SQL ADD CONSTRAINT 在 phpMyAdmin 中不起作用
- javascript - 在组件已经卸载后,如何从解决承诺中取消状态更改?
- c# - 使用 .NetStandard 打开自定义的外部 Web 浏览器
- php - WordPress + ACF 画廊图像大小的随机数组
- javascript - 使绝对定位按钮的宽度与其内容相同