java Xstream:删除类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2008043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xstream: removing class attribute
提问by Quintin Par
How do I remove the class=”Something ”attributes in Xstream .
如何删除Xstream中的class=”Something ”属性。
I use Xstream with annotations
我使用带注释的 Xstream
回答by Bleastrind
I read its code and found if your class is not mapper.defaultImplementationOf(fieldType), it will add the default class attribute for you, unless the class attribute name is null;
我读了它的代码,发现如果你的类不是mapper.defaultImplementationOf(fieldType),它会为你添加默认的类属性,除非类属性名称为空;
So, set this can remove the class=”Something ” attributes
所以,设置这个可以去掉 class="Something" 属性
xstream.aliasSystemAttribute(null, "class");
回答by Christopher Oezbek
Indeed the problem is not as clearly phrased as it should. My guess is that you are using a non-standard collection or using a field of an interface type for which XStream needs to store the actual class.
事实上,这个问题并没有像它应该的那样清楚地表达出来。我的猜测是您正在使用非标准集合或使用 XStream 需要为其存储实际类的接口类型的字段。
In the second case you can just use alias:
在第二种情况下,您可以只使用别名:
xstream.alias("field name", Interface.class, ActualClassToUse.class);
xstream.alias("field name", Interface.class, ActualClassToUse.class);
See http://markmail.org/message/gds63p3dnhpy3ef2for more details.
有关更多详细信息,请参阅http://markmail.org/message/gds63p3dnhpy3ef2。
回答by MiKu
Use something of this sort to remove the class attribute completely rather than aliasing it with something else:
使用这样的东西来完全删除类属性,而不是用其他东西给它别名:
private String generateResponse(final XStream xStream)
{
StringWriter writer = new StringWriter();
xStream.marshal(this, new PrettyPrintWriter(writer) {
@Override
public void addAttribute(final String key, final String value)
{
if (!key.equals("class"))
{
super.addAttribute(key, value);
}
}
});
return writer.toString();
}
回答by Dave
Can you give some example output? I think this usually happens when using Collections. Without seeing the output, my best guess is that you need to register aliases:
你能给出一些示例输出吗?我认为这通常发生在使用集合时。在没有看到输出的情况下,我最好的猜测是您需要注册别名:
xstream.alias("blog", Blog.class);
See http://x-stream.github.io/alias-tutorial.htmlfor more in-depth coverage. Again, paste in some sample output.
有关更深入的报道,请参阅http://x-stream.github.io/alias-tutorial.html。再次粘贴一些示例输出。
回答by kcpr
This attribute is shown, at least, when it's not obvious which class shall be used. Usage of interface is an example. In situations like that You can try:
至少,当不清楚应该使用哪个类时,会显示该属性。接口的使用就是一个例子。在这种情况下,您可以尝试:
xStream.addDefaultImplementation(YourDefaultImplementation.class, YourInterface.class);
.
.

