Skip to content

Commit

Permalink
Merge pull request #132 from jasolko/deserialize-enums
Browse files Browse the repository at this point in the history
fix(deserialization): deserialize enums
  • Loading branch information
jaredcnance authored Jun 14, 2017
2 parents 111773f + d45fe14 commit 94b6a62
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Internal/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static object ConvertType(object value, Type type)
if (type == typeof(DateTimeOffset))
return DateTimeOffset.Parse(stringValue);

if (type.GetTypeInfo().IsEnum)
return Enum.Parse(type, stringValue);

return Convert.ChangeType(stringValue, type);
}
catch (Exception e)
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.0.8</VersionPrefix>
<VersionPrefix>2.0.9</VersionPrefix>
<TargetFrameworks>netstandard1.6</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand All @@ -21,4 +21,4 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="System.ValueTuple" Version="4.3.1" />
</ItemGroup>
</Project>
</Project>
18 changes: 18 additions & 0 deletions test/UnitTests/Internal/TypeHelper_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,23 @@ public void Bad_DateTimeOffset_String_Throws()
// assert
Assert.Throws<FormatException>(() => TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset)));
}

[Fact]
public void Can_Convert_Enums()
{
// arrange
var formattedString = "1";

// act
var result = TypeHelper.ConvertType(formattedString, typeof(TestEnum));

// assert
Assert.Equal(TestEnum.Test, result);
}

public enum TestEnum
{
Test = 1
}
}
}

0 comments on commit 94b6a62

Please sign in to comment.