-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Added support for DateTimeOffset #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4fc0ad5
2d411a5
a1f25c7
ed7540a
9789dd6
7de5465
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,8 @@ | |
|
||
using RestSharp.Extensions; | ||
using System.Globalization; | ||
using System.Xml; | ||
using System.Xml; | ||
using System.ComponentModel; | ||
|
||
namespace RestSharp.Deserializers | ||
{ | ||
|
@@ -168,7 +169,34 @@ private void Map(object x, XElement root) | |
} | ||
|
||
prop.SetValue(x, value, null); | ||
} | ||
} | ||
else if (type == typeof(DateTimeOffset)) | ||
{ | ||
var toConvert = value.ToString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, there is a null check at line 129 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This previous block of code seems unrelated to the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in https://github.com/EamonHetherton/RestSharp/commit/7de5465d486773d7c9934e98b16f11ddf3b17e54
Should I be issuing a new pull request for this?
There was a problem hiding this comment.
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.