tags:

views:

35

answers:

2

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?

+1  A: 

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...

Fortega
I have created a converter and works fine... I still have some doubts on how to get the node name.
yeforriak
A: 

The EnumSetConverter isn't good.

If you use can use another collection, works fine:

public class DayOfWeekSet {
    private List<DayOfWeek> days = new ArrayList<DayOfWeek>();

    DayOfWeekSet() {
        for (DayOfWeek dow: DayOfWeek.values()) {
            days.add(dow);
        }
    }

    @XStreamAlias("day")
    public enum DayOfWeek implements Serializable {
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY,
        SUNDAY;
    }
}

<DayOfWeekSet>
  <days>
    <day>MONDAY</day>
    <day>TUESDAY</day>
    <day>WEDNESDAY</day>
    <day>THURSDAY</day>
    <day>FRIDAY</day>
    <day>SATURDAY</day>
    <day>SUNDAY</day>
  </days>
</DayOfWeekSet>
pablosaraiva
I see what you mean , but I can not modify the DayOfWeekSet class. For now implementing the converter is the best solution I found.
yeforriak