Having backward compatibility for xstream in the following case

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 override the class.

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 problem of renaming variables, I will use an alias.

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

However, how can I find out that I am reading the old XML file and I will add +1 for the new month of reading field to change it from 0 to 1 based on 1?

+3
source share
2 answers
// 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());
0
source

Frequently Asked Questions xstream http://x-stream.imtqy.com/faq.html contains a section on how tp handles different versions.

+3
source

Source: https://habr.com/ru/post/1779759/


All Articles