Joda LocalDate and XStream

View: New views
4 Messages — Rating Filter:   Alert me  

Joda LocalDate and XStream

by jamcdonald :: Rate this Message:

| View Threaded | Show Only this Message

Round trip serialize/deserialize with XStream appears to subtract a day from a LocalDate. My workaround for this is to use a custom converter for LocalDate that formats/parses the LocalDate as a String.

My main concern is whether this is exposing a problem in either JodaTime or XStream that could affect other code.

    final LocalDate ld0 = new LocalDate(2008,07,03);    
    final XStream xs = new XStream();
    final String xml0 = xs.toXML(ld0);
    final LocalDate ld1 = (LocalDate) xs.fromXML(xml0);
    final String xml1 = xs.toXML(ld1);
    final LocalDate ld2 = (LocalDate) xs.fromXML(xml1);
    System.out.println(ld0 + '\n' + xml0 +
'\n' +
                       ld1 + '\n' + xml1 + '\n' +
                       ld2);

produces (note the second serialize/deserialize round trip preserves the date):

2008-07-03

<org.joda.time.LocalDate id="1">
  <iLocalMillis>1215043200000</iLocalMillis>
  <iChronology class="org.joda.time.chrono.ISOChronology" id="2"    
    resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
    <org.joda.time.chrono.ISOChronology_-Stub>
      <org.joda.time.tz.FixedDateTimeZone
        id="3" resolves-to="org.joda.time.DateTimeZone$Stub"/>
    </org.joda.time.chrono.ISOChronology_-Stub>
  </iChronology>
</org.joda.time.LocalDate>

2008-07-02

<org.joda.time.LocalDate id="1">
  <iLocalMillis>1215043200000</iLocalMillis>
  <iChronology class="org.joda.time.chrono.ISOChronology" id="2" 
    resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
    <org.joda.time.chrono.ISOChronology_-Stub>
      <org.joda.time.tz.CachedDateTimeZone id="3"
        resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
        <org.joda.time.DateTimeZone_-Stub>
          <string>America/Los_Angeles</string>
        </org.joda.time.DateTimeZone_-Stub>
      </org.joda.time.tz.CachedDateTimeZone>
    </org.joda.time.chrono.ISOChronology_-Stub>
  </iChronology>
</org.joda.time.LocalDate>

2008-07-02

 
--
John Alan McDonald
jamcdonald@...

RE: Joda LocalDate and XStream

by Jörg Schaible :: Rate this Message:

| View Threaded | Show Only this Message

Hi John,

________________________________

        From: jamcdonald@...
        Sent: Thursday, July 10, 2008 11:01 PM
        Subject: [xstream-user] Joda LocalDate and XStream
       
       
        Round trip serialize/deserialize with XStream appears to subtract a day from a LocalDate. My workaround for this is to use a custom converter for LocalDate that formats/parses the LocalDate as a String.
       
        My main concern is whether this is exposing a problem in either JodaTime or XStream that could affect other code.
       
            final LocalDate ld0 = new LocalDate(2008,07,03);    
            final XStream xs = new XStream();
            final String xml0 = xs.toXML(ld0);
            final LocalDate ld1 = (LocalDate) xs.fromXML(xml0);
            final String xml1 = xs.toXML(ld1);
            final LocalDate ld2 = (LocalDate) xs.fromXML(xml1);
            System.out.println(ld0 + '\n' + xml0 + '\n' +
                               ld1 + '\n' + xml1 + '\n' +
                               ld2);
       
        produces (note the second serialize/deserialize round trip preserves the date):
       
        2008-07-03
       
        <org.joda.time.LocalDate id="1">
          <iLocalMillis>1215043200000</iLocalMillis>
          <iChronology class="org.joda.time.chrono.ISOChronology" id="2"    
            resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
            <org.joda.time.chrono.ISOChronology_-Stub>
              <org.joda.time.tz.FixedDateTimeZone
                id="3" resolves-to="org.joda.time.DateTimeZone$Stub"/>
            </org.joda.time.chrono.ISOChronology_-Stub>
          </iChronology>
        </org.joda.time.LocalDate>
       
        2008-07-02
       
        <org.joda.time.LocalDate id="1">
          <iLocalMillis>1215043200000</iLocalMillis>
          <iChronology class="org.joda.time.chrono.ISOChronology" id="2"  
            resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
            <org.joda.time.chrono.ISOChronology_-Stub>
              <org.joda.time.tz.CachedDateTimeZone id="3"
                resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
                <org.joda.time.DateTimeZone_-Stub>
                  <string>America/Los_Angeles</string>
                </org.joda.time.DateTimeZone_-Stub>
              </org.joda.time.tz.CachedDateTimeZone>
            </org.joda.time.chrono.ISOChronology_-Stub>
          </iChronology>
        </org.joda.time.LocalDate>
       
        2008-07-02
________________________________

Works for me (TM). What version of joda-time you're using? However, problem might be ralted with your TZ having a negative offset. Can you run a short test with this:

=========== %< ============
                final LocalDate ld0 = new LocalDate(2008, 07, 03);
                final XStream xs = new XStream();
                final String xml0 = xs.toXML(ld0);
                final LocalDate ld1 = (LocalDate) xs.fromXML(xml0);
                final String xml1 = xs.toXML(ld1);
                final LocalDate ld2 = (LocalDate) xs.fromXML(xml1);
               
                LocalDate ld1s = null;
                LocalDate ld2s = null;
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(baos);
                    oos.writeObject(ld0);
                    oos.close();
                    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
                    ld1s = (LocalDate)ois.readObject();
                    ois.close();
                    baos = new ByteArrayOutputStream();
                    oos = new ObjectOutputStream(baos);
                    oos.writeObject(ld1s);
                    oos.close();
                    ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
                    ld2s = (LocalDate)ois.readObject();
                    ois.close();
                } catch (Exception e) {
                    // nothing
                }
                System.out.println(ld0.toString() + '\n' + xml0 + '\n'
                                   + ld1 + " (" + ld1s + ")\n" + xml1 + '\n'
                                   + ld2  + " (" + ld2s + ")\n");
=========== %< ============

For me it prints:

2008-07-03
<org.joda.time.LocalDate>
  <iLocalMillis>1215043200000</iLocalMillis>
  <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
    <org.joda.time.chrono.ISOChronology_-Stub>
      <org.joda.time.tz.FixedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub"/>
    </org.joda.time.chrono.ISOChronology_-Stub>
  </iChronology>
</org.joda.time.LocalDate>
2008-07-03 (2008-07-03)
<org.joda.time.LocalDate>
  <iLocalMillis>1215043200000</iLocalMillis>
  <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
    <org.joda.time.chrono.ISOChronology_-Stub>
      <org.joda.time.tz.CachedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
        <org.joda.time.DateTimeZone_-Stub>
          <string>Europe/Berlin</string>
        </org.joda.time.DateTimeZone_-Stub>
      </org.joda.time.tz.CachedDateTimeZone>
    </org.joda.time.chrono.ISOChronology_-Stub>
  </iChronology>
</org.joda.time.LocalDate>
2008-07-03 (2008-07-03)

I am curious about the dates in prackets using pure Java serialization ...

- Jörg

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



RE: Joda LocalDate and XStream

by jamcdonald :: Rate this Message:

| View Threaded | Show Only this Message

Thanks for looking into this. I'm using joda-time 1.5.2 and xstream 1.3.

I ran your code. Looks like it is a problem with joda-time:

--2008-07-11 10:43:25,375 JodaTimeUtilsTest.testSerializeLocalDate(253)--

2008-07-03
<org.joda.time.LocalDate>
  <iLocalMillis>1215043200000</iLocalMillis>
  <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
    <org.joda.time.chrono.ISOChronology_-Stub>
      <org.joda.time.tz.FixedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub"/>
    </org.joda.time.chrono.ISOChronology_-Stub>
  </iChronology>
</org.joda.time.LocalDate>
2008-07-02 (2008-07-03)
<org.joda.time.LocalDate>
  <iLocalMillis>1215043200000</iLocalMillis>
  <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
    <org.joda.time.chrono.ISOChronology_-Stub>
      <org.joda.time.tz.CachedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
        <org.joda.time.DateTimeZone_-Stub>
          <string>America/Los_Angeles</string>
        </org.joda.time.DateTimeZone_-Stub>
      </org.joda.time.tz.CachedDateTimeZone>
    </org.joda.time.chrono.ISOChronology_-Stub>
  </iChronology>
</org.joda.time.LocalDate>
2008-07-02 (2008-07-03)


--
John Alan McDonald
jamcdonald@...
 
-------------- Original message from Jörg Schaible <Joerg.Schaible@...>: --------------


> Hi John,
>
> ________________________________
>
> From: jamcdonald@...
> Sent: Thursday, July 10, 2008 11:01 PM
> Subject: [xstream-user] Joda LocalDate and XStream
>
> Round trip serialize/deserialize with XStream appears to subtract a day from a LocalDate. My workaround for this is to use a custom converter for
> LocalDate that formats/parses the LocalDate as a String.
> My main concern is whether this is exposing a problem in either JodaTime
> or XStream that could affect other code.
>
> final LocalDate ld0 = new LocalDate(2008,07,03);
> final XStream xs = new XStream();
> final String xml0 = xs.toXML(ld0);
> final LocalDate ld1 = (LocalDate) xs.fromXML(xml0);
> final String xml1 = xs.toXML(ld1);
> final LocalDate ld2 = (LocalDate) xs.fromXML(xml1);
> System.out.println(ld0 + '\n' + xml0 + '\n' +
> ld1 + '\n' + xml1 + '\n' +
> ld2);
> produces (note the second serialize/deserialize round trip preserves the
> date):
>
> 2008-07-03
>
>
> 1215043200000
>
> resolves-to="org.joda.time.chrono.ISOChronology$Stub"
> serialization="custom">
>
>
> id="3" resolves-to="org.joda.time.DateTimeZone$Stub"/>
>

>

>

>
> 2008-07-02
>
>
> 1215043200000
>
> resolves-to="org.joda.time.chrono.ISOChronology$Stub"
> serialization="custom">
>
>
> resolves-to="org.joda.time.DateTimeZone$Stub"
> serialization="custom">
>
> America/Los_Angeles
>

>

>

>

>

>
> 2008-07-02
> ________________________________
>
> Works for me (TM). What version of joda-time you're using? However, problem
> might be ralted with your TZ having a negative offset. Can you run a short test
> with this:
>
> =========== %< ============
> final LocalDate ld0 = new LocalDate(2008, 07, 03);
> final XStream xs = new XStream();
> final String xml0 = xs.toXML(ld0);
> final LocalDate ld1 = (LocalDate) xs.fromXML(xml0);
> final String xml1 = xs.toXML(ld1);
> final LocalDate ld2 = (LocalDate) xs.fromXML(xml1);
>
> LocalDate ld1s = null;
> LocalDate ld2s = null;
> try {
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> ObjectOutputStream oos = new ObjectOutputStream(baos);
> oos.writeObject(ld0);
> oos.close();
> ObjectInputStream ois = new ObjectInputStream(new
> ByteArrayInputStream(baos.toByteArray()));
> ld1s = (LocalDate)ois.readObject();
> ois.close();
> baos = new ByteArrayOutputStream();
> oos = new ObjectOutputStream(baos);
> oos.writeObject(ld1s);
> oos.close();
> ois = new ObjectInputStream(new
> ByteArrayInputStream(baos.toByteArray()));
> ld2s = (LocalDate)ois.readObject();
> ois.close();
> } catch (Exception e) {
> // nothing
> }
> System.out.println(ld0.toString() + '\n' + xml0 + '\n'
> + ld1 + " (" + ld1s + ")\n" + xml1 + '\n'
> + ld2 + " (" + ld2s + ")\n");
> =========== %< ============
>
> For me it prints:
>
> 2008-07-03
>
> 1215043200000
>
> resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
>
>
> resolves-to="org.joda.time.DateTimeZone$Stub"/>
>

>

>

> 2008-07-03 (2008-07-03)
>
> 1215043200000
>
> resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
>
>
> resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
>
> Europe/Berlin
>

>

>

>

>

> 2008-07-03 (2008-07-03)
>
> I am curious about the dates in prackets using pure Java serialization ...
>
> - Jörg
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>

RE: Joda LocalDate and XStream

by Jörg Schaible :: Rate this Message:

| View Threaded | Show Only this Message

Hi John,

________________________________

        From: jamcdonald@...
        Sent: Friday, July 11, 2008 7:47 PM
        Cc: Jörg Schaible
        Subject: RE: [xstream-user] Joda LocalDate and XStream
       
        Thanks for looking into this. I'm using joda-time 1.5.2 and xstream 1.3.
       
        I ran your code. Looks like it is a problem with joda-time:
       
        --2008-07-11 10:43:25,375 JodaTimeUtilsTest.testSerializeLocalDate(253)--
       
        2008-07-03
        <org.joda.time.LocalDate>
          <iLocalMillis>1215043200000</iLocalMillis>
          <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
            <org.joda.time.chrono.ISOChronology_-Stub>
              <org.joda.time.tz.FixedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub"/>
            </org.joda.time.chrono.ISOChronology_-Stub>
          </iChronology>
        </org.joda.time.LocalDate>
        2008-07-02 (2008-07-03)
        <org.joda.time.LocalDate>
          <iLocalMillis>1215043200000</iLocalMillis>
          <iChronology class="org.joda.time.chrono.ISOChronology" resolves-to="org.joda.time.chrono.ISOChronology$Stub" serialization="custom">
            <org.joda.time.chrono.ISOChronology_-Stub>
              <org.joda.time.tz.CachedDateTimeZone resolves-to="org.joda.time.DateTimeZone$Stub" serialization="custom">
                <org.joda.time.DateTimeZone_-Stub>
                  <string>America/Los_Angeles</string>
                </org.joda.time.DateTimeZone_-Stub>
              </org.joda.time.tz.CachedDateTimeZone>
            </org.joda.time.chrono.ISOChronology_-Stub>
          </iChronology>
        </org.joda.time.LocalDate>
        2008-07-02 (2008-07-03)
________________________________

From what I can see, it is an incompatibility between the Java serialization and the way XStream supports it. Look at the dates in the brackets.  They are still correct after Java serialization.  For some reason the deserialized LocalDate of XStream suddenly contains an iChronolgy insance that uses a cached DateTimeZone of "America/Los Angeles", while the original LocalDate contains a FixedDateTimeZone. That one survives the JDK serialization, but not in XStream.

Can you open a JIRA issue about this?

- Jörg

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email