Skip to content

fix(deserialization): deserialize enums #132

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

Merged
merged 2 commits into from
Jun 14, 2017
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
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
}
}
}