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
176 changes: 95 additions & 81 deletions RestSharp.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ namespace RestSharp.Tests
{
public class JsonTests
{
private const string AlternativeCulture = "pt-PT";
private const string AlternativeCulture = "pt-PT";

private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";

[Fact]
public void Can_Deserialize_4sq_Json_With_Root_Element_Specified()
Expand Down Expand Up @@ -190,8 +190,8 @@ public void Can_Deserialize_Elements_to_Nullable_Values()
Assert.Equal(123, output.Id);
Assert.NotNull(output.StartDate);
Assert.Equal(
new DateTime(2010, 2, 21, 9, 35, 00, DateTimeKind.Utc).ToString("u"),
output.StartDate.Value.ToString("u"));
new DateTime(2010, 2, 21, 9, 35, 00, DateTimeKind.Utc),
output.StartDate.Value);
Assert.Equal(new Guid(GuidString), output.UniqueId);
}

Expand Down Expand Up @@ -315,14 +315,14 @@ public void Can_Deserialize_With_Default_Root()
Assert.Equal("Foe 2", p.Foes["dict2"].Nickname);
}

[Fact]
public void Can_Deserialize_With_Default_Root_Alternative_Culture()
{
using (new CultureChange(AlternativeCulture))
{
Can_Deserialize_With_Default_Root();
}
}
[Fact]
public void Can_Deserialize_With_Default_Root_Alternative_Culture()
{
using (new CultureChange(AlternativeCulture))
{
Can_Deserialize_With_Default_Root();
}
}

[Fact]
public void Can_Deserialize_Names_With_Underscores_With_Default_Root()
Expand Down Expand Up @@ -353,14 +353,14 @@ public void Can_Deserialize_Names_With_Underscores_With_Default_Root()
Assert.Equal("Foe 2", p.Foes["dict2"].Nickname);
}

[Fact]
public void Can_Deserialize_Names_With_Underscores_With_Default_Root_Alternative_Culture()
{
using (new CultureChange(AlternativeCulture))
{
Can_Deserialize_Names_With_Underscores_With_Default_Root();
}
}
[Fact]
public void Can_Deserialize_Names_With_Underscores_With_Default_Root_Alternative_Culture()
{
using (new CultureChange(AlternativeCulture))
{
Can_Deserialize_Names_With_Underscores_With_Default_Root();
}
}

[Fact]
public void Can_Deserialize_Names_With_Dashes_With_Default_Root()
Expand Down Expand Up @@ -391,14 +391,14 @@ public void Can_Deserialize_Names_With_Dashes_With_Default_Root()
Assert.Equal("Foe 2", p.Foes["dict2"].Nickname);
}

[Fact]
public void Can_Deserialize_Names_With_Dashes_With_Default_Root_Alternative_Culture()
{
using (new CultureChange(AlternativeCulture))
{
Can_Deserialize_Names_With_Dashes_With_Default_Root();
}
}
[Fact]
public void Can_Deserialize_Names_With_Dashes_With_Default_Root_Alternative_Culture()
{
using (new CultureChange(AlternativeCulture))
{
Can_Deserialize_Names_With_Dashes_With_Default_Root();
}
}

[Fact]
public void Ignore_Protected_Property_That_Exists_In_Data()
Expand All @@ -423,22 +423,19 @@ public void Ignore_ReadOnly_Property_That_Exists_In_Data()
}

[Fact]
public void Can_Deserialize_TimeSpan()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "timespans.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<TimeSpanTestStructure>(response);
public void Can_Deserialize_TimeSpan()
{
var payload = GetPayLoad<TimeSpanTestStructure>("timespans.txt");

Assert.Equal(new TimeSpan(468006), payload.Tick);
Assert.Equal(new TimeSpan(0, 0, 0, 0, 125), payload.Millisecond);
Assert.Equal(new TimeSpan(0, 0, 8), payload.Second);
Assert.Equal(new TimeSpan(0, 55, 2), payload.Minute);
Assert.Equal(new TimeSpan(21, 30, 7), payload.Hour);
Assert.Null(payload.NullableWithoutValue);
Assert.NotNull(payload.NullableWithValue);
Assert.Equal(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
}
Assert.Equal(new TimeSpan(468006), payload.Tick);
Assert.Equal(new TimeSpan(0, 0, 0, 0, 125), payload.Millisecond);
Assert.Equal(new TimeSpan(0, 0, 8), payload.Second);
Assert.Equal(new TimeSpan(0, 55, 2), payload.Minute);
Assert.Equal(new TimeSpan(21, 30, 7), payload.Hour);
Assert.Null(payload.NullableWithoutValue);
Assert.NotNull(payload.NullableWithValue);
Assert.Equal(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
}

[Fact]
public void Can_Deserialize_Iso_Json_Dates()
Expand All @@ -465,88 +462,97 @@ public void Can_Deserialize_Unix_Json_Dates()
[Fact]
public void Can_Deserialize_JsonNet_Dates()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "person.json.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var person = d.Deserialize<PersonForJson>(response);
var person = GetPayLoad<PersonForJson>("person.json.txt");

Assert.Equal(
new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
person.StartDate.ToString("u"));
new DateTime(2011, 6, 30, 8, 15, 46, 929, DateTimeKind.Utc),
person.StartDate);
}

[Fact]
public void Can_Deserialize_DateTime()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "datetimes.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<DateTimeTestStructure>(response);
var payload = GetPayLoad<DateTimeTestStructure>("datetimes.txt");

Assert.Equal(
new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
payload.DateTime.ToString("u"));
new DateTime(2011, 6, 30, 8, 15, 46, 929, DateTimeKind.Utc),
payload.DateTime);
}

[Fact]
public void Can_Deserialize_Nullable_DateTime_With_Value()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "datetimes.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<DateTimeTestStructure>(response);
var payload = GetPayLoad<DateTimeTestStructure>("datetimes.txt");

Assert.NotNull(payload.NullableDateTimeWithValue);
Assert.Equal(
new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
payload.NullableDateTimeWithValue.Value.ToString("u"));
new DateTime(2011, 6, 30, 8, 15, 46, 929, DateTimeKind.Utc),
payload.NullableDateTimeWithValue.Value);
}

[Fact]
public void Can_Deserialize_Nullable_DateTime_With_Null()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "datetimes.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<DateTimeTestStructure>(response);
var payload = GetPayLoad<DateTimeTestStructure>("datetimes.txt");

Assert.Null(payload.NullableDateTimeWithNull);
}

[Fact]
public void Can_Deserialize_DateTimeOffset()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "datetimes.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<DateTimeTestStructure>(response);
var payload = GetPayLoad<DateTimeTestStructure>("datetimes.txt");

Assert.Equal(
new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
payload.DateTimeOffset.ToString("u"));
new DateTime(2011, 6, 30, 8, 15, 46, 929, DateTimeKind.Utc),
payload.DateTimeOffset);
}

[Fact]
public void Can_Deserialize_Iso8601DateTimeLocal()
{
var payload = GetPayLoad<Iso8601DateTimeTestStructure>("iso8601datetimes.txt");

Assert.Equal(
new DateTime(2012, 7, 19, 10, 23, 25, DateTimeKind.Utc),
payload.DateTimeLocal);
}

[Fact]
public void Can_Deserialize_Iso8601DateTimeZulu()
{
var payload = GetPayLoad<Iso8601DateTimeTestStructure>("iso8601datetimes.txt");

Assert.Equal(
new DateTime(2012, 7, 19, 10, 23, 25, 544, DateTimeKind.Utc),
payload.DateTimeUtc.ToUniversalTime());
}

[Fact]
public void Can_Deserialize_Iso8601DateTimeWithOffset()
{
var payload = GetPayLoad<Iso8601DateTimeTestStructure>("iso8601datetimes.txt");

Assert.Equal(
new DateTime(2012, 7, 19, 10, 23, 25, 544, DateTimeKind.Utc),
payload.DateTimeWithOffset.ToUniversalTime());
}

[Fact]
public void Can_Deserialize_Nullable_DateTimeOffset_With_Value()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "datetimes.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<DateTimeTestStructure>(response);
var payload = GetPayLoad<DateTimeTestStructure>("datetimes.txt");

Assert.NotNull(payload.NullableDateTimeOffsetWithValue);
Assert.Equal(
new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
payload.NullableDateTimeOffsetWithValue.Value.ToString("u"));
new DateTime(2011, 6, 30, 8, 15, 46, 929, DateTimeKind.Utc),
payload.NullableDateTimeOffsetWithValue);
}

[Fact]
public void Can_Deserialize_Nullable_DateTimeOffset_With_Null()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "datetimes.txt"));
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var payload = d.Deserialize<DateTimeTestStructure>(response);
var payload = GetPayLoad<DateTimeTestStructure>("datetimes.txt");

Assert.Null(payload.NullableDateTimeOffsetWithNull);
}
Expand Down Expand Up @@ -759,5 +765,13 @@ public string CreateDynamicJsonStringDictionary ()
doc["ThingBlue"] = new JObject (new JProperty("Name", "ThingBlue"), new JProperty ("Color", "Blue"));
return doc.ToString ();
}

private T GetPayLoad<T>(string fileName)
{
var doc = File.ReadAllText(Path.Combine("SampleData", fileName));
var response = new RestResponse { Content = doc };
var d = new JsonDeserializer();
return d.Deserialize<T>(response);
}
}
}
3 changes: 3 additions & 0 deletions RestSharp.Tests/RestSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
<Content Include="SampleData\boolean_from_number.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\iso8601datetimes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\datetimes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
7 changes: 7 additions & 0 deletions RestSharp.Tests/SampleClasses/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ public class DateTimeTestStructure
public DateTimeOffset? NullableDateTimeOffsetWithValue { get; set; }
}

public class Iso8601DateTimeTestStructure
{
public DateTime DateTimeLocal { get; set; }
public DateTime DateTimeUtc { get; set; }
public DateTime DateTimeWithOffset { get; set; }
}

public class TimeSpanTestStructure
{
public TimeSpan Tick { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions RestSharp.Tests/SampleData/iso8601datetimes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"DateTimeLocal": "2012-07-19T10:23:25",
"DateTimeUtc": "2012-07-19T10:23:25.544Z",
"DateTimeWithOffset": "2012-07-19T03:23:25.544-07:00",
}
5 changes: 5 additions & 0 deletions RestSharp/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ private static DateTime ParseFormattedDate(string input, CultureInfo culture)
return date;
}

if (DateTime.TryParse(input, culture, DateTimeStyles.None, out date))
{
return date;
}

return default(DateTime);
}

Expand Down