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?