Browse files

Merged pull request #143

Merge remote-tracking branch 'remotes/tathamoddie/master' into john_master

Conflicts:
	RestSharp.Tests/RestSharp.Tests.csproj
  • Loading branch information...
2 parents dd3e88e + 4e4b93d commit 61a2fecc2bfe241e66601e2db268df0c4e61deb3 @ayoung ayoung committed Aug 19, 2011
View
96 RestSharp.Tests/JsonTests.cs
@@ -110,7 +110,10 @@ public void Can_Deserialize_Elements_to_Nullable_Values()
Assert.NotNull(output.UniqueId);
Assert.Equal(123, output.Id);
- Assert.Equal(new DateTime(2010, 2, 21, 9, 35, 00), output.StartDate);
+ Assert.NotNull(output.StartDate);
+ Assert.Equal(
+ new DateTime(2010, 2, 21, 9, 35, 00, DateTimeKind.Utc).ToString("u"),
+ output.StartDate.Value.ToString("u"));
Assert.Equal(new Guid(GuidString), output.UniqueId);
}
@@ -308,6 +311,95 @@ public void Can_Deserialize_JScript_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);
+
+ Assert.Equal(
+ new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
+ person.StartDate.ToString("u"));
+ }
+
+ [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);
+
+ Assert.Equal(
+ new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
+ payload.DateTime.ToString("u"));
+ }
+
+ [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);
+
+ Assert.NotNull(payload.NullableDateTimeWithValue);
+ Assert.Equal(
+ new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
+ payload.NullableDateTimeWithValue.Value.ToString("u"));
+ }
+
+ [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);
+
+ 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);
+
+ Assert.Equal(
+ new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
+ payload.DateTimeOffset.ToString("u"));
+ }
+
+ [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);
+
+ Assert.NotNull(payload.NullableDateTimeOffsetWithValue);
+ Assert.Equal(
+ new DateTime(2011, 6, 30, 8, 15, 46, DateTimeKind.Utc).ToString("u"),
+ payload.NullableDateTimeOffsetWithValue.Value.ToString("u"));
+ }
+
+ [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);
+
+ Assert.Null(payload.NullableDateTimeOffsetWithNull);
+ }
+
+ [Fact]
public void Can_Deserialize_To_Dictionary_String_String()
{
var doc = CreateJsonStringDictionary();
@@ -475,7 +567,7 @@ private string CreateJsonWithoutEmptyValues()
{
var doc = new JObject();
doc["Id"] = 123;
- doc["StartDate"] = new DateTime(2010, 2, 21, 9, 35, 00);
+ doc["StartDate"] = new DateTime(2010, 2, 21, 9, 35, 00, DateTimeKind.Utc);
doc["UniqueId"] = new Guid(GuidString).ToString();
return doc.ToString();
View
6 RestSharp.Tests/RestSharp.Tests.csproj
@@ -107,6 +107,12 @@
<Content Include="SampleData\boolean_from_number.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
+ <Content Include="SampleData\datetimes.txt">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ <Content Include="SampleData\person.json.txt">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
<Content Include="SampleData\directlists.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
View
10 RestSharp.Tests/SampleClasses/misc.cs
@@ -133,4 +133,14 @@ public class GenericWithList<T>
{
public List<T> Items { get; set; }
}
+
+ public class DateTimeTestStructure
+ {
+ public DateTime DateTime { get; set; }
+ public DateTime? NullableDateTimeWithNull { get; set; }
+ public DateTime? NullableDateTimeWithValue { get; set; }
+ public DateTimeOffset DateTimeOffset { get; set; }
+ public DateTimeOffset? NullableDateTimeOffsetWithNull { get; set; }
+ public DateTimeOffset? NullableDateTimeOffsetWithValue { get; set; }
+ }
}
View
6 RestSharp.Tests/SampleData/datetimes.txt
@@ -0,0 +1,6 @@
+???{
+ "DateTime": "/Date(1309421746929+0000)/",
+ "NullableDateTimeWithValue": "/Date(1309421746929+0000)/",
+ "DateTimeOffset": "/Date(1309421746929+0000)/",
+ "NullableDateTimeOffsetWithValue": "/Date(1309421746929+0000)/"
+}
View
3 RestSharp.Tests/SampleData/person.json.txt
@@ -0,0 +1,3 @@
+???{
+ 'StartDate': '/Date(1309421746929+0000)/'
+}
View
11 RestSharp/Deserializers/JsonDeserializer.cs
@@ -171,21 +171,28 @@ private void Map(object x, JToken json)
string raw = value.AsString();
prop.SetValue(x, raw, null);
}
- else if (type == typeof(DateTime))
+ else if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
{
DateTime dt;
if (DateFormat.HasValue())
{
var clean = value.AsString();
dt = DateTime.ParseExact(clean, DateFormat, Culture);
}
+ else if (value.Type == JTokenType.Date)
+ {
+ dt = value.Value<DateTime>().ToUniversalTime();
+ }
else
{
// try parsing instead
dt = value.AsString().ParseJsonDate(Culture);
}
- prop.SetValue(x, dt, null);
+ if (type == typeof(DateTime))
+ prop.SetValue(x, dt, null);
+ else if (type == typeof(DateTimeOffset))
+ prop.SetValue(x, (DateTimeOffset)dt, null);
}
else if (type == typeof(Decimal))
{

0 comments on commit 61a2fec

Please sign in to comment.