XStream
  1. XStream
  2. XSTR-86

Cannot correctly convert Calendar classes

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0.1
    • Fix Version/s: 1.0.2
    • Component/s: Converters
    • Labels:
      None

      Description

      1.0.1 does not correctly handle the serialization and deserialization of Calendar instances. This is probably because they declare some member variables without initializers, and rely on the constructors providing good values ...

      I've provided a test case and converter to solve the problem, but there may be a more elegant solution. The supplied converter simply uses a DateConverter to perform the work.

      TestCase
      =========
      public void testCalendarCanBeReconstructed()

      { Calendar cal = Calendar.getInstance(); XStream stream = new XStream(); String serializedCal = stream.toXML(cal); Calendar newCal = (Calendar) stream.fromXML(serializedCal); assertEquals(cal, newCal); System.out.println("serializedCal = " + serializedCal); System.out.println("newCal = " + newCal); // This will cause an NPE }

      Fix
      =======
      public class CalendarConverter extends AbstractBasicConverter {
      private DateConverter dateConverter = new DateConverter();

      public boolean canConvert(Class type)

      { return Calendar.class.isAssignableFrom(type); }

      protected Object fromString(String str)

      { Date date = (Date) dateConverter.fromString(str); Calendar instance = Calendar.getInstance(); instance.setTime(date); return instance; }

      protected String toString(Object obj)

      { Calendar cal = (Calendar) obj; return dateConverter.toString(cal.getTime()); }

      }

      Obviously, an addition to the DefaultConverterLookup is also required.

      ~Neil

        People

        • Assignee:
          Unassigned
          Reporter:
          Neil Clayton
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: