2007-10-26 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Fri, 26 Oct 2007 04:00:22 +0000 (04:00 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Fri, 26 Oct 2007 04:00:22 +0000 (04:00 -0000)
* TypeTranslator.cs, XmlCustomFormatter.cs : looks like duration is
  serialized to System.String, not TimeSpan. Fixed bug #336625.

* XmlSerializerTests.cs : added test for DataType in [XmlElement].

svn path=/trunk/mcs/; revision=88247

mcs/class/System.XML/System.Xml.Serialization/ChangeLog
mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs
mcs/class/System.XML/System.Xml.Serialization/XmlCustomFormatter.cs
mcs/class/System.XML/Test/System.Xml.Serialization/ChangeLog
mcs/class/System.XML/Test/System.Xml.Serialization/XmlSerializerTests.cs

index cfd9123006dd0a3acb11f871fc28e9a6a969c886..69e4b9988fbff960a472cd32761c127d5d0e69aa 100644 (file)
@@ -1,3 +1,8 @@
+2007-10-26  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * TypeTranslator.cs, XmlCustomFormatter.cs : looks like duration is
+         serialized to System.String, not TimeSpan. Fixed bug #336625.
+
 2007-09-27  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlMapping.cs : added XsdElementName.
index 01139433874bb7627c8936ccf911a9bd940f4fcb..efda1962c4eb6727de7fa1118a79421d9240ad71 100644 (file)
@@ -108,7 +108,8 @@ namespace System.Xml.Serialization
                        nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
                        nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
                        nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
-                       nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
+                       // The key type is wrong (must be string), but otherwise it cannot be added to Hashtable!
+                       nameCache.Add (typeof (TimeSpan), new TypeData (typeof (string), "duration", true));
 
                        primitiveTypes = new Hashtable();
                        ICollection types = nameCache.Values;
index 5dfa05f05723851e80d525ac64702b1f97b95121..d34649e0b97ab3bf6eea9df363bd8db021742a56 100644 (file)
@@ -252,7 +252,7 @@ namespace System.Xml.Serialization {
                                case "base64":
                                case "base64Binary": return value == null ? String.Empty : Convert.ToBase64String ((byte[])value);
                                case "hexBinary": return value == null ? String.Empty : XmlConvert.ToBinHexString ((byte[]) value);
-                               case "duration": return XmlConvert.ToString ((TimeSpan) value);
+                               case "duration": return (string) value;
                        default: return value is IFormattable ? ((IFormattable) value).ToString (null, CultureInfo.InvariantCulture) : value.ToString ();
                        }
                }
@@ -287,7 +287,7 @@ namespace System.Xml.Serialization {
                                case "base64":
                                case "base64Binary": return Convert.FromBase64String (value);
                                case "hexBinary": return XmlConvert.FromBinHexString (value);
-                               case "duration": return XmlConvert.ToTimeSpan (value);
+                               case "duration": return value;
                                default: 
                                        if (type.Type != null)
                                                return Convert.ChangeType (value, type.Type);
@@ -336,7 +336,7 @@ namespace System.Xml.Serialization {
                                case "base64":
                                case "base64Binary": return value + " == null ? String.Empty : Convert.ToBase64String (" + value + ")";
                                case "hexBinary": return value + " == null ? String.Empty : ToBinHexString (" + value + ")";
-                               case "duration": return "XmlConvert.ToString (" + value + ")";
+                               case "duration": return value;
                                case "NMTOKEN":
                                case "Name":
                                case "NCName":
@@ -390,7 +390,7 @@ namespace System.Xml.Serialization {
                                case "base64:":
                                case "base64Binary": return "Convert.FromBase64String (" + value + ")";
                                case "hexBinary": return "FromBinHexString (" + value + ")";
-                               case "duration": return "XmlConvert.ToTimeSpan (" + value + ")";
+                               case "duration": return value;
                                default: return value;
                        }
                }
index 7b9a171d50fc5309197038176243f6894e9e3bd7..951c63ebc7472d89d62c3e2accb302c88070bc99 100644 (file)
@@ -1,3 +1,7 @@
+2007-10-26  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlSerializerTests.cs : added test for DataType in [XmlElement].
+
 2007-09-25  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlSerializerTests.cs : another XmlSchemaProvider test; use it
index f945d95bf6712d195588a0bd7d75c7d7cbc735ae..47e12225f61b46412687ffb56944fbca2cd272bb 100644 (file)
@@ -2291,6 +2291,29 @@ namespace MonoTests.System.XmlSerialization
                        Assert.AreEqual (Infoset (expected), WriterText);
                }
 
+               [Test]
+               public void SerializeDurationToString ()
+               {
+                       XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer1));
+                       ser.Serialize (TextWriter.Null, new TimeSpanContainer1 ());
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void SerializeDurationToTimeSpan ()
+               {
+                       XmlSerializer ser = new XmlSerializer (typeof (TimeSpanContainer2));
+                       ser.Serialize (TextWriter.Null, new TimeSpanContainer2 ());
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void SerializeInvalidDataType ()
+               {
+                       XmlSerializer ser = new XmlSerializer (typeof (InvalidTypeContainer));
+                       ser.Serialize (TextWriter.Null, new InvalidTypeContainer ());
+               }
+
                #region GenericsSeralizationTests
 
 #if NET_2_0
@@ -2762,6 +2785,24 @@ namespace MonoTests.System.XmlSerialization
                        public string Value;
                }
 
+               public class InvalidTypeContainer
+               {
+                       [XmlElement (DataType = "invalid")]
+                       public string InvalidTypeItem = "aaa";
+               }
+
+               public class TimeSpanContainer1
+               {
+                       [XmlElement (DataType = "duration")]
+                       public string StringDuration = "aaa";
+               }
+
+               public class TimeSpanContainer2
+               {
+                       [XmlElement (DataType = "duration")]
+                       public TimeSpan StringDuration = TimeSpan.FromSeconds (1);
+               }
+
 #if NET_2_0
                public class Bug80759
                {