使用具有无效字段忽略的对象映射器反序列化

问题描述

我不是Java程序员,但渴望了解如何实现在反序列化时会忽略一个或多个无效字段的功能。在我的情况下,byte[]由旧式系统发送,因此无法控制数据校正(在编码方面,或者在丢失时发送默认值)。这意味着,我需要预测byte[]中的无效数据/丢失数据。

以下代码对有效负载进行序列化,但是在收到无效代码(错误编码,null等)后引发异常。

   public class LDeserializer<T> implements Deserializer<T> {

    private final ObjectMapper objectMapper;
    private final Class<T> myType;

    public LDeserializer(final ObjectMapper objectMapper,final Class<T> myType) {
        this.objectMapper = objectMapper;
        this.myType = myType;
      }

    public LDeserializer(final Class<T> myType) {
        this(new ObjectMapper(),myType);
    }

    @Override
    public T deserialize(final String sometext,final byte[] bytes) {
        if (bytes == null) {
            return null;
        }

        try {
            return objectMapper.readValue(bytes,myType);
        } catch (final IOException e) {
            throw new SerializationException(e);
        }
    }
}

问题:就我而言,我可以安全地在序列化时忽略无效值。但是,我不确定如何指示序列化程序忽略无效字段(无论其类型如何)。这样,即使构造的对象不完整,序列化程序也可以发出对象。

注意ObjectMapper是我在这里用来支持序列化的类型。但是,我可以随意使用任何可以代替ObjectMapper使用的帮助程序类型。这里唯一需要关注的是如何忽略无效字段(无论其类型如何)。

请告知

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...