Skip to content

Commit

Permalink
Merge pull request #6 from deuxsucres/develop
Browse files Browse the repository at this point in the history
Correct some behaviors.
Publish to version 1.0.1.
  • Loading branch information
ygrenier committed Dec 6, 2014
2 parents 1f8f0c5 + a5e9c37 commit 7754b99
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
316 changes: 240 additions & 76 deletions src/deuxsucres.XSerializer.Tests/XDocSerializerTests.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/deuxsucres.XSerializer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
176 changes: 122 additions & 54 deletions src/deuxsucres.XSerializer/XDocSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ protected virtual String SingularizeName(String name)
return name;
}

/// <summary>
/// Clean the name to be a valid XML tag name
/// </summary>
protected virtual String CleanupName(String name)
{
return System.Text.RegularExpressions.Regex.Replace(name, @"[^\w-]", String.Empty);
}

/// <summary>
/// Pluralize a name
/// </summary>
Expand All @@ -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))
{
Expand All @@ -84,15 +92,15 @@ 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);
}
}
return "Items";
}
}
return type.Name;
return CleanupName(type.Name);
}

#endregion
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -213,6 +220,14 @@ public void Populate(XElement node, object target)
InternalPopulate(node, target);
}

/// <summary>
/// Populate object valeurs from XML text
/// </summary>
public void Populate(String xml, object target)
{
Populate(XDocument.Parse(xml).Root, target);
}

/// <summary>
/// Internal object values population from XML element
/// </summary>
Expand Down Expand Up @@ -358,6 +373,14 @@ public T Deserialize<T>(XElement node)
return (T)Deserialize(node, typeof(T));
}

/// <summary>
/// Deserialize to a typed value
/// </summary>
public T Deserialize<T>(String xml)
{
return Deserialize<T>(XDocument.Parse(xml).Root);
}

/// <summary>
/// Deserialize to a typed value
/// </summary>
Expand All @@ -368,6 +391,14 @@ public object Deserialize(XElement node, Type type)
return InternalDeserialize(node, type);
}

/// <summary>
/// Deserialize to a typed value
/// </summary>
public object Deserialize(String xml, Type type)
{
return Deserialize(XDocument.Parse(xml).Root, type);
}

/// <summary>
/// Deserialize to an untyped value
/// </summary>
Expand All @@ -377,6 +408,14 @@ public object Deserialize(XElement node)
return InternalDeserialize(node, typeof(Object));
}

/// <summary>
/// Deserialize to an untyped value
/// </summary>
public object Deserialize(String xml)
{
return Deserialize(XDocument.Parse(xml).Root);
}

/// <summary>
/// Internal XML node deserialization to an object
/// </summary>
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Try to convert a string value to a value type.
/// </summary>
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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/deuxsucres.XSerializer/deuxsucres.XSerializer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<None Include="deuxsucres.XSerializer.nuspec" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Loading

0 comments on commit 7754b99

Please sign in to comment.