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

Commit b7ac626

Browse files
committed
Allow includeDefaultEnums to control whether to emit default values
1 parent f8d5e26 commit b7ac626

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,11 @@ public TypePropertyWriter(Type propertyType, string propertyName, string propert
309309

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

315315
if (!Serializer.IncludeNullValues
316-
&& (propertyValue == null
317-
|| (propertySuppressDefaultConfig && Equals(DefaultValue, propertyValue))))
316+
&& (propertyValue == null || (propertySuppressDefaultConfig && Equals(DefaultValue, propertyValue))))
318317
{
319318
return false;
320319
}

tests/ServiceStack.Text.Tests/EnumTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void Can_deserialize_null_Nullable_Enum()
180180
[Test]
181181
public void Does_write_EnumValues_when_ExcludeDefaultValues()
182182
{
183-
using (JsConfig.With(excludeDefaultValues: true))
183+
using (JsConfig.With(excludeDefaultValues:true))
184184
{
185185
Assert.That(new ClassWithEnums
186186
{
@@ -192,6 +192,19 @@ public void Does_write_EnumValues_when_ExcludeDefaultValues()
192192
NoFlagsEnum = EnumWithoutFlags.Zero
193193
}.ToJson(), Is.EqualTo("{\"FlagsEnum\":0,\"NoFlagsEnum\":\"Zero\"}"));
194194
}
195+
196+
using (JsConfig.With(excludeDefaultValues:true, includeDefaultEnums:false))
197+
{
198+
Assert.That(new ClassWithEnums
199+
{
200+
NoFlagsEnum = EnumWithoutFlags.One
201+
}.ToJson(), Is.EqualTo("{\"NoFlagsEnum\":\"One\"}"));
202+
203+
Assert.That(new ClassWithEnums
204+
{
205+
NoFlagsEnum = EnumWithoutFlags.Zero
206+
}.ToJson(), Is.EqualTo("{}"));
207+
}
195208
}
196209

197210
}

0 commit comments

Comments
 (0)