反序列化时忽略过时的字段 [英] Ignore obsolete field when deserializing

查看:203
本文介绍了反序列化时忽略过时的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在寻找一种方法来实现这一目标而没有结果...

I have been searching all day for a way to achieve this without result...

我正在尝试反序列化一个对象.当我序列化它时,我有一个叫做(叫)obsoleteBool的布尔值.在该类的新版本中,我删除了该布尔,然后在反序列化时遇到错误(找不到字段"obsoleteBool").

I am trying to deserialize an object. When I serialized it, I had a bool called (let's say) obsoleteBool. In the new version of the class I removed this bool, and I now have an error when deserializing (Field "obsoleteBool" not found).

在类的旧版本和新版本之间添加新字段非常容易.即使不使用[OptionalField]属性,但是如何管理类的旧版本和新版本之间的字段删除?

It is very easy to add new fields between an old and a new version of a class. Even without using the [OptionalField] attribute... but how can I manage the removal of a field between an old and new version of a class?

推荐答案

有3种可能的解决方案:

There are 3 possible solutions:

  • 最简单的方法:将旧字段添加到类的新版本中
  • 自定义代码:实施自定义反序列化器
  • 额外的工作:编写一个转换程序,该程序遍历所有序列化的类,以旧格式读取它们,并使用新的类定义重新序列化它们.

就商业价值而言,第一种选择可能更可取.

In terms of business value the first option is probably preferable.

这篇关于反序列化时忽略过时的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆