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

Commit 2a59f0b

Browse files
committed
Add JsConfig.IncludeDefaultEnums
1 parent defe1cd commit 2a59f0b

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ private static bool Init()
172172
propertyInfo.GetValueGetter<T>(),
173173
Serializer.GetWriteFn(propertyType),
174174
propertyType.GetDefaultValue(),
175-
shouldSerialize
175+
shouldSerialize,
176+
propertyType.IsEnum()
176177
);
177178
}
178179

@@ -223,7 +224,8 @@ private static bool Init()
223224
fieldInfo.GetValueGetter<T>(),
224225
Serializer.GetWriteFn(propertyType),
225226
defaultValue,
226-
shouldSerialize
227+
shouldSerialize,
228+
propertyType.IsEnum()
227229
);
228230
}
229231

@@ -255,10 +257,11 @@ internal string PropertyName
255257
internal readonly WriteObjectDelegate WriteFn;
256258
internal readonly object DefaultValue;
257259
internal readonly Func<T, bool> shouldSerialize;
260+
internal readonly bool isEnum;
258261

259262
public TypePropertyWriter(string propertyName, string propertyDeclaredTypeName, string propertyNameCLSFriendly,
260263
string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultConfig,bool propertySuppressDefaultAttribute,
261-
Func<T, object> getterFn, WriteObjectDelegate writeFn, object defaultValue, Func<T, bool> shouldSerialize)
264+
Func<T, object> getterFn, WriteObjectDelegate writeFn, object defaultValue, Func<T, bool> shouldSerialize, bool isEnum)
262265
{
263266
this.propertyName = propertyName;
264267
this.propertyOrder = propertyOrder;
@@ -271,6 +274,25 @@ public TypePropertyWriter(string propertyName, string propertyDeclaredTypeName,
271274
this.WriteFn = writeFn;
272275
this.DefaultValue = defaultValue;
273276
this.shouldSerialize = shouldSerialize;
277+
this.isEnum = isEnum;
278+
}
279+
280+
public bool ShouldWriteProperty(object propertyValue)
281+
{
282+
if (propertySuppressDefaultAttribute && Equals(DefaultValue, propertyValue))
283+
return false;
284+
285+
if (!Serializer.IncludeNullValues
286+
&& (propertyValue == null
287+
|| (propertySuppressDefaultConfig && Equals(DefaultValue, propertyValue))))
288+
{
289+
return false;
290+
}
291+
292+
if (isEnum && !JsConfig.IncludeDefaultEnums && Equals(DefaultValue, propertyValue))
293+
return false;
294+
295+
return true;
274296
}
275297
}
276298

@@ -336,23 +358,15 @@ public static void WriteProperties(TextWriter writer, object value)
336358
{
337359
var propertyWriter = PropertyWriters[index];
338360

339-
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)value)) continue;
361+
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)value))
362+
continue;
340363

341364
var propertyValue = value != null
342365
? propertyWriter.GetterFn((T)value)
343366
: null;
344367

345-
if (propertyWriter.propertySuppressDefaultAttribute && Equals(propertyWriter.DefaultValue, propertyValue))
346-
{
347-
continue;
348-
}
349-
if ((propertyValue == null
350-
|| (propertyWriter.propertySuppressDefaultConfig && Equals(propertyWriter.DefaultValue, propertyValue)))
351-
&& !Serializer.IncludeNullValues
352-
)
353-
{
368+
if (!propertyWriter.ShouldWriteProperty(propertyValue))
354369
continue;
355-
}
356370

357371
if (JsConfig.ExcludePropertyReferences != null
358372
&& JsConfig.ExcludePropertyReferences.Contains(propertyWriter.propertyReferenceName)) continue;

src/ServiceStack.Text/JsConfig.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static JsConfigScope With(
3131
bool? tryToParsePrimitiveTypeValues = null,
3232
bool? tryToParseNumericType = null,
3333
bool? includeNullValues = null,
34+
bool? includeDefaultEnums = null,
3435
bool? excludeTypeInfo = null,
3536
bool? includeTypeInfo = null,
3637
bool? emitCamelCaseNames = null,
@@ -58,6 +59,7 @@ public static JsConfigScope With(
5859
TryToParsePrimitiveTypeValues = tryToParsePrimitiveTypeValues ?? sTryToParsePrimitiveTypeValues,
5960
TryToParseNumericType = tryToParseNumericType ?? sTryToParseNumericType,
6061
IncludeNullValues = includeNullValues ?? sIncludeNullValues,
62+
IncludeDefaultEnums = includeDefaultEnums ?? sIncludeDefaultEnums,
6163
ExcludeTypeInfo = excludeTypeInfo ?? sExcludeTypeInfo,
6264
IncludeTypeInfo = includeTypeInfo ?? sIncludeTypeInfo,
6365
EmitCamelCaseNames = emitCamelCaseNames ?? sEmitCamelCaseNames,
@@ -132,8 +134,8 @@ public static bool IncludeNullValues
132134
{
133135
get
134136
{
135-
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeNullValues: null)
136-
?? sIncludeNullValues
137+
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeNullValues : null)
138+
?? sIncludeNullValues
137139
?? false;
138140
}
139141
set
@@ -142,6 +144,21 @@ public static bool IncludeNullValues
142144
}
143145
}
144146

147+
private static bool? sIncludeDefaultEnums;
148+
public static bool IncludeDefaultEnums
149+
{
150+
get
151+
{
152+
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeDefaultEnums : null)
153+
?? sIncludeDefaultEnums
154+
?? true;
155+
}
156+
set
157+
{
158+
if (!sIncludeDefaultEnums.HasValue) sIncludeDefaultEnums = value;
159+
}
160+
}
161+
145162
private static bool? sTreatEnumAsInteger;
146163
public static bool TreatEnumAsInteger
147164
{

src/ServiceStack.Text/JsConfigScope.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void Dispose()
5656
public bool? TryToParsePrimitiveTypeValues { get; set; }
5757
public bool? TryToParseNumericType { get; set; }
5858
public bool? IncludeNullValues { get; set; }
59+
public bool? IncludeDefaultEnums { get; set; }
5960
public bool? TreatEnumAsInteger { get; set; }
6061
public bool? ExcludeTypeInfo { get; set; }
6162
public bool? IncludeTypeInfo { get; set; }

tests/ServiceStack.Text.Tests/EnumTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public void SetUp()
1515

1616
public enum EnumWithoutFlags
1717
{
18+
Zero = 0,
1819
One = 1,
1920
Two = 2
2021
}
2122

2223
[Flags]
2324
public enum EnumWithFlags
2425
{
26+
Zero = 0,
2527
One = 1,
2628
Two = 2
2729
}
@@ -51,6 +53,24 @@ public void Can_correctly_serialize_enums()
5153
Assert.AreEqual(expected, text);
5254
}
5355

56+
[Test]
57+
public void Can_exclude_default_enums()
58+
{
59+
var item = new ClassWithEnums
60+
{
61+
FlagsEnum = EnumWithFlags.Zero,
62+
NoFlagsEnum = EnumWithoutFlags.One,
63+
};
64+
65+
Assert.That(item.ToJson(), Is.EqualTo("{\"FlagsEnum\":0,\"NoFlagsEnum\":\"One\"}"));
66+
67+
JsConfig.IncludeDefaultEnums = false;
68+
69+
Assert.That(item.ToJson(), Is.EqualTo("{\"NoFlagsEnum\":\"One\"}"));
70+
71+
JsConfig.Reset();
72+
}
73+
5474
public void Should_deserialize_enum()
5575
{
5676
Assert.That(JsonSerializer.DeserializeFromString<EnumWithoutFlags>("\"Two\""), Is.EqualTo(EnumWithoutFlags.Two));
@@ -114,7 +134,6 @@ public void Can_use_enum_as_key_in_map()
114134
var map = json.FromJson<Dictionary<AnEnum, int>>();
115135
Assert.That(map[AnEnum.This], Is.EqualTo(1));
116136
}
117-
118137
}
119138
}
120139

0 commit comments

Comments
 (0)