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

Commit 51d5a43

Browse files
committed
Changed the code to minimize side effects
1 parent e9ad33a commit 51d5a43

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ private static bool Init()
127127
string propertyName, propertyNameCLSFriendly, propertyNameLowercaseUnderscore, propertyReflectedName;
128128
int propertyOrder = -1;
129129
var propertyType = propertyInfo.PropertyType;
130-
bool suppressDefaultValue = !propertyType.IsValueType() || JsConfig.HasSerializeFn.Contains(propertyType);
131-
bool suppressNullValue = propertyType.GetDefaultValue() == null;
130+
var defaultValue = propertyType.GetDefaultValue();
131+
bool propertySuppressDefaultConfig = defaultValue != null && propertyType.IsValueType() && JsConfig.HasSerializeFn.Contains(propertyType);
132+
bool propertySuppressDefaultAttribute = false;
132133

133134
if (isDataContract)
134135
{
@@ -140,8 +141,7 @@ private static bool Init()
140141
propertyNameLowercaseUnderscore = dcsDataMember.Name ?? propertyName.ToLowercaseUnderscore();
141142
propertyReflectedName = dcsDataMember.Name ?? propertyInfo.ReflectedType.Name;
142143
propertyOrder = dcsDataMember.Order;
143-
suppressDefaultValue = !dcsDataMember.EmitDefaultValue;
144-
suppressNullValue &= !dcsDataMember.EmitDefaultValue;
144+
propertySuppressDefaultAttribute = !dcsDataMember.EmitDefaultValue;
145145
}
146146
else
147147
{
@@ -159,8 +159,8 @@ private static bool Init()
159159
propertyNameCLSFriendly,
160160
propertyNameLowercaseUnderscore,
161161
propertyOrder,
162-
suppressDefaultValue,
163-
suppressNullValue,
162+
propertySuppressDefaultConfig,
163+
propertySuppressDefaultAttribute,
164164
propertyInfo.GetValueGetter<T>(),
165165
Serializer.GetWriteFn(propertyType),
166166
propertyType.GetDefaultValue()
@@ -174,8 +174,9 @@ private static bool Init()
174174
string propertyName, propertyNameCLSFriendly, propertyNameLowercaseUnderscore, propertyReflectedName;
175175
int propertyOrder = -1;
176176
var propertyType = fieldInfo.FieldType;
177-
bool suppressDefaultValue = !propertyType.IsValueType() || JsConfig.HasSerializeFn.Contains(propertyType);
178-
bool suppressNullValue = propertyType.GetDefaultValue()==null;
177+
var defaultValue = propertyType.GetDefaultValue();
178+
bool propertySuppressDefaultConfig = defaultValue != null && propertyType.IsValueType() && JsConfig.HasSerializeFn.Contains(propertyType);
179+
bool propertySuppressDefaultAttribute = false;
179180

180181
if (isDataContract)
181182
{
@@ -187,8 +188,7 @@ private static bool Init()
187188
propertyNameLowercaseUnderscore = dcsDataMember.Name ?? propertyName.ToLowercaseUnderscore();
188189
propertyReflectedName = dcsDataMember.Name ?? fieldInfo.ReflectedType.Name;
189190
propertyOrder = dcsDataMember.Order;
190-
suppressDefaultValue = !dcsDataMember.EmitDefaultValue;
191-
suppressNullValue &= !dcsDataMember.EmitDefaultValue;
191+
propertySuppressDefaultAttribute = !dcsDataMember.EmitDefaultValue;
192192
}
193193
else
194194
{
@@ -205,11 +205,11 @@ private static bool Init()
205205
propertyNameCLSFriendly,
206206
propertyNameLowercaseUnderscore,
207207
propertyOrder,
208-
suppressDefaultValue,
209-
suppressNullValue,
208+
propertySuppressDefaultConfig,
209+
propertySuppressDefaultAttribute,
210210
fieldInfo.GetValueGetter<T>(),
211211
Serializer.GetWriteFn(propertyType),
212-
propertyType.GetDefaultValue()
212+
defaultValue
213213
);
214214
}
215215
PropertyWriters = PropertyWriters.OrderBy(x => x.propertyOrder).ToArray();
@@ -231,8 +231,8 @@ internal string PropertyName
231231
}
232232
internal readonly string propertyName;
233233
internal readonly int propertyOrder;
234-
internal readonly bool propertySuppressDefaultValue;
235-
internal readonly bool propertySuppressNullValue;
234+
internal readonly bool propertySuppressDefaultConfig;
235+
internal readonly bool propertySuppressDefaultAttribute;
236236
internal readonly string propertyReflectedName;
237237
internal readonly string propertyCombinedNameUpper;
238238
internal readonly string propertyNameCLSFriendly;
@@ -241,13 +241,13 @@ internal string PropertyName
241241
internal readonly WriteObjectDelegate WriteFn;
242242
internal readonly object DefaultValue;
243243

244-
public TypePropertyWriter(string propertyName, string propertyReflectedName, string propertyNameCLSFriendly, string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultValue,bool propertySuppressNullValue,
244+
public TypePropertyWriter(string propertyName, string propertyReflectedName, string propertyNameCLSFriendly, string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultConfig,bool propertySuppressDefaultAttribute,
245245
Func<T, object> getterFn, WriteObjectDelegate writeFn, object defaultValue)
246246
{
247247
this.propertyName = propertyName;
248248
this.propertyOrder = propertyOrder;
249-
this.propertySuppressDefaultValue = propertySuppressDefaultValue;
250-
this.propertySuppressNullValue = propertySuppressNullValue;
249+
this.propertySuppressDefaultConfig = propertySuppressDefaultConfig;
250+
this.propertySuppressDefaultAttribute = propertySuppressDefaultAttribute;
251251
this.propertyReflectedName = propertyReflectedName;
252252
this.propertyCombinedNameUpper = propertyReflectedName.ToUpper() + "." + propertyName.ToUpper();
253253
this.propertyNameCLSFriendly = propertyNameCLSFriendly;
@@ -324,20 +324,17 @@ public static void WriteProperties(TextWriter writer, object value)
324324
var propertyValue = value != null
325325
? propertyWriter.GetterFn((T)value)
326326
: null;
327-
328-
if (propertyValue == null)
327+
328+
if (propertyWriter.propertySuppressDefaultAttribute && Equals(propertyWriter.DefaultValue, propertyValue))
329329
{
330-
if (propertyWriter.propertySuppressNullValue && !Serializer.IncludeNullValues)
331-
{
332-
continue;
333-
}
330+
continue;
334331
}
335-
else if (propertyValue.Equals(propertyWriter.DefaultValue))
332+
if ((propertyValue == null
333+
|| (propertyWriter.propertySuppressDefaultConfig && Equals(propertyWriter.DefaultValue, propertyValue)))
334+
&& !Serializer.IncludeNullValues
335+
)
336336
{
337-
if (propertyWriter.propertySuppressDefaultValue && !Serializer.IncludeNullValues)
338-
{
339-
continue;
340-
}
337+
continue;
341338
}
342339

343340
if (exclude.Any() && exclude.Contains(propertyWriter.propertyCombinedNameUpper)) continue;

tests/ServiceStack.Text.Tests/JsonTests/JsonDataContractCompatibilityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Respects_EmitDefaultValue()
3333
{
3434
using (var x = JsConfig.BeginScope())
3535
{
36-
x.IncludeNullValues = false;
36+
x.IncludeNullValues = true;
3737

3838
var jsonModel = new Movie { Genres = null };
3939

0 commit comments

Comments
 (0)