Title: [2339] trunk: Support deserialization ofW3C datetime format in DateConverter with Java 7 runtime (XSTR-759).
Revision
2339
Author
joehni
Date
2015-02-16 16:58:19 -0600 (Mon, 16 Feb 2015)

Log Message

Support deserialization ofW3C datetime format in DateConverter with Java 7 runtime (XSTR-759).

Modified Paths

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/basic/DateConverter.java (2338 => 2339)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/basic/DateConverter.java	2015-02-15 11:31:34 UTC (rev 2338)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/basic/DateConverter.java	2015-02-16 22:58:19 UTC (rev 2339)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2003, 2004 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2009, 2011, 2012, 2013, 2014 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2009, 2011, 2012, 2013, 2014, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -36,6 +36,11 @@
  * is localized since Java 6.
  * </p>
  * <p>
+ * Using a Java 7 runtime or higher, the converter supports the <a href=""
+ * format defined by W3C</a> (a subset of ISO 8601) at deserialization. Only the formats that also contain the time
+ * information.
+ * </p>
+ * <p>
  * Dates in a different era are using a special default pattern that contains the era itself.
  * </p>
  * 
@@ -69,6 +74,11 @@
         if (!utcSupported) {
             acceptablePatterns.add("yyyy-MM-dd HH:mm:ss 'UTC'");
         }
+        if (JVM.canParseISO8601TimeZoneInDateFormat()) {
+            acceptablePatterns.add("yyyy-MM-dd'T'HH:mm:ss.SX");
+            acceptablePatterns.add("yyyy-MM-dd'T'HH:mm:ssX");
+            acceptablePatterns.add("yyyy-MM-dd'T'HH:mmX");
+        }
         // backwards compatibility
         acceptablePatterns.add("yyyy-MM-dd HH:mm:ssa");
         DEFAULT_ACCEPTABLE_FORMATS = acceptablePatterns.toArray(new String[acceptablePatterns.size()]);

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/core/JVM.java (2338 => 2339)


--- trunk/xstream/src/java/com/thoughtworks/xstream/core/JVM.java	2015-02-15 11:31:34 UTC (rev 2338)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/core/JVM.java	2015-02-16 22:58:19 UTC (rev 2339)
@@ -48,6 +48,7 @@
     private static final boolean optimizedTreeSetAddAll;
     private static final boolean optimizedTreeMapPutAll;
     private static final boolean canParseUTCDateFormat;
+    private static final boolean canParseISO8601TimeZoneInDateFormat;
     private static final boolean canCreateDerivedObjectOutputStream;
 
     private static final String vendor = System.getProperty("java.vm.vendor");
@@ -165,6 +166,15 @@
         }
         canParseUTCDateFormat = test;
         try {
+            new SimpleDateFormat("X").parse("Z");
+            test = true;
+        } catch (final ParseException e) {
+            test = false;
+        } catch (final IllegalArgumentException e) {
+            test = false;
+        }
+        canParseISO8601TimeZoneInDateFormat = test;
+        try {
             test = new CustomObjectOutputStream(null) != null;
         } catch (final RuntimeException e) {
             test = false;
@@ -486,6 +496,13 @@
     }
 
     /**
+     * @since upcoming
+     */
+    public static boolean canParseISO8601TimeZoneInDateFormat() {
+        return canParseISO8601TimeZoneInDateFormat;
+    }
+
+    /**
      * @since 1.4.6
      */
     public static boolean canCreateDerivedObjectOutputStream() {

Modified: trunk/xstream/src/test/com/thoughtworks/xstream/converters/basic/DateConverterTest.java (2338 => 2339)


--- trunk/xstream/src/test/com/thoughtworks/xstream/converters/basic/DateConverterTest.java	2015-02-15 11:31:34 UTC (rev 2338)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/converters/basic/DateConverterTest.java	2015-02-16 22:58:19 UTC (rev 2339)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004, 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2009, 2012, 2014 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2009, 2012, 2014, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -12,6 +12,7 @@
 package com.thoughtworks.xstream.converters.basic;
 
 import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.core.JVM;
 import com.thoughtworks.xstream.testutil.TimeZoneChanger;
 
 import junit.framework.TestCase;
@@ -84,6 +85,15 @@
         assertEquals(expected, converter.fromString("2004-02-22 20:16:04.0 UTC"));
         assertEquals(expected, converter.fromString("2004-02-23 01:46:04.0 IST"));
         assertEquals(expected, converter.fromString("2004-02-23 01:46:04.0 GMT+05:30"));
+        
+        if (JVM.canParseISO8601TimeZoneInDateFormat()) {
+            // W3C subset of ISO 8601 date time representations
+            assertEquals(expected, converter.fromString("2004-02-22T15:16:04-05:00"));
+            assertEquals(expected, converter.fromString("2004-02-22T20:16:04Z"));
+            assertEquals(expected, converter.fromString("2004-02-22T20:16:04.0Z"));
+            expected.setTime(expected.getTime()-4000);
+            assertEquals(expected, converter.fromString("2004-02-22T15:16-05:00"));
+        }
     }
 
     public void testUnmarshalsDateWithDifferentDefaultTimeZones() throws ParseException {

Modified: trunk/xstream-distribution/src/content/changes.html (2338 => 2339)


--- trunk/xstream-distribution/src/content/changes.html	2015-02-15 11:31:34 UTC (rev 2338)
+++ trunk/xstream-distribution/src/content/changes.html	2015-02-16 22:58:19 UTC (rev 2339)
@@ -70,6 +70,8 @@
     	<li>XSTR-761: Support ignored serialPersistentField at deserialization time.</li>
     	<li>XSTR-755: ExternalizableConverter does not respect writeReplace and readResolve.</li>
     	<li>XSTR-757: Deserialized TreeSet does not honor remove(Object) return value contract.</li>
+    	<li>XSTR-759: Support deserialization of <a href="" datetime format</a>
+    	in DateConverter with Java 7 runtime.</li>
     	<li>Fix: DateConverter ignores provided locale.</li>
     	<li>Fix: WeakCache.entrySet().iterator().next.setValue(value) returns the reference instead of the old value.</li>
     	<li>Fix: SqlTimestampConverter throws IllegalArgumentException instead of ConversionException on fromString().</li>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to