Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5865256

Browse files
committed
Add fix for deserializing nullable enums
1 parent 486615e commit 5865256

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/ServiceStack.Text/Common/ParseUtils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public static Type ParseType(string assemblyQualifiedName)
6363

6464
public static object TryParseEnum(Type enumType, string str)
6565
{
66+
if (str == null)
67+
return null;
68+
6669
if (JsConfig.EmitLowercaseUnderscoreNames)
6770
{
6871
string[] names = Enum.GetNames(enumType);

tests/ServiceStack.Text.Tests/EnumTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
34
using NUnit.Framework;
45

56
namespace ServiceStack.Text.Tests
@@ -157,6 +158,25 @@ public void Can_serialize_different_enum_styles()
157158
Assert.That("Underscore_Words".FromJson<EnumStyles>(), Is.EqualTo(EnumStyles.Underscore_Words));
158159
}
159160
}
161+
162+
[DataContract]
163+
public class NullableEnum
164+
{
165+
[DataMember(Name = "myEnum")]
166+
public EnumWithoutFlags? MyEnum { get; set; }
167+
}
168+
169+
[Test]
170+
public void Can_deserialize_null_Nullable_Enum()
171+
{
172+
JsConfig.ThrowOnDeserializationError = true;
173+
string json = @"{""myEnum"":null}";
174+
var o = json.FromJson<NullableEnum>();
175+
Assert.That(o.MyEnum, Is.Null);
176+
177+
JsConfig.Reset();
178+
}
179+
160180
}
161181
}
162182

0 commit comments

Comments
 (0)