@@ -127,8 +127,9 @@ private static bool Init()
127
127
string propertyName , propertyNameCLSFriendly , propertyNameLowercaseUnderscore , propertyReflectedName ;
128
128
int propertyOrder = - 1 ;
129
129
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 ;
132
133
133
134
if ( isDataContract )
134
135
{
@@ -140,8 +141,7 @@ private static bool Init()
140
141
propertyNameLowercaseUnderscore = dcsDataMember . Name ?? propertyName . ToLowercaseUnderscore ( ) ;
141
142
propertyReflectedName = dcsDataMember . Name ?? propertyInfo . ReflectedType . Name ;
142
143
propertyOrder = dcsDataMember . Order ;
143
- suppressDefaultValue = ! dcsDataMember . EmitDefaultValue ;
144
- suppressNullValue &= ! dcsDataMember . EmitDefaultValue ;
144
+ propertySuppressDefaultAttribute = ! dcsDataMember . EmitDefaultValue ;
145
145
}
146
146
else
147
147
{
@@ -159,8 +159,8 @@ private static bool Init()
159
159
propertyNameCLSFriendly ,
160
160
propertyNameLowercaseUnderscore ,
161
161
propertyOrder ,
162
- suppressDefaultValue ,
163
- suppressNullValue ,
162
+ propertySuppressDefaultConfig ,
163
+ propertySuppressDefaultAttribute ,
164
164
propertyInfo . GetValueGetter < T > ( ) ,
165
165
Serializer . GetWriteFn ( propertyType ) ,
166
166
propertyType . GetDefaultValue ( )
@@ -174,8 +174,9 @@ private static bool Init()
174
174
string propertyName , propertyNameCLSFriendly , propertyNameLowercaseUnderscore , propertyReflectedName ;
175
175
int propertyOrder = - 1 ;
176
176
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 ;
179
180
180
181
if ( isDataContract )
181
182
{
@@ -187,8 +188,7 @@ private static bool Init()
187
188
propertyNameLowercaseUnderscore = dcsDataMember . Name ?? propertyName . ToLowercaseUnderscore ( ) ;
188
189
propertyReflectedName = dcsDataMember . Name ?? fieldInfo . ReflectedType . Name ;
189
190
propertyOrder = dcsDataMember . Order ;
190
- suppressDefaultValue = ! dcsDataMember . EmitDefaultValue ;
191
- suppressNullValue &= ! dcsDataMember . EmitDefaultValue ;
191
+ propertySuppressDefaultAttribute = ! dcsDataMember . EmitDefaultValue ;
192
192
}
193
193
else
194
194
{
@@ -205,11 +205,11 @@ private static bool Init()
205
205
propertyNameCLSFriendly ,
206
206
propertyNameLowercaseUnderscore ,
207
207
propertyOrder ,
208
- suppressDefaultValue ,
209
- suppressNullValue ,
208
+ propertySuppressDefaultConfig ,
209
+ propertySuppressDefaultAttribute ,
210
210
fieldInfo . GetValueGetter < T > ( ) ,
211
211
Serializer . GetWriteFn ( propertyType ) ,
212
- propertyType . GetDefaultValue ( )
212
+ defaultValue
213
213
) ;
214
214
}
215
215
PropertyWriters = PropertyWriters . OrderBy ( x => x . propertyOrder ) . ToArray ( ) ;
@@ -231,8 +231,8 @@ internal string PropertyName
231
231
}
232
232
internal readonly string propertyName ;
233
233
internal readonly int propertyOrder ;
234
- internal readonly bool propertySuppressDefaultValue ;
235
- internal readonly bool propertySuppressNullValue ;
234
+ internal readonly bool propertySuppressDefaultConfig ;
235
+ internal readonly bool propertySuppressDefaultAttribute ;
236
236
internal readonly string propertyReflectedName ;
237
237
internal readonly string propertyCombinedNameUpper ;
238
238
internal readonly string propertyNameCLSFriendly ;
@@ -241,13 +241,13 @@ internal string PropertyName
241
241
internal readonly WriteObjectDelegate WriteFn ;
242
242
internal readonly object DefaultValue ;
243
243
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 ,
245
245
Func < T , object > getterFn , WriteObjectDelegate writeFn , object defaultValue )
246
246
{
247
247
this . propertyName = propertyName ;
248
248
this . propertyOrder = propertyOrder ;
249
- this . propertySuppressDefaultValue = propertySuppressDefaultValue ;
250
- this . propertySuppressNullValue = propertySuppressNullValue ;
249
+ this . propertySuppressDefaultConfig = propertySuppressDefaultConfig ;
250
+ this . propertySuppressDefaultAttribute = propertySuppressDefaultAttribute ;
251
251
this . propertyReflectedName = propertyReflectedName ;
252
252
this . propertyCombinedNameUpper = propertyReflectedName . ToUpper ( ) + "." + propertyName . ToUpper ( ) ;
253
253
this . propertyNameCLSFriendly = propertyNameCLSFriendly ;
@@ -324,20 +324,17 @@ public static void WriteProperties(TextWriter writer, object value)
324
324
var propertyValue = value != null
325
325
? propertyWriter . GetterFn ( ( T ) value )
326
326
: null ;
327
-
328
- if ( propertyValue == null )
327
+
328
+ if ( propertyWriter . propertySuppressDefaultAttribute && Equals ( propertyWriter . DefaultValue , propertyValue ) )
329
329
{
330
- if ( propertyWriter . propertySuppressNullValue && ! Serializer . IncludeNullValues )
331
- {
332
- continue ;
333
- }
330
+ continue ;
334
331
}
335
- else if ( propertyValue . Equals ( propertyWriter . DefaultValue ) )
332
+ if ( ( propertyValue == null
333
+ || ( propertyWriter . propertySuppressDefaultConfig && Equals ( propertyWriter . DefaultValue , propertyValue ) ) )
334
+ && ! Serializer . IncludeNullValues
335
+ )
336
336
{
337
- if ( propertyWriter . propertySuppressDefaultValue && ! Serializer . IncludeNullValues )
338
- {
339
- continue ;
340
- }
337
+ continue ;
341
338
}
342
339
343
340
if ( exclude . Any ( ) && exclude . Contains ( propertyWriter . propertyCombinedNameUpper ) ) continue ;
0 commit comments