Skip to content
Merged
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
8 changes: 8 additions & 0 deletions RestSharp.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,14 @@ public void Can_Deserialize_TimeSpan()
Assert.Null(payload.NullableWithoutValue);
Assert.NotNull(payload.NullableWithValue);
Assert.Equal(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
Assert.Equal(new TimeSpan(0, 0, 10), payload.IsoSecond);
Assert.Equal(new TimeSpan(0, 3, 23), payload.IsoMinute);
Assert.Equal(new TimeSpan(5, 4, 9), payload.IsoHour);
Assert.Equal(new TimeSpan(1, 19, 27, 13), payload.IsoDay);
// 2 months + 4 days = 64 days
Assert.Equal(new TimeSpan(64, 3, 14, 19), payload.IsoMonth);
// 1 year = 365 days
Assert.Equal(new TimeSpan(365, 9, 27, 48), payload.IsoYear);
}

[Fact]
Expand Down
12 changes: 12 additions & 0 deletions RestSharp.Tests/SampleClasses/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public class TimeSpanTestStructure
public TimeSpan? NullableWithoutValue { get; set; }

public TimeSpan? NullableWithValue { get; set; }

public TimeSpan? IsoSecond { get; set; }

public TimeSpan? IsoMinute { get; set; }

public TimeSpan? IsoHour { get; set; }

public TimeSpan? IsoDay { get; set; }

public TimeSpan? IsoMonth { get; set; }

public TimeSpan? IsoYear { get; set; }
}

public class JsonEnumsTestStructure
Expand Down
6 changes: 6 additions & 0 deletions RestSharp.Tests/SampleData/timespans.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
"Hour": "21:30:07.0000000",
"NullableWithoutValue": null,
"NullableWithValue": "21:30:07.0000000",
"IsoSecond": "PT10S",
"IsoMinute": "PT3M23S",
"IsoHour": "PT5H4M9S",
"IsoDay": "P1DT19H27M13S",
"IsoMonth": "P2M4DT3H14M19S",
"IsoYear": "P1YT9H27M48S"
}
11 changes: 10 additions & 1 deletion RestSharp/Deserializers/JsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace RestSharp.Deserializers
{
using System.Xml;

public class JsonDeserializer : IDeserializer
{
public string RootElement { get; set; }
Expand Down Expand Up @@ -270,7 +272,14 @@ private object ConvertValue(Type type, object value)
}
else if (type == typeof(TimeSpan))
{
return TimeSpan.Parse(stringValue);
TimeSpan timeSpan;
if (TimeSpan.TryParse(stringValue, out timeSpan))
{
return timeSpan;
}

// This should handle ISO 8601 durations
return XmlConvert.ToTimeSpan(stringValue);
}
else if (type.IsGenericType)
{
Expand Down