Re: Problem with DataFormat and XML Serialise/Deserialise

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri May 25 2007 - 05:14:01 EDT

  • Next message: Andrus Adamchik: "Re: Paging and SQL queries"

    Hi Adrian,

    I think you correctly identified the problem (encoding date via a
    'toString()' method is definitely wrong), and looks like the patch
    attached to CAY-792 should fix it by not including any textual output
    in the encoded date. There's one problem though - our existing users
    may have stored XML that was encoded using the previous default
    formatter. I guess I am going to add some failover strategy that
    would try a previous formatter in case of parse exceptions..

    On a side note - maybe we can improve encoder/decoder performance by
    introducing some "context" object that internally stores reusable
    thread-unsafe objects like formatters, instead of creating them for
    every field?

    Thanks
    Andrus

    On May 22, 2007, at 8:58 PM, Adrian Wiesmann wrote:

    > Hello
    >
    > I guess I found a problem within the XML Serialisation/Deserialisation
    > code when working with Dates. But before posting a patch or bug in
    > JIRA I
    > thought I'd make sure that I really found a problem and am not hunting
    > ghosts (the wrong ones :) ).
    >
    > The scenario is the following:
    >
    > Serialising a list of objects with the XMLEncoder:
    >
    >> xmlEncoder.encode(myId, dataObjectList);
    >
    > results in the date fields to be formatted like this:
    >
    > "Tue Mar 13 20:56:11 CET 2007"
    >
    > which basically represents this format:
    >
    > "EEE MMM dd HH:mm:ss zzz yyyy"
    >
    > That works like a charm. The resulting XML is well formatted.
    >
    > The problem is then, when you try to read back such a deserialised
    > list
    > with the help of the XMLDecoder into a DataContext:
    >
    >> XMLDecoder.decodeList(buffer, dataContext);
    >
    > Sometimes this resulted in our application in the following error:
    >
    >> Unparseable date: "Tue Mar 13 20:56:11 CET 2007"
    >
    > and first we thought this is an error with Windows because the
    > error only
    > happened on that system. But after doing some testing I think we have
    > a locale problem:
    >
    > The XMLDecoder decodes Dates like this:
    >
    >> formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    >> formatter.parse("Tue Mar 13 20:56:11 CET 2007");
    >
    > The problem is, that the SimpleDateFormat seems to use the locale
    > of the
    > respective system, although we define the mask it should use in its
    > constructor! I tried to use non lenient functionality:
    >
    >> formatter.setLenient(false);
    >
    > but that didn't work as well. Which basically means the XMLDecoder and
    > XMLEncoder are not supporting different locales.
    >
    > Can somebody confirm this behaviour? (And if yes, shouldn't we
    > change the
    > format of the date/time as well as the formatting within the
    > Decoder and
    > Encoder?)
    >
    > Regards,
    > Adrian
    >
    >
    > P.S: This is some snippet to test the problem:
    >
    > String strDate = "Tue Mar 13 20:56:11 CET 2007";
    > String strFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
    > DateFormat formatter = new SimpleDateFormat(strFormat, Locale.CHINA);
    >
    >
    > try {
    > System.out.println(formatter.parse(strDate));
    > } catch (ParseException e) {
    > e.printStackTrace();
    > }
    >
    > formatter = new SimpleDateFormat(strFormat, Locale.CHINA);
    > formatter.setLenient(false);
    > try {
    > System.out.println(formatter.parse(strDate));
    > } catch (ParseException e) {
    > e.printStackTrace();
    > }
    >
    > formatter = new SimpleDateFormat(strFormat, Locale.US);
    > try {
    > System.out.println(formatter.parse(strDate));
    > } catch (ParseException e) {
    > e.printStackTrace();
    > }
    >



    This archive was generated by hypermail 2.0.0 : Fri May 25 2007 - 05:14:29 EDT