简单的2.6.7不像Simple 2.6那样处理枚举吗?

时间:2012-11-26 10:44:07

标签: java enums xml-serialization simple-framework

考虑以下使用simple-xml注释注释的枚举:

@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;
    }
}

并且,使用简单框架将其序列化为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());

使用simple-2.6,我看到了这个输出 - 这就是我所期望的:

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

然而,当使用simple-2.6.7序列化时,相同代码会产生以下结果:

<daysOfWeek>TUESDAY</daysOfWeek>

基本上,在simple-2.6.7中,枚举的各个成员(以及它们上的simple-xml注释)将被忽略,并且枚举常量的名称始终用于序列化。

这是打算吗?如何在考虑枚举的各个成员的同时获取最新版本的simple-xml来序列化枚举?

1 个答案:

答案 0 :(得分:2)

changelog for(2.6.x)版本中只有一个包含 enum 的条目:

  

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

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


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

DaysOfWeek的注释:

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

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

(可能) Converter的实现 - 接口:

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>

<强>文档