在XStream中为不在XML中的字段设置默认值 [英] Set default value for fields not in XML in XStream
问题描述
有没有办法创建转换器或在每次转换后执行的某些操作?对于上下文,我试图填充不在我的XML中的字段的默认值,以便在我的数据模型更改时保持向后兼容性。例如,如果我有这个对象:
Is there a way to create a converter or some operation that is performed after every single conversion? For context, I am trying to populate default values for fields that are not in my XML in order to maintain backwards compatibility if my data model changes. For instance, if I had this object:
class A {
private String b;
private String c;
private String d;
}
我的XML类似于:
<a>
<b>b</b>
<d>d</d>
</a>
我希望导入XML以了解字段的默认值 c
即c
并将其设置为 A
。这应该是一个通用操作,我可以将默认值添加到非常复杂的图形的任何字段。如果在每次转换后都有某种方法可以触发函数,它可以根据我想设置默认值的对象图检查当前对象。
I want my import of the XML to know that there is a default value for the field c
that is "c"
and set it on A
as such. This should be a generic operation to which I can add defaults to any field of a very complex graph. If there were some way to trigger a function after every conversion, it could check the current object against a map of objects I'd like to set a default value on.
还要注意,使用readResolve / readObject似乎不是一个选项,因为1. readObject()似乎根本不适合我.2。readResolve将使用默认值覆盖该字段,即使它实际包含在XML中。如果我的假设在这里是错误的,请告诉我。
Also note, that using readResolve/readObject does not seem to be an option since 1. readObject() never seemed to work for me at all and 2. readResolve would overwrite the field with the default value even if it were actually included in the XML. Please let me know if my assumptions here are wrong though.
:我在用户邮件列表中找到了这个相关的主题:
http://article.gmane.org/gmane.comp.java。 xstream.user / 4619 / match = default + value
: I found this related thread on the user mailing list: http://article.gmane.org/gmane.comp.java.xstream.user/4619/match=default+value
似乎唯一建议的解决方案是使用 readResolve()
我已经说过这不是一个有效的解决方案。
and it seems like the only suggested solution is to use readResolve()
which I already said was not a valid solution.
推荐答案
XStream xstream = new XStream(new PureJavaReflectionProvider());
并且像往常一样使用默认值初始化对象。通过字段初始化或构造函数代码。
and just initialize your object with default values as usual. Either through field initialization or constructor code.
背景
如果不这样做指定 ReflectionProvider
xstream试图找到最佳反射提供者。但xstream的最佳 ReflectionProvider
可能不是最好的,因为它通常会选择 Sun14ReflectionProvider
。
If you do not specify a ReflectionProvider
xstream tries to find the best reflection provider. But the best ReflectionProvider
for xstream might not be the best for you, because it normally selects the Sun14ReflectionProvider
.
Sun14ReflectionProvider
使用与java序列化机制相同的实例化策略,这意味着它跳过构造函数代码或更详细 - 对象初始化程序代码。
The Sun14ReflectionProvider
uses the same instantiation strategy as the java serialization mechanism and that means that it skips constructor code or to be more detailed - the object initializer code.
因此,实例字段初始化如
Therefore instance field initializations like
class A {
private String b = "DEFAULT";
}
将不会应用,也不会应用构造函数代码,例如
will not be applied and also constructor code will not be applied, e.g.
class A {
private String b;
public A(){
b = "DEFAULT";
}
}
PureJavaReflectionProvider
而是使用(顾名思义)java反射API来实例化对象,例如 Class.newInstance()
因此执行对象初始化代码。
The PureJavaReflectionProvider
instead uses (as the name implies) the java reflection API to instantiate objects, e.g. Class.newInstance()
and therefore object initialization code is executed.
这篇关于在XStream中为不在XML中的字段设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!