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

Commit a60ceb4

Browse files
committed
Add JsConfig.ExcludeDefaultValues
1 parent e6cbb86 commit a60ceb4

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public TypePropertyWriter(string propertyName, string propertyDeclaredTypeName,
305305

306306
public bool ShouldWriteProperty(object propertyValue)
307307
{
308-
if (propertySuppressDefaultAttribute && Equals(DefaultValue, propertyValue))
308+
if ((propertySuppressDefaultAttribute || JsConfig.ExcludeDefaultValues) && Equals(DefaultValue, propertyValue))
309309
return false;
310310

311311
if (!Serializer.IncludeNullValues

src/ServiceStack.Text/JsConfig.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static JsConfigScope With(
3333
bool? tryToParseNumericType = null,
3434
ParseAsType? parsePrimitiveFloatingPointTypes = null,
3535
ParseAsType? parsePrimitiveIntegerTypes = null,
36+
bool? excludeDefaultValues = null,
3637
bool? includeNullValues = null,
3738
bool? includeDefaultEnums = null,
3839
bool? excludeTypeInfo = null,
@@ -67,6 +68,7 @@ public static JsConfigScope With(
6768
ParsePrimitiveFloatingPointTypes = parsePrimitiveFloatingPointTypes ?? sParsePrimitiveFloatingPointTypes,
6869
ParsePrimitiveIntegerTypes = parsePrimitiveIntegerTypes ?? sParsePrimitiveIntegerTypes,
6970

71+
ExcludeDefaultValues = excludeDefaultValues ?? sExcludeDefaultValues,
7072
IncludeNullValues = includeNullValues ?? sIncludeNullValues,
7173
IncludeDefaultEnums = includeDefaultEnums ?? sIncludeDefaultEnums,
7274
ExcludeTypeInfo = excludeTypeInfo ?? sExcludeTypeInfo,
@@ -169,6 +171,21 @@ public static ParseAsType ParsePrimitiveIntegerTypes
169171
}
170172
}
171173

174+
private static bool? sExcludeDefaultValues;
175+
public static bool ExcludeDefaultValues
176+
{
177+
get
178+
{
179+
return (JsConfigScope.Current != null ? JsConfigScope.Current.ExcludeDefaultValues : null)
180+
?? sExcludeDefaultValues
181+
?? false;
182+
}
183+
set
184+
{
185+
if (!sExcludeDefaultValues.HasValue) sExcludeDefaultValues = value;
186+
}
187+
}
188+
172189
private static bool? sIncludeNullValues;
173190
public static bool IncludeNullValues
174191
{
@@ -689,6 +706,7 @@ public static void Reset()
689706
sTryToParsePrimitiveTypeValues = null;
690707
sTryToParseNumericType = null;
691708
sConvertObjectTypesIntoStringDictionary = null;
709+
sExcludeDefaultValues = null;
692710
sIncludeNullValues = null;
693711
sExcludeTypeInfo = null;
694712
sEmitCamelCaseNames = null;

src/ServiceStack.Text/JsConfigScope.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public void Dispose()
5858
public bool? TryToParseNumericType { get; set; }
5959
public ParseAsType? ParsePrimitiveFloatingPointTypes { get; set; }
6060
public ParseAsType? ParsePrimitiveIntegerTypes { get; set; }
61+
public bool? ExcludeDefaultValues { get; set; }
6162
public bool? IncludeNullValues { get; set; }
6263
public bool? IncludeDefaultEnums { get; set; }
6364
public bool? TreatEnumAsInteger { get; set; }

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,23 @@ public object OnDeserializing(string fieldName, object value)
146146
return value;
147147
}
148148
}
149+
150+
[DataContract]
151+
public class HasEmitDefaultValue
152+
{
153+
[DataMember(EmitDefaultValue = false)]
154+
public int DontEmitDefaultValue { get; set; }
155+
156+
[DataMember]
157+
public int IntValue { get; set; }
158+
}
159+
160+
[Test]
161+
public void Does_exclude_default_property_with_EmitDefaultValue()
162+
{
163+
var dto = new HasEmitDefaultValue();
164+
165+
Assert.That(dto.ToJson(), Is.EqualTo("{\"IntValue\":0}"));
166+
}
149167
}
150168
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
34
using NUnit.Framework;
45
using ServiceStack.Text.Tests.DynamicModels;
56

@@ -43,6 +44,22 @@ public void Can_Serialize_map()
4344
Console.WriteLine(s);
4445
}
4546

47+
public class ModelWithValueTypes
48+
{
49+
public int Int { get; set; }
50+
public long Long { get; set; }
51+
public float Float { get; set; }
52+
public double Double { get; set; }
53+
}
4654

55+
[Test]
56+
public void Does_ExcludeDefaultValues()
57+
{
58+
JsConfig.ExcludeDefaultValues = true;
59+
60+
var dto = new ModelWithValueTypes();
61+
62+
Assert.That(dto.ToJson(), Is.EqualTo("{}"));
63+
}
4764
}
4865
}

0 commit comments

Comments
 (0)