From 2a655ddfd046cf4018c501be497fa85323b1eb94 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Tue, 30 May 2017 22:26:51 -0500 Subject: [PATCH 1/2] add support for deserializing datetimeoffset format --- src/JsonApiDotNetCore/Internal/TypeHelper.cs | 30 ++++++++++++++------ test/UnitTests/Internal/TypeHelper_Tests.cs | 23 +++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 test/UnitTests/Internal/TypeHelper_Tests.cs diff --git a/src/JsonApiDotNetCore/Internal/TypeHelper.cs b/src/JsonApiDotNetCore/Internal/TypeHelper.cs index a0bb133e01..6ceb3b06ac 100644 --- a/src/JsonApiDotNetCore/Internal/TypeHelper.cs +++ b/src/JsonApiDotNetCore/Internal/TypeHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; namespace JsonApiDotNetCore.Internal { @@ -6,17 +7,30 @@ public static class TypeHelper { public static object ConvertType(object value, Type type) { - if(value == null) - return null; + try + { + if (value == null) + return null; + + type = Nullable.GetUnderlyingType(type) ?? type; + + var stringValue = value.ToString(); - type = Nullable.GetUnderlyingType(type) ?? type; + if (type == typeof(Guid)) + return Guid.Parse(stringValue); - var stringValue = value.ToString(); - - if(type == typeof(Guid)) - return Guid.Parse(stringValue); + if (type == typeof(DateTimeOffset)) + return DateTimeOffset.Parse(stringValue); - return Convert.ChangeType(stringValue, type); + return Convert.ChangeType(stringValue, type); + } + catch (Exception) + { + if (type.GetTypeInfo().IsValueType) + return Activator.CreateInstance(type); + + return null; + } } } } diff --git a/test/UnitTests/Internal/TypeHelper_Tests.cs b/test/UnitTests/Internal/TypeHelper_Tests.cs new file mode 100644 index 0000000000..912e516af9 --- /dev/null +++ b/test/UnitTests/Internal/TypeHelper_Tests.cs @@ -0,0 +1,23 @@ +using System; +using JsonApiDotNetCore.Internal; +using Xunit; + +namespace UnitTests.Internal +{ + public class TypeHelper_Tests + { + [Fact] + public void Can_Convert_DateTimeOffsets() + { + // arrange + var dto = DateTimeOffset.Now; + var formattedString = dto.ToString("O"); + + // act + var result = TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset)); + + // assert + Assert.Equal(dto, result); + } + } +} From 2401baf89e67ef0566a56daf988c027d0e10dfb5 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Tue, 30 May 2017 22:29:29 -0500 Subject: [PATCH 2/2] chore(csproj): bump package version --- src/JsonApiDotNetCore/JsonApiDotNetCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj index 1dbf2ce662..699a20076b 100755 --- a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj +++ b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj @@ -1,6 +1,6 @@  - 2.0.7 + 2.0.8 netstandard1.6 JsonApiDotNetCore JsonApiDotNetCore