Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion RestSharp.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,42 @@ public void Can_Deserialize_Mixture_Of_Empty_Elements_With_Attributes_And_Popula
Assert.Null(output.Id);
Assert.Null(output.StartDate);
Assert.Equal(new Guid(GuidString), output.UniqueId);
}
}

[Fact]
public void Can_Deserialize_DateTimeOffset()
{
var culture = CultureInfo.InvariantCulture;
var doc = new XDocument(culture);

DateTimeOffset DateTimeOffset = new DateTimeOffset(2013, 02, 08, 9, 18, 22, TimeSpan.FromHours(10));
DateTimeOffset? NullableDateTimeOffsetWithValue = new DateTimeOffset(2013, 02, 08, 9, 18, 23, TimeSpan.FromHours(10));

var root = new XElement("Dates");
root.Add(new XElement("DateTimeOffset", DateTimeOffset));
root.Add(new XElement("NullableDateTimeOffsetWithNull", string.Empty));
root.Add(new XElement("NullableDateTimeOffsetWithValue", NullableDateTimeOffsetWithValue));

doc.Add(root);

var xml = new XmlDeserializer
{
Culture = culture,
};

var response = new RestResponse { Content = doc.ToString() };

var d = new XmlDeserializer()
{
Culture = culture,
};
var payload = d.Deserialize<DateTimeTestStructure>(response);
Assert.Equal(DateTimeOffset, payload.DateTimeOffset);
Assert.Null(payload.NullableDateTimeOffsetWithNull);

Assert.True(payload.NullableDateTimeOffsetWithValue.HasValue);
Assert.Equal(NullableDateTimeOffsetWithValue, payload.NullableDateTimeOffsetWithValue);
}

private static string CreateUnderscoresXml()
{
Expand Down Expand Up @@ -934,5 +969,6 @@ private static string CreateXmlWithAttributesAndNullValuesAndPopulatedValues()

return doc.ToString();
}

}
}
2 changes: 1 addition & 1 deletion RestSharp/Deserializers/XmlAttributeDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public T Deserialize<T>(IRestResponse response)
RemoveNamespace(doc);
}

var x = Activator.CreateInstance<T>();
var x = Activator.CreateInstance<T>();
var objType = x.GetType();

if (objType.IsSubclassOfRawGeneric(typeof(List<>)))
Expand Down
75 changes: 62 additions & 13 deletions RestSharp/Deserializers/XmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

using RestSharp.Extensions;
using System.Globalization;
using System.Xml;
using System.Xml;
using System.ComponentModel;

namespace RestSharp.Deserializers
{
Expand Down Expand Up @@ -168,7 +169,34 @@ private void Map(object x, XElement root)
}

prop.SetValue(x, value, null);
}
}
else if (type == typeof(DateTimeOffset))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. If you push your changes to the same branch, the pull request is updated automatically.

{
var toConvert = value.ToString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't value be null here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there is a null check at line 129

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool. Thanks!

if (!string.IsNullOrEmpty(toConvert))
{
DateTimeOffset deserialisedValue;
try
{
deserialisedValue = XmlConvert.ToDateTimeOffset(toConvert);
prop.SetValue(x, deserialisedValue, null);
}
catch (Exception)
{
object result;
if (TryGetFromString(toConvert, out result, type))
{
prop.SetValue(x, result, null);
}
else
{
//fallback to parse
deserialisedValue = DateTimeOffset.Parse(toConvert);
prop.SetValue(x, deserialisedValue, null);
}
}
}
}
else if (type == typeof(Decimal))
{
value = Decimal.Parse(value.ToString(), Culture);
Expand Down Expand Up @@ -209,20 +237,41 @@ private void Map(object x, XElement root)
prop.SetValue(x, list, null);
}
else
{
// nested property classes
if (root != null)
{
var element = GetElementByName(root, name);
if (element != null)
{
var item = CreateAndMap(type, element);
prop.SetValue(x, item, null);
}
{
//fallback to type converters if possible
object result;
if (TryGetFromString(value.ToString(), out result, type))
{
prop.SetValue(x, result, null);
}
else
{
// nested property classes
if (root != null)
{
var element = GetElementByName(root, name);
if (element != null)
{
var item = CreateAndMap(type, element);
prop.SetValue(x, item, null);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This previous block of code seems unrelated to the DateTimeOffset change. Should this be in another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, but I added it in the middle of two commits related to the DateTimeOffset functionality and I'm not good enough with git shell to work out how to break it out (certainly can't work it out in git for windows). The actual commit from my repo with this change in it is https://github.com/EamonHetherton/RestSharp/commit/a1f25c77d40aca462166324b3f76941a7298019f

}
}
}
}
}

private static bool TryGetFromString(string inputString, out object result, Type type)
{
var converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(typeof(string)))
{
result = (converter.ConvertFromInvariantString(inputString));
return true;
}
result = null;
return false;
}

private void PopulateListFromElements(Type t, IEnumerable<XElement> elements, IList list)
{
Expand Down