Skip to content

Commit a7fa2ad

Browse files
committed
Extended JsonDeserializer to add support for TimeSpan incl. UnitTests.
Supported format is: "00:00:00.0000000"
1 parent 86b31f9 commit a7fa2ad

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ public void Ignore_ReadOnly_Property_That_Exists_In_Data()
313313
Assert.Null(p.ReadOnlyProxy);
314314
}
315315

316+
[Fact]
317+
public void Can_Deserialize_TimeSpan()
318+
{
319+
var doc = File.ReadAllText(Path.Combine("SampleData", "timespans.txt"));
320+
var d = new JsonDeserializer();
321+
var response = new RestResponse { Content = doc };
322+
var payload = d.Deserialize<TimeSpanTestStructure>(response);
323+
324+
Assert.Equal(new TimeSpan(468006), payload.Tick);
325+
Assert.Equal(new TimeSpan(0, 0, 0, 0, 125), payload.Millisecond);
326+
Assert.Equal(new TimeSpan(0, 0, 8), payload.Second);
327+
Assert.Equal(new TimeSpan(0, 55, 2), payload.Minute);
328+
Assert.Equal(new TimeSpan(21, 30, 7), payload.Hour);
329+
Assert.Null(payload.NullableWithoutValue);
330+
Assert.NotNull(payload.NullableWithValue);
331+
Assert.Equal(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
332+
}
333+
316334
[Fact]
317335
public void Can_Deserialize_Iso_Json_Dates()
318336
{

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@
150150
<Content Include="SampleData\Lastfm.xml">
151151
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
152152
</Content>
153+
<Content Include="SampleData\timespans.txt">
154+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
155+
</Content>
153156
<Content Include="SampleData\xmllists.xml">
154157
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
155158
</Content>

RestSharp.Tests/SampleClasses/misc.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ public class DateTimeTestStructure
154154
public DateTimeOffset? NullableDateTimeOffsetWithValue { get; set; }
155155
}
156156

157+
public class TimeSpanTestStructure
158+
{
159+
public TimeSpan Tick { get; set; }
160+
public TimeSpan Millisecond { get; set; }
161+
public TimeSpan Second { get; set; }
162+
public TimeSpan Minute { get; set; }
163+
public TimeSpan Hour { get; set; }
164+
public TimeSpan? NullableWithoutValue { get; set; }
165+
public TimeSpan? NullableWithValue { get; set; }
166+
}
167+
157168
public class JsonEnumsTestStructure
158169
{
159170
public Disposition Upper { get; set; }

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ private void Map(object x, JToken json)
161161
string raw = value.AsString();
162162
var guid = string.IsNullOrEmpty(raw) ? Guid.Empty : new Guid(raw);
163163
prop.SetValue(x, guid, null);
164-
}
164+
}
165+
else if (type == typeof(TimeSpan))
166+
{
167+
var timeSpan = TimeSpan.Parse(value.AsString(Culture));
168+
prop.SetValue(x, timeSpan, null);
169+
}
165170
else if (type.IsGenericType)
166171
{
167172
var genericTypeDef = type.GetGenericTypeDefinition();

0 commit comments

Comments
 (0)