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

Commit f994ac4

Browse files
committed
Always emit Enum in ExcludeDefaultValues
1 parent eb34349 commit f994ac4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public TypePropertyWriter(Type propertyType, string propertyName, string propert
309309

310310
public bool ShouldWriteProperty(object propertyValue)
311311
{
312-
if ((propertySuppressDefaultAttribute || JsConfig.ExcludeDefaultValues) && Equals(DefaultValue, propertyValue))
312+
if (!isEnum && (propertySuppressDefaultAttribute || JsConfig.ExcludeDefaultValues) && Equals(DefaultValue, propertyValue))
313313
return false;
314314

315315
if (!Serializer.IncludeNullValues

tests/ServiceStack.Text.Tests/EnumTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ public void Can_deserialize_null_Nullable_Enum()
177177
JsConfig.Reset();
178178
}
179179

180+
[Test]
181+
public void Does_write_EnumValues_when_ExcludeDefaultValues()
182+
{
183+
using (JsConfig.With(excludeDefaultValues: true))
184+
{
185+
Assert.That(new ClassWithEnums
186+
{
187+
NoFlagsEnum = EnumWithoutFlags.One
188+
}.ToJson(), Is.EqualTo("{\"FlagsEnum\":0,\"NoFlagsEnum\":\"One\"}"));
189+
190+
Assert.That(new ClassWithEnums
191+
{
192+
NoFlagsEnum = EnumWithoutFlags.Zero
193+
}.ToJson(), Is.EqualTo("{\"FlagsEnum\":0,\"NoFlagsEnum\":\"Zero\"}"));
194+
}
195+
}
196+
180197
}
181198
}
182199

0 commit comments

Comments
 (0)