使用 Xstream 序列化枚举时如何指定别名
我正在序列化一个包含枚举作为字段的类,假设:
private class DayOfWeekSet {
private final EnumSet<DayOfWeek> days;
}
public enum DayOfWeek implements Serializable {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
枚举的 Xstream 输出是:
<days>
<day enum-type="com.company.model.DayOfWeek">MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY</day>
</days>
并且我想要类似的东西:
<days>
<day>MONDAY</day>
<day>TUESDAY</day>
<day>WEDNESDAY</day>
<day>THURSDAY</day>
<day>FRIDAY</day>
<day>SATURDAY</day>
<day>SUNDAY</day>
</days>
我在 XStream 文档中找到了 @XStreamImplicit(itemFieldName="name") 注释,但它是仅适用于集合。
是否可以通过注释来完成,或者我是否需要创建一个转换器?
为了获得我在创建以下转换器之前描述的结果:
public class XstreamDayOfTheWeekEnumConverter implements Converter {
@Override
@SuppressWarnings("rawtypes")
public boolean canConvert(Class type) {
return type.equals(DayOfWeekSet.class);
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
DayOfWeekSet set = (DayOfWeekSet) source;
for (Iterator<DayOfWeek> iterator = set.getDays().iterator(); iterator.hasNext();) {
Enum<DayOfWeek> value = iterator.next();
writer.startNode("day");
writer.setValue(value.name());
writer.endNode();
}
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
//Not implemented
return null;
}
}
如果我用 xstream 别名注释 (@XStreamAlias) 注释 Enum,是否有任何方法可以从它获取节点名称?
I am serializing a class that contains an enum as a field, let say :
private class DayOfWeekSet {
private final EnumSet<DayOfWeek> days;
}
public enum DayOfWeek implements Serializable {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
the Xstream output of the enum is :
<days>
<day enum-type="com.company.model.DayOfWeek">MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY</day>
</days>
and I would like something like:
<days>
<day>MONDAY</day>
<day>TUESDAY</day>
<day>WEDNESDAY</day>
<day>THURSDAY</day>
<day>FRIDAY</day>
<day>SATURDAY</day>
<day>SUNDAY</day>
</days>
I found @XStreamImplicit(itemFieldName="name") annotattion in the XStream documentation but it is only working for Collections.
Is it possible to do it with annotations or do I need to create a converter?
To obtain the result I described before I've created the following converter:
public class XstreamDayOfTheWeekEnumConverter implements Converter {
@Override
@SuppressWarnings("rawtypes")
public boolean canConvert(Class type) {
return type.equals(DayOfWeekSet.class);
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
DayOfWeekSet set = (DayOfWeekSet) source;
for (Iterator<DayOfWeek> iterator = set.getDays().iterator(); iterator.hasNext();) {
Enum<DayOfWeek> value = iterator.next();
writer.startNode("day");
writer.setValue(value.name());
writer.endNode();
}
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
//Not implemented
return null;
}
}
is there any way to get the node name from xstream alias annotation (@XStreamAlias) if I annotated the Enum with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 XStream 中不存在这种情况。您可能必须编写自己的转换器。您可以从 EnumSetConverter,应该不是很困难...
I don't think this exists in XStream. You will probably have to write your own converter. You could start from the code of the EnumSetConverter, it should not be very difficult...
EnumSetConverter 不好。
如果您可以使用另一个集合,则效果很好:
The EnumSetConverter isn't good.
If you can use another collection, works fine: