2

Consider the following enum annotated with simple-xml annotations:

@Root(name="days")
public enum DaysOfWeek {

    SUNDAY("blue", 30),
    MONDAY("green", 60),
    TUESDAY("yellow", 50),
    WEDNESDAY("red", 45),
    THURSDAY("black", 45),
    FRIDAY("white", 65),
    SATURDAY("brown", 40);

    @Attribute(name="color")
    private String color;

    @Element(name="mins")
    private int minutes;


    DaysOfWeek(String color, int minutes){
        this.color = color;
        this.minutes = minutes;
    }

    DaysOfWeek(){
        /*
         * Default constructor
         */
    }

    public void setColor(String color){
        this.color = color;
    }

    public void setMinutes(int minutes){
        this.minutes = minutes;
    }

    public String getColor(){
        return this.color;

    }

    public int getMinutes(){
        return this.minutes;
    }
}

And, the code that uses simple framework to serialize it into XML:

    StringWriter writer = new StringWriter();
    try {
        serializer.write(DaysOfWeek.TUESDAY, writer);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(writer.toString());

With simple-2.6, I see this output - which is what I expect:

<days color="yellow">
   <mins>50</mins>
</days>

However, the same code, when serialized with simple-2.6.7, gives this:

<daysOfWeek>TUESDAY</daysOfWeek>

Basically, in simple-2.6.7, the individual members of an enum (and the simple-xml annotations on them) are ignored and always the name of the enum constant is used for serialization.

Is this intended? How do I get the latest version of simple-xml to serialize an enum while taking the individual members of the enum into consideration?

4

1 回答 1

2

(2.6.x) 版本的变更日志中只有一个包含枚举的条目:

简单 2.6.3:
- 修复错误以确保可以正确序列化抽象枚举

但是你为什么要序列化呢colorminutes如果您序列化和反序列化DaysOfWeek,枚举将根据您的定义(构造函数)获取它们的值。


无论如何,您可以使用 aConverter来自定义您的 XML:

的注释DaysOfWeek

@Root(name = "days")
@Convert(DaysOfWeekConverter.class)
public enum DaysOfWeek
{
    // ...
}

注意:您不再需要@Element@Attribute注释,现在指定此类的 xml 的“内容” Converter

(可能)Converter -Interface的实现:

public class DaysOfWeekConverter implements Converter<DaysOfWeek>
{
    @Override
    public DaysOfWeek read(InputNode node) throws Exception
    {
        DaysOfWeek rtn = getDayByColor(node.getAttribute("color").getValue());
        rtn.setMinutes(Integer.valueOf(node.getNext("mins").getValue()));

        return rtn;
    }


    @Override
    public void write(OutputNode node, DaysOfWeek value) throws Exception
    {
        node.setName("days");
        node.setAttribute("color", value.getColor());
        node.getChild("mins").setValue("" + value.getMinutes());
    }


    private DaysOfWeek getDayByColor(String color)
    {
        for( DaysOfWeek value : DaysOfWeek.values() )
        {
            if( value.getColor().equals(color) )
                return value;
        }

        throw new IllegalArgumentException("No Day available for color \'" + color + "\'");
    }
}

(示例)使用转换器:

/*
 * Setting 'AnnotationStrategy' is requried here - else the Converter will get ignored.
 */
Serializer ser = new Persister(new AnnotationStrategy());

StringWriter sw = new StringWriter();
ser.write(DaysOfWeek.TUESDAY, sw);

// ...

StringReader sr = new StringReader(sw.toString());
DaysOfWeek day = ser.read(DaysOfWeek.class, sr);

// ...

序列化 XML (来自示例)

<days color="yellow">
   <mins>50</mins>
</days>

文档:

于 2012-11-26T22:03:48.607 回答