在以下情况下向后兼容 xstream

发布于 2024-10-06 20:39:59 字数 592 浏览 6 评论 0原文

我有以下课程。

class SimpleDate {
    private final int year;   /* ? */
    private final int month;  /* 0 ~ 11 */
    private final int date;   /* 1 ~ 31 */
}

现在,我计划将课程重构为。

class SimpleDate {
    private final int year;   /* ? */
    private final int month;  /* 1 ~ 12!!!!! <-- change from 0 based to 1 based */
    private final int day;    /* 1 ~ 31 */
}

为了解决变量重命名问题,我将使用别名。

xStream.aliasField("date", SimpleDate.class, "day");

但是,我如何知道我正在读取旧的 XML 文件,并且我将为新读取的月份字段+1,将其从基于 0 更改为基于 1?

I had the following class.

class SimpleDate {
    private final int year;   /* ? */
    private final int month;  /* 0 ~ 11 */
    private final int date;   /* 1 ~ 31 */
}

Now, I plan to re-factor the class to.

class SimpleDate {
    private final int year;   /* ? */
    private final int month;  /* 1 ~ 12!!!!! <-- change from 0 based to 1 based */
    private final int day;    /* 1 ~ 31 */
}

To solve the the variable renaming issues, I will use alias.

xStream.aliasField("date", SimpleDate.class, "day");

However, how can I know I am reading an old XML file, and I will +1 for the newly read month field, to change it from based 0 to based 1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深空失忆 2024-10-13 20:39:59

xstream 常见问题解答 http://x-stream.github.io/faq.html 有关于如何处理不同版本的部分。

The xstream FAQ http://x-stream.github.io/faq.html has a section about how tp deal with different versions.

就是爱搞怪 2024-10-13 20:39:59
// Converter used to make sure XStream still able to read old version of XML
// files.
private static Converter getSimpleDateConverter() {
    return new Converter() {
        @Override
        public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
            SimpleDate simpleDate = (SimpleDate) o;
            writer.startNode("year");
            writer.setValue("" + simpleDate.getYear());
            writer.endNode();
            writer.startNode("month");
            writer.setValue("" + simpleDate.getMonth());
            writer.endNode();
            writer.startNode("day");
            writer.setValue("" + simpleDate.getDay());
            writer.endNode();
        }

        @Override
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
            Map<String, String> map = new HashMap<String, String>();
            while (reader.hasMoreChildren()) {
                reader.moveDown();
                map.put(reader.getNodeName(), reader.getValue());
                reader.moveUp();
            }
            // Introduce since version 1.0.6.
            // We had renamed 'date' field to 'day' field.
            final boolean isOldVersion = map.containsKey("date");
            if (isOldVersion) {
                final int year = Integer.parseInt(map.get("year"));
                // We changed 0 based month to 1 based month, to fit into Joda library.
                final int month = Integer.parseInt(map.get("month")) + 1;
                final int day  = Integer.parseInt(map.get("date"));
                return new SimpleDate(year, month, day);
            } else {
                final int year = Integer.parseInt(map.get("year"));
                final int month = Integer.parseInt(map.get("month"));
                final int day  = Integer.parseInt(map.get("day"));
                return new SimpleDate(year, month, day);
            }
        }

        @Override
        public boolean canConvert(Class type) {
            return SimpleDate.class.isAssignableFrom(type);
        }
    };
}

xStream.registerConverter(getSimpleDateConverter());
// Converter used to make sure XStream still able to read old version of XML
// files.
private static Converter getSimpleDateConverter() {
    return new Converter() {
        @Override
        public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
            SimpleDate simpleDate = (SimpleDate) o;
            writer.startNode("year");
            writer.setValue("" + simpleDate.getYear());
            writer.endNode();
            writer.startNode("month");
            writer.setValue("" + simpleDate.getMonth());
            writer.endNode();
            writer.startNode("day");
            writer.setValue("" + simpleDate.getDay());
            writer.endNode();
        }

        @Override
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
            Map<String, String> map = new HashMap<String, String>();
            while (reader.hasMoreChildren()) {
                reader.moveDown();
                map.put(reader.getNodeName(), reader.getValue());
                reader.moveUp();
            }
            // Introduce since version 1.0.6.
            // We had renamed 'date' field to 'day' field.
            final boolean isOldVersion = map.containsKey("date");
            if (isOldVersion) {
                final int year = Integer.parseInt(map.get("year"));
                // We changed 0 based month to 1 based month, to fit into Joda library.
                final int month = Integer.parseInt(map.get("month")) + 1;
                final int day  = Integer.parseInt(map.get("date"));
                return new SimpleDate(year, month, day);
            } else {
                final int year = Integer.parseInt(map.get("year"));
                final int month = Integer.parseInt(map.get("month"));
                final int day  = Integer.parseInt(map.get("day"));
                return new SimpleDate(year, month, day);
            }
        }

        @Override
        public boolean canConvert(Class type) {
            return SimpleDate.class.isAssignableFrom(type);
        }
    };
}

xStream.registerConverter(getSimpleDateConverter());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文