使用 Xstream 序列化枚举时如何指定别名

发布于 2024-09-25 17:15:07 字数 1917 浏览 4 评论 0原文

我正在序列化一个包含枚举作为字段的类,假设:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-10-02 17:15:07

我认为 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...

妄断弥空 2024-10-02 17:15:07

EnumSetConverter 不好。

如果您可以使用另一个集合,则效果很好:

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>

The EnumSetConverter isn't good.

If you 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文