Skip to content

Commit 0ef75b0

Browse files
EamonHethertonhaacked
authored andcommitted
added fallback option for datetimeoffset objects that can't be deserialised by XmlConvert
1 parent 0db7b9c commit 0ef75b0

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

RestSharp/Deserializers/XmlDeserializer.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,26 @@ private void Map(object x, XElement root)
175175
var toConvert = value.ToString();
176176
if (!string.IsNullOrEmpty(toConvert))
177177
{
178-
prop.SetValue(x, XmlConvert.ToDateTimeOffset(toConvert), null);
178+
DateTimeOffset deserialisedValue;
179+
try
180+
{
181+
deserialisedValue = XmlConvert.ToDateTimeOffset(toConvert);
182+
prop.SetValue(x, deserialisedValue, null);
183+
}
184+
catch (Exception)
185+
{
186+
object result;
187+
if (TryGetFromString(toConvert, out result, type))
188+
{
189+
prop.SetValue(x, result, null);
190+
}
191+
else
192+
{
193+
//fallback to parse
194+
deserialisedValue = DateTimeOffset.Parse(toConvert);
195+
prop.SetValue(x, deserialisedValue, null);
196+
}
197+
}
179198
}
180199
}
181200
else if (type == typeof(Decimal))
@@ -223,7 +242,7 @@ private void Map(object x, XElement root)
223242
object result;
224243
if (TryGetFromString(value.ToString(), out result, type))
225244
{
226-
prop.SetValue(x, value, null);
245+
prop.SetValue(x, result, null);
227246
}
228247
else
229248
{

0 commit comments

Comments
 (0)