Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for DateTime serialized as UnixTime #1735

Merged
merged 7 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Octokit.Tests/SimpleJsonSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void CanDeserializeOrganization()
"\"followers\": 0," +
"\"following\": 0," +
"\"created_at\": \"2009-02-10T17:53:17Z\"," +
"\"updated_at\": \"2014-07-07T00:12:56Z\"" +
"\"updated_at\": 1404691976" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe could you add .Asserts that test CreatedAt and UpdatedAt fields get the values expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"}";

var result = new SimpleJsonSerializer().Deserialize<User>(json);
Expand Down
12 changes: 11 additions & 1 deletion Octokit.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit", "Octokit\Octokit.csproj", "{104E8324-C2B9-43BE-8040-36B736A64D45}"
EndProject
Expand All @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit.Tests.Conventions",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octokit.Tests.Integration", "Octokit.Tests.Integration\Octokit.Tests.Integration.csproj", "{EF19E577-D810-4357-BF95-9029A359CB4E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{64FD6CD6-3714-4A7B-AF5A-B8E7DFEEC807}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -43,4 +45,12 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E58EC0EA-B6E5-43AA-A7D2-AD0A6D4BB0D2} = {64FD6CD6-3714-4A7B-AF5A-B8E7DFEEC807}
{A04124DC-D057-4DB0-8E5F-ECD114FA371A} = {64FD6CD6-3714-4A7B-AF5A-B8E7DFEEC807}
{EF19E577-D810-4357-BF95-9029A359CB4E} = {64FD6CD6-3714-4A7B-AF5A-B8E7DFEEC807}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3ABF5A09-BE91-46DF-93C1-BE3932581DF5}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions Octokit/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
using Octokit.Reflection;
#if !SIMPLE_JSON_NO_LINQ_EXPRESSION
using System.Linq.Expressions;
using Octokit.Helpers;
#endif
#if SIMPLE_JSON_DYNAMIC
using System.Dynamic;
Expand Down Expand Up @@ -1429,6 +1430,14 @@ public virtual object DeserializeObject(object value, Type type)
return value;
if ((valueIsDouble && type != typeof(double)) || (valueIsLong && type != typeof(long)))
{
if (valueIsLong && (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?)))
{
return ((long)value).FromUnixTime();
}
else if (valueIsLong && (type == typeof(DateTime) || type == typeof(DateTime?)))
{
return ((long)value).FromUnixTime().DateTime;
}
obj = type == typeof(int) || type == typeof(long) || type == typeof(double) || type == typeof(float) || type == typeof(bool) || type == typeof(decimal) || type == typeof(byte) || type == typeof(short)
? Convert.ChangeType(value, type, CultureInfo.InvariantCulture)
: value;
Expand Down