diff --git a/src/deuxsucres.XSerializer.Tests/TestClasses/TestClassSimple.cs b/src/deuxsucres.XSerializer.Tests/TestClasses/TestClassSimple.cs index a219170..e3f5d45 100644 --- a/src/deuxsucres.XSerializer.Tests/TestClasses/TestClassSimple.cs +++ b/src/deuxsucres.XSerializer.Tests/TestClasses/TestClassSimple.cs @@ -15,5 +15,6 @@ public class TestClassSimple public Int64 Value5 { get { return _Value5; } } public String Value6 = null; public TestClassSimple Value7 = null; + public TestClassSimple Value8 { get; set; } } } diff --git a/src/deuxsucres.XSerializer.Tests/XDocSerializerTests.cs b/src/deuxsucres.XSerializer.Tests/XDocSerializerTests.cs index 8421497..d9e420f 100644 --- a/src/deuxsucres.XSerializer.Tests/XDocSerializerTests.cs +++ b/src/deuxsucres.XSerializer.Tests/XDocSerializerTests.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -41,6 +42,23 @@ public void TestGetSetCulture() #region Serialization + [Fact] + public void TestSerializeValues() + { + var serializer = new XDocSerializer(); + + // Value types are not supported + Assert.Throws(() => serializer.Serialize(123)); + Assert.Throws(() => serializer.Serialize(123.456)); + Assert.Throws(() => serializer.Serialize(DateTime.Now)); + Assert.Throws(() => serializer.Serialize(true)); + Assert.Throws(() => serializer.Serialize("Text")); + + // Invalid arguments + Assert.Throws(() => serializer.Serialize(null, (XElement)null)); + Assert.Throws(() => serializer.Serialize(new TestClassSimple(), (XElement)null)); + } + [Fact] public void TestSerializeClass() { @@ -52,11 +70,11 @@ public void TestSerializeClass() Value3 = 67.89, Value4 = dt }); - Assert.Equal("67.89232014-06-08 11:44:56Z", node1.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1", node1.ToString(SaveOptions.DisableFormatting)); Assert.Throws(() => serializer.Serialize(123)); Assert.Throws(() => serializer.Serialize(null)); Assert.Throws(() => serializer.Serialize(new TestClassSimple(), String.Empty)); - + var node2 = new XElement("root"); serializer.Serialize(new TestClassSimple() { Value1 = null, @@ -64,9 +82,7 @@ public void TestSerializeClass() Value3 = 67.89, Value4 = dt }, node2); - Assert.Equal("67.89232014-06-08 11:44:56Z", node2.ToString(SaveOptions.DisableFormatting)); - Assert.Throws(() => serializer.Serialize(null, (XElement)null)); - Assert.Throws(() => serializer.Serialize(new TestClassSimple(), (XElement)null)); + Assert.Equal("67.89232014-06-08 11:44:56Z1", node2.ToString(SaveOptions.DisableFormatting)); } [Fact] @@ -91,16 +107,23 @@ public void TestSerializeEnumerable() var list = new List(array); var node1 = serializer.Serialize(array); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node1.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node1.ToString(SaveOptions.DisableFormatting)); node1 = serializer.Serialize(list); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node1.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node1.ToString(SaveOptions.DisableFormatting)); + + node1 = serializer.Serialize(new String[] { "Un", "Deux", "Trois" }); + Assert.Equal("UnDeuxTrois", node1.ToString(SaveOptions.DisableFormatting)); var node2 = new XElement("root"); serializer.Serialize(array, node2); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node2.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node2.ToString(SaveOptions.DisableFormatting)); node2 = new XElement("root"); serializer.Serialize(list, node2); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node2.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node2.ToString(SaveOptions.DisableFormatting)); + + node2 = new XElement("roots"); + serializer.Serialize(new String[] { "Un", "Deux", "Trois" }, node2); + Assert.Equal("UnDeuxTrois", node2.ToString(SaveOptions.DisableFormatting)); } [Fact] @@ -125,16 +148,16 @@ public void TestSerializeNonTypedEnumerable() System.Collections.IEnumerable list = new List(array); var node1 = serializer.Serialize(array); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node1.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node1.ToString(SaveOptions.DisableFormatting)); node1 = serializer.Serialize(list); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node1.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node1.ToString(SaveOptions.DisableFormatting)); var node2 = new XElement("root"); serializer.Serialize(array, node2); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node2.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node2.ToString(SaveOptions.DisableFormatting)); node2 = new XElement("root"); serializer.Serialize(list, node2); - Assert.Equal("67.89232014-06-08 11:44:56Z-12.67Texte982014-06-08 11:44:56Z", node2.ToString(SaveOptions.DisableFormatting)); + Assert.Equal("67.89232014-06-08 11:44:56Z1-12.67Texte982014-06-08 11:44:56Z1", node2.ToString(SaveOptions.DisableFormatting)); } [Fact] @@ -175,6 +198,86 @@ public void TestSerializeDictionary() "Un" + "2" + "2014-06-08 11:44:56Z" + + "1" + + "" + + "" + + "Un" + + "4" + + "78.89" + + "2014-06-08 11:44:56Z" + + "" + + "", + ser + ); + + } + + [Fact] + public void TestAnonymousObject() + { + var target = new XDocSerializer(); + + var obj1 = new { + val1 = "ABC", + value1 = "Text", + VALUE2 = 987, + value3 = 123.456, + value4 = new DateTime(2014, 6, 8, 11, 44, 56), + value5 = new TestClassSimple() { + Value1 = "Un", + Value2 = 2, + Value3 = 2.4, + Value4 = new DateTime(2014, 6, 8, 11, 44, 56) + }, + value6 = new { + val1 = "Un", + val2 = 4, + val3 = 78.89, + val4 = new DateTime(2014, 6, 8, 11, 44, 56) + } + }; + + var ser = target.Serialize(obj1).ToString(SaveOptions.DisableFormatting); + + Assert.Equal( + "" + + "ABC" + + "Text" + + "987" + + "123.456" + + "2014-06-08 11:44:56Z" + + "" + + "2.4" + + "Un" + + "2" + + "2014-06-08 11:44:56Z" + + "1" + + "" + + "" + + "Un" + + "4" + + "78.89" + + "2014-06-08 11:44:56Z" + + "" + + "", + ser + ); + + ser = target.Serialize(obj1, "root").ToString(SaveOptions.DisableFormatting); + + Assert.Equal( + "" + + "ABC" + + "Text" + + "987" + + "123.456" + + "2014-06-08 11:44:56Z" + + "" + + "2.4" + + "Un" + + "2" + + "2014-06-08 11:44:56Z" + + "1" + "" + "" + "Un" + @@ -206,7 +309,7 @@ public void TestPopulate() Assert.Equal(1, val.Value5); val = new TestClassSimple(); - serializer.Populate(XDocument.Parse("").Root, val); + serializer.Populate("", val); Assert.Equal("Text", val.Value1); Assert.Equal(987, val.Value2); Assert.Equal(123.456, val.Value3); @@ -243,7 +346,7 @@ public void TestPopulateArray() Assert.Equal("123.456", arr1[2]); arr1 = new String[8]; - serializer.Populate(XDocument.Parse("Text987123.4562014-06-08 11:44:56123").Root, arr1); + serializer.Populate("Text987123.4562014-06-08 11:44:56123", arr1); Assert.Equal("Text", arr1[0]); Assert.Equal("987", arr1[1]); Assert.Equal("123.456", arr1[2]); @@ -254,7 +357,7 @@ public void TestPopulateArray() Assert.Equal(null, arr1[7]); // Null arguments - Assert.Throws(() => serializer.Populate(null, "")); + Assert.Throws(() => serializer.Populate((XElement)null, "")); Assert.Throws(() => serializer.Populate(XDocument.Parse("Text").Root, null)); // With string Assert.Throws(() => serializer.Populate(XDocument.Parse("Text").Root, "")); @@ -291,14 +394,14 @@ public void TestPopulateDictionary() Assert.Equal("ABC", val["val1"]); Assert.Equal("Text", val["value1"]); Assert.Equal((Int64)987, val["value2"]); - Assert.Equal(123.456, val["value3"]); + Assert.Equal(123.456m, val["value3"]); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), val["value4"]); Assert.IsType>(val["value5"]); var dic2 = (Dictionary)val["value5"]; Assert.Equal("Un", dic2["val1"]); Assert.Equal((Int64)2, dic2["val2"]); - Assert.Equal(2.4, dic2["val3"]); + Assert.Equal(2.4m, dic2["val3"]); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), dic2["val4"]); } @@ -310,8 +413,8 @@ public void TestPopulateDictionary() public void TestDeserializeError() { var serializer = new XDocSerializer(); - Assert.Throws(() => serializer.Deserialize(null)); - Assert.Throws(() => serializer.Deserialize(null)); + Assert.Throws(() => serializer.Deserialize((XElement)null)); + Assert.Throws(() => serializer.Deserialize((XElement)null)); } [Fact] @@ -333,89 +436,147 @@ public void TestDeserializeBool() Assert.Equal(true, serializer.Deserialize(XDocument.Parse("true").Root)); Assert.Equal(false, serializer.Deserialize(XDocument.Parse("FALSE").Root)); Assert.Equal(true, serializer.Deserialize(XDocument.Parse("true").Root)); - Assert.Equal(false, serializer.Deserialize(XDocument.Parse("false").Root)); + Assert.Equal(false, serializer.Deserialize("false")); } [Fact] - public void TestDeserializeInt16() + public void TestDeserializeInt() { var serializer = new XDocSerializer(); - Assert.Equal(-16, serializer.Deserialize(XDocument.Parse("-16").Root)); - Assert.Equal((Int64)(-16), serializer.Deserialize(XDocument.Parse("-16").Root)); - } + // Int16 + Assert.Equal((Int16)(-16), serializer.Deserialize(XDocument.Parse("-16").Root)); + Assert.Equal((Int16)(-16), serializer.Deserialize(XDocument.Parse("-16").Root, typeof(Int16))); + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("test").Root)); - [Fact] - public void TestDeserializeInt32() - { - var serializer = new XDocSerializer(); + // Int16? + Assert.Equal((Int16?)-16, serializer.Deserialize(XDocument.Parse("-16").Root)); + Assert.Equal((Int16?)-16, serializer.Deserialize("-16", typeof(Int16?))); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Int32 Assert.Equal(32, serializer.Deserialize(XDocument.Parse("32").Root)); - Assert.Equal((Int64)32, serializer.Deserialize(XDocument.Parse("32").Root)); - } + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("").Root)); - [Fact] - public void TestDeserializeInt64() - { - var serializer = new XDocSerializer(); + // Int32? + Assert.Equal(32, serializer.Deserialize(XDocument.Parse("32").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Int64 Assert.Equal(64, serializer.Deserialize(XDocument.Parse("64").Root)); - } + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("test").Root)); - [Fact] - public void TestDeserializeUInt16() - { - var serializer = new XDocSerializer(); - Assert.Equal((UInt16)16, serializer.Deserialize(XDocument.Parse("16").Root)); + // Int64? + Assert.Equal(64, serializer.Deserialize(XDocument.Parse("64").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Non typed + Assert.Equal((Int64)(-16), serializer.Deserialize(XDocument.Parse("-16").Root)); } [Fact] - public void TestDeserializeUInt32() + public void TestDeserializeUInt() { var serializer = new XDocSerializer(); + // UInt16 + Assert.Equal(16, serializer.Deserialize(XDocument.Parse("16").Root)); + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("-16").Root)); + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("test").Root)); + + // UInt16? + Assert.Equal((UInt16?)16, serializer.Deserialize(XDocument.Parse("16").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("-16").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // UInt32 Assert.Equal((UInt32)32, serializer.Deserialize(XDocument.Parse("32").Root)); - } + Assert.Equal((UInt32)0, serializer.Deserialize(XDocument.Parse("").Root)); - [Fact] - public void TestDeserializeUInt64() - { - var serializer = new XDocSerializer(); + // UInt32? + Assert.Equal((UInt32?)32, serializer.Deserialize(XDocument.Parse("32").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // UInt64 Assert.Equal((UInt64)64, serializer.Deserialize(XDocument.Parse("64").Root)); - } + Assert.Equal((UInt64)0, serializer.Deserialize(XDocument.Parse("test").Root)); - [Fact] - public void TestDeserializeSingle() - { - var serializer = new XDocSerializer(); - Assert.Equal((Single)123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); - Assert.Equal((Double)123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); + // UInt64? + Assert.Equal((UInt64?)64, serializer.Deserialize(XDocument.Parse("64").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); } [Fact] - public void TestDeserializeDouble() + public void TestDeserializeFloat() { var serializer = new XDocSerializer(); - Assert.Equal((Double)123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); - Assert.Equal((Double)123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); - } + // Single + Assert.Equal((Single)(-16), serializer.Deserialize(XDocument.Parse("-16").Root)); + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("test").Root)); - [Fact] - public void TestDeserializeDecimal() - { - var serializer = new XDocSerializer(); + // Single? + Assert.Equal((Single)123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); + Assert.Equal((Single)12345, serializer.Deserialize(XDocument.Parse("123,45").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Double + Assert.Equal(123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("").Root)); + + // Double? + Assert.Equal(123.45, serializer.Deserialize(XDocument.Parse("123.45").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Devimal Assert.Equal(123.45m, serializer.Deserialize(XDocument.Parse("123.45").Root)); + Assert.Equal(0, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Decimal? + Assert.Equal(123.45m, serializer.Deserialize(XDocument.Parse("123.45").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Non typed + Assert.Equal(123.45m, serializer.Deserialize(XDocument.Parse("123.45").Root)); + Assert.Equal(12345L, serializer.Deserialize(XDocument.Parse("123,45").Root)); + + // Not invariant culture + serializer.Culture = CultureInfo.GetCultureInfo("fr-fr"); + Assert.Equal((Single)123.45, serializer.Deserialize(XDocument.Parse("123,45").Root)); + Assert.Equal(123.45m, serializer.Deserialize(XDocument.Parse("123,45").Root)); + } [Fact] public void TestDeserializeDateTime() { var serializer = new XDocSerializer(); + // DateTime Assert.Equal(new DateTime(2014, 8, 6), serializer.Deserialize(XDocument.Parse("2014/08/06").Root)); Assert.Equal(new DateTime(2014, 8, 6, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014/08/06 11:44:56").Root)); Assert.Equal(new DateTime(2014, 6, 8), serializer.Deserialize(XDocument.Parse("2014-06-08").Root)); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014-06-08 11:44:56").Root)); - + Assert.Equal(new DateTime(2014, 8, 6, 11, 44, 56), serializer.Deserialize(XDocument.Parse("08/06/2014 11:44:56").Root)); + Assert.Equal(DateTime.MinValue, serializer.Deserialize(XDocument.Parse("test").Root)); + + // DateTime? + Assert.Equal(new DateTime(2014, 8, 6), serializer.Deserialize(XDocument.Parse("2014/08/06").Root)); + Assert.Equal(new DateTime(2014, 8, 6, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014/08/06 11:44:56").Root)); + Assert.Equal(new DateTime(2014, 6, 8), serializer.Deserialize(XDocument.Parse("2014-06-08").Root)); + Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014-06-08 11:44:56").Root)); + Assert.Equal(new DateTime(2014, 8, 6, 11, 44, 56), serializer.Deserialize(XDocument.Parse("08/06/2014 11:44:56").Root)); + Assert.Equal(null, serializer.Deserialize(XDocument.Parse("test").Root)); + + // Non typed Assert.Equal(new DateTime(2014, 8, 6), serializer.Deserialize(XDocument.Parse("2014/08/06").Root)); Assert.Equal(new DateTime(2014, 8, 6, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014/08/06 11:44:56").Root)); Assert.Equal(new DateTime(2014, 6, 8), serializer.Deserialize(XDocument.Parse("2014-06-08").Root)); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014-06-08 11:44:56").Root)); + Assert.Equal(new DateTime(2014, 8, 6, 11, 44, 56), serializer.Deserialize(XDocument.Parse("08/06/2014 11:44:56").Root)); + + // Not invariant culture + serializer.Culture = CultureInfo.GetCultureInfo("fr-fr"); + Assert.Equal(new DateTime(2014, 8, 6), serializer.Deserialize(XDocument.Parse("2014/08/06").Root)); + Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), serializer.Deserialize(XDocument.Parse("2014-06-08 11:44:56").Root)); + Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), serializer.Deserialize(XDocument.Parse("08/06/2014 11:44:56").Root)); + Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), serializer.Deserialize(XDocument.Parse("08/06/2014 11:44:56").Root)); } #endregion @@ -460,7 +621,7 @@ public void TestDeserializeClass() Assert.Equal(new DateTime(2014, 12, 3, 19, 43, 56), val.Value7.Value4); Assert.Equal(1, val.Value7.Value5); - val = serializer.Deserialize(XDocument.Parse("").Root); + val = serializer.Deserialize(""); Assert.Equal("Text", val.Value1); Assert.Equal(987, val.Value2); Assert.Equal(123.456, val.Value3); @@ -468,11 +629,14 @@ public void TestDeserializeClass() Assert.Equal(1, val.Value5); // Null arguments - Assert.Throws(() => serializer.Deserialize(null, typeof(TestClassSimple))); + Assert.Throws(() => serializer.Deserialize((XElement)null, typeof(TestClassSimple))); Assert.Throws(() => serializer.Deserialize(XDocument.Parse("Text").Root, null)); // Attribute to object - Assert.Throws(() => serializer.Deserialize(XDocument.Parse("Text").Root, null)); + Exception ex = Assert.Throws(() => serializer.Deserialize(XDocument.Parse("Text").Root)); + Assert.Equal("Type 'deuxsucres.XSerializer.Tests.TestClasses.TestClassSimple' can't be deserialized from an attribute value.", ex.Message); + ex = Assert.Throws(() => serializer.Deserialize(XDocument.Parse("Text").Root)); + Assert.Equal("Type 'deuxsucres.XSerializer.Tests.TestClasses.TestClassSimple' can't be deserialized from an attribute value.", ex.Message); } [Fact] @@ -546,29 +710,29 @@ public void TestDeserializeDictionary() Assert.Equal("ABC", dic1["val1"]); Assert.Equal("Text", dic1["value1"]); Assert.Equal((Int64)987, dic1["VALUE2"]); - Assert.Equal(123.456, dic1["value3"]); + Assert.Equal(123.456m, dic1["value3"]); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), dic1["value4"]); Assert.IsType>(dic1["value5"]); var sdic1 = (Dictionary)dic1["value5"]; Assert.Equal("Un", sdic1["val1"]); Assert.Equal((Int64)2, sdic1["val2"]); - Assert.Equal(2.4, sdic1["val3"]); + Assert.Equal(2.4m, sdic1["val3"]); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), sdic1["val4"]); - var dic2 = serializer.Deserialize>(XDocument.Parse( + var dic2 = serializer.Deserialize>( @" Text 987 123.456 2014-06-08 11:44:56 - Un - 2 - 2.4 - 2014-06-08 11:44:56 + Un + 2 + 2.4 + 2014-06-08 11:44:56 -").Root); +"); Assert.Equal( new string[] { "val1", "value1", "VALUE2", "value3", "value4", "value5" }, dic1.Keys.ToArray() @@ -576,14 +740,14 @@ public void TestDeserializeDictionary() Assert.Equal("ABC", dic2["val1"]); Assert.Equal("Text", dic2["value1"]); Assert.Equal((Int64)987, dic2["value2"]); - Assert.Equal(123.456, dic2["value3"]); + Assert.Equal(123.456m, dic2["value3"]); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), dic2["value4"]); Assert.IsType>(dic2["value5"]); sdic1 = (Dictionary)dic2["value5"]; Assert.Equal("Un", sdic1["val1"]); Assert.Equal((Int64)2, sdic1["val2"]); - Assert.Equal(2.4, sdic1["val3"]); + Assert.Equal(2.4m, sdic1["val3"]); Assert.Equal(new DateTime(2014, 6, 8, 11, 44, 56), sdic1["val4"]); } diff --git a/src/deuxsucres.XSerializer/Properties/AssemblyInfo.cs b/src/deuxsucres.XSerializer/Properties/AssemblyInfo.cs index 483a353..2c30709 100644 --- a/src/deuxsucres.XSerializer/Properties/AssemblyInfo.cs +++ b/src/deuxsucres.XSerializer/Properties/AssemblyInfo.cs @@ -26,5 +26,5 @@ // Vous pouvez spécifier toutes les valeurs ou utiliser par défaut les numéros de build et de version // en utilisant '*', comme indiqué ci-dessous : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] +[assembly: AssemblyFileVersion("1.0.1.0")] diff --git a/src/deuxsucres.XSerializer/XDocSerializer.cs b/src/deuxsucres.XSerializer/XDocSerializer.cs index 7a1284b..30fdc8b 100644 --- a/src/deuxsucres.XSerializer/XDocSerializer.cs +++ b/src/deuxsucres.XSerializer/XDocSerializer.cs @@ -47,6 +47,14 @@ protected virtual String SingularizeName(String name) return name; } + /// + /// Clean the name to be a valid XML tag name + /// + protected virtual String CleanupName(String name) + { + return System.Text.RegularExpressions.Regex.Replace(name, @"[^\w-]", String.Empty); + } + /// /// Pluralize a name /// @@ -69,10 +77,10 @@ protected virtual String NodeNameFromType(Type type) { if (type.IsArray) { - var r = NodeNameFromType(type.GetElementType()); + var r = CleanupName(NodeNameFromType(type.GetElementType())); return PluralizeName(r); } - else + else if (!type.IsValueType && type != typeof(String)) { if (typeof(IEnumerable).IsAssignableFrom(type)) { @@ -84,7 +92,7 @@ protected virtual String NodeNameFromType(Type type) Type tintArgType = tint.GetGenericArguments()[0]; if (tintArgType != typeof(Object)) { - var r = NodeNameFromType(tintArgType); + var r = CleanupName(NodeNameFromType(tintArgType)); if (!r.EndsWith("s")) r += "s"; return PluralizeName(r); } @@ -92,7 +100,7 @@ protected virtual String NodeNameFromType(Type type) return "Items"; } } - return type.Name; + return CleanupName(type.Name); } #endregion @@ -106,13 +114,13 @@ public XElement Serialize(object value, String nodeName = null) { if (value == null) throw new ArgumentNullException("value"); var type = value.GetType(); - if (nodeName == null) nodeName = NodeNameFromType(type); // Check is an object if (value is String || (!type.IsClass && type.IsValueType)) throw new ArgumentException(String.Format("The value of type '{0}' is not an object.", type.FullName), "value"); // Prepare element and serialize it + if (nodeName == null) nodeName = NodeNameFromType(type); XElement result = new XElement(nodeName); InternalSerialize(value, result, type); return result; @@ -189,7 +197,6 @@ protected virtual void InternalSerialize(object value, XElement target, Type typ // For each property foreach (var property in typeValue.GetProperties()) { - if (!property.CanWrite) continue; var v = property.GetValue(value, null); if (v == null) continue; var x = new XElement(property.Name); @@ -213,6 +220,14 @@ public void Populate(XElement node, object target) InternalPopulate(node, target); } + /// + /// Populate object valeurs from XML text + /// + public void Populate(String xml, object target) + { + Populate(XDocument.Parse(xml).Root, target); + } + /// /// Internal object values population from XML element /// @@ -358,6 +373,14 @@ public T Deserialize(XElement node) return (T)Deserialize(node, typeof(T)); } + /// + /// Deserialize to a typed value + /// + public T Deserialize(String xml) + { + return Deserialize(XDocument.Parse(xml).Root); + } + /// /// Deserialize to a typed value /// @@ -368,6 +391,14 @@ public object Deserialize(XElement node, Type type) return InternalDeserialize(node, type); } + /// + /// Deserialize to a typed value + /// + public object Deserialize(String xml, Type type) + { + return Deserialize(XDocument.Parse(xml).Root, type); + } + /// /// Deserialize to an untyped value /// @@ -377,6 +408,14 @@ public object Deserialize(XElement node) return InternalDeserialize(node, typeof(Object)); } + /// + /// Deserialize to an untyped value + /// + public object Deserialize(String xml) + { + return Deserialize(XDocument.Parse(xml).Root); + } + /// /// Internal XML node deserialization to an object /// @@ -450,7 +489,7 @@ object InternalDeserializeObject(XElement node) case "float": case "double": case "number": - return InternalDeserialize(node, typeof(Double)); + return InternalDeserialize(node, typeof(Decimal)); case "date": case "datetime": return InternalDeserialize(node, typeof(DateTime)); @@ -486,20 +525,50 @@ object InternalDeserializeValue(String value) if (String.Equals("false", value, StringComparison.OrdinalIgnoreCase)) return false; Int64 tint; if (Int64.TryParse(value, NumberStyles.Any, Culture, out tint)) return tint; - Double tdbl; - if (Double.TryParse(value, NumberStyles.Any, Culture, out tdbl)) return tdbl; + Decimal tdbl; + if (Decimal.TryParse(value, NumberStyles.Any, Culture, out tdbl)) return tdbl; DateTime tdt; if (DateTime.TryParse(value, Culture, DateTimeStyles.AssumeLocal, out tdt)) return tdt; return value; } + Int64? TryParseInt(String value) + { + Int64 result; + if (String.IsNullOrWhiteSpace(value)) return null; + if (Int64.TryParse(value, out result)) + return result; + return null; + } + + UInt64? TryParseUInt(String value) + { + UInt64 result; + if (String.IsNullOrWhiteSpace(value)) return null; + if (UInt64.TryParse(value, out result)) + return result; + return null; + } + + Decimal? TryParseFloat(String value) + { + Decimal result; + if (String.IsNullOrWhiteSpace(value)) return null; + if (Decimal.TryParse(value, NumberStyles.Any, Culture, out result)) + return result; + return null; + } + /// /// Try to convert a string value to a value type. /// bool TryToConvertValue(Type fromType, String fromValue, out object toValue) { + Int64? iTmp; + UInt64? uiTmp; + Decimal? fTmp; + DateTime dtTmp; toValue = null; - if (fromType == typeof(String)) { toValue = fromValue; @@ -524,141 +593,140 @@ bool TryToConvertValue(Type fromType, String fromValue, out object toValue) #region Int32 else if (fromType == typeof(Int32)) { - toValue = Convert.ToInt32(Int64.Parse(fromValue)); + toValue = Convert.ToInt32(TryParseInt(fromValue) ?? 0); } else if (fromType == typeof(Int32?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (Int32?)null; + iTmp = TryParseInt(fromValue); + if (iTmp.HasValue) + toValue = (Int32?)Convert.ToInt16(iTmp.Value); else - toValue = (Int32?)Convert.ToInt32(Int64.Parse(fromValue)); + toValue = (Int32?)null; } #endregion #region Int16 else if (fromType == typeof(Int16)) { - toValue = Convert.ToInt16(Int64.Parse(fromValue)); + toValue = Convert.ToInt16(TryParseInt(fromValue) ?? 0); } else if (fromType == typeof(Int16?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (Int16?)null; + iTmp = TryParseInt(fromValue); + if (iTmp.HasValue) + toValue = (Int16?)Convert.ToInt16(iTmp.Value); else - toValue = (Int16?)Convert.ToInt16(Int64.Parse(fromValue)); + toValue = (Int16?)null; } #endregion #region Int64 else if (fromType == typeof(Int64)) { - toValue = Int64.Parse(fromValue); + toValue = TryParseInt(fromValue) ?? 0; } else if (fromType == typeof(Int64?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (Int64?)null; - else - toValue = (Int64?)Int64.Parse(fromValue); + toValue = TryParseInt(fromValue); } #endregion #region UInt32 else if (fromType == typeof(UInt32)) { - toValue = Convert.ToUInt32(UInt64.Parse(fromValue)); - return true; + toValue = Convert.ToUInt32(TryParseUInt(fromValue) ?? 0); } else if (fromType == typeof(UInt32?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (UInt32?)null; + uiTmp = TryParseUInt(fromValue); + if (uiTmp.HasValue) + toValue = (UInt32?)Convert.ToUInt32(uiTmp.Value); else - toValue = (UInt32?)Convert.ToUInt32(UInt64.Parse(fromValue)); + toValue = (UInt32?)null; } #endregion #region UInt16 else if (fromType == typeof(UInt16)) { - toValue = Convert.ToUInt16(UInt64.Parse(fromValue)); + toValue = Convert.ToUInt16(TryParseUInt(fromValue) ?? 0); } else if (fromType == typeof(UInt16?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (UInt16?)null; + uiTmp = TryParseUInt(fromValue); + if (uiTmp.HasValue) + toValue = (UInt16?)Convert.ToUInt16(uiTmp.Value); else - toValue = (UInt16?)Convert.ToUInt16(UInt64.Parse(fromValue)); + toValue = (UInt16?)null; } #endregion #region UInt64 else if (fromType == typeof(UInt64)) { - toValue = UInt64.Parse(fromValue); + toValue = TryParseUInt(fromValue) ?? 0; } else if (fromType == typeof(UInt64?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (UInt64?)null; - else - toValue = (UInt64?)UInt64.Parse(fromValue); + toValue = TryParseUInt(fromValue); } #endregion #region double else if (fromType == typeof(Double)) { - toValue = double.Parse(fromValue, Culture); + toValue = Convert.ToDouble(TryParseFloat(fromValue) ?? 0); } else if (fromType == typeof(Double?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (double?)null; + fTmp = TryParseFloat(fromValue); + if (fTmp.HasValue) + toValue = (Double?)Convert.ToDouble(fTmp.Value); else - toValue = (double?)double.Parse(fromValue, Culture); + toValue = (Double?)null; } #endregion #region Single else if (fromType == typeof(Single)) { - toValue = Single.Parse(fromValue, Culture); + toValue = Convert.ToSingle(TryParseFloat(fromValue) ?? 0); } else if (fromType == typeof(Single?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (Single?)null; + fTmp = TryParseFloat(fromValue); + if (fTmp.HasValue) + toValue = (Single?)Convert.ToSingle(fTmp.Value); else - toValue = (Single?)Single.Parse(fromValue, Culture); + toValue = (Single?)null; } #endregion #region Decimal else if (fromType == typeof(Decimal)) { - toValue = Decimal.Parse(fromValue, Culture); + toValue = TryParseFloat(fromValue) ?? 0; } else if (fromType == typeof(Decimal?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (Decimal?)null; - else - toValue = (Decimal?)Decimal.Parse(fromValue, Culture); + toValue = TryParseFloat(fromValue); } #endregion #region DateTime else if (fromType == typeof(DateTime)) { - toValue = DateTime.Parse(fromValue, Culture, DateTimeStyles.AssumeLocal); + if (DateTime.TryParse(fromValue, Culture, DateTimeStyles.AssumeLocal, out dtTmp)) + toValue = dtTmp; + else + toValue = DateTime.MinValue; } else if (fromType == typeof(DateTime?)) { - if (String.IsNullOrWhiteSpace(fromValue)) - toValue = (DateTime?)null; + if (DateTime.TryParse(fromValue, Culture, DateTimeStyles.AssumeLocal, out dtTmp)) + toValue = (DateTime?)dtTmp; else - toValue = (DateTime?)DateTime.Parse(fromValue, Culture, DateTimeStyles.AssumeLocal); + toValue = (DateTime?)null; } #endregion else diff --git a/src/deuxsucres.XSerializer/deuxsucres.XSerializer.csproj b/src/deuxsucres.XSerializer/deuxsucres.XSerializer.csproj index 08f9753..10ff876 100644 --- a/src/deuxsucres.XSerializer/deuxsucres.XSerializer.csproj +++ b/src/deuxsucres.XSerializer/deuxsucres.XSerializer.csproj @@ -35,6 +35,7 @@ + diff --git a/src/deuxsucres.XSerializer/deuxsucres.XSerializer.nuspec b/src/deuxsucres.XSerializer/deuxsucres.XSerializer.nuspec index 7509369..4d57b7c 100644 --- a/src/deuxsucres.XSerializer/deuxsucres.XSerializer.nuspec +++ b/src/deuxsucres.XSerializer/deuxsucres.XSerializer.nuspec @@ -2,13 +2,13 @@ deuxsucres.XSerializer - 1.0.0 + 1.0.1 deuxsucres deuxsucres https://github.com/deuxsucres/XSerializer/blob/master/LICENSE https://github.com/deuxsucres/XSerializer A small library to serialize and deserialize objects and values from XML - First version of a small XML serializer library. + Correct some behaviors. Copyright deuxsucres 2014 XSerializer xml seralizer