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

Commit 328ce9e

Browse files
committed
Add JsConfig<T>.IncludeDefaultValue
1 parent dd9449b commit 328ce9e

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ private static bool Init()
147147
int propertyOrder = -1;
148148
var propertyType = propertyInfo.PropertyType;
149149
var defaultValue = propertyType.GetDefaultValue();
150-
bool propertySuppressDefaultConfig = defaultValue != null && propertyType.IsValueType() && !propertyType.IsEnum() && JsConfig.HasSerializeFn.Contains(propertyType);
150+
bool propertySuppressDefaultConfig = defaultValue != null
151+
&& propertyType.IsValueType()
152+
&& !propertyType.IsEnum()
153+
&& JsConfig.HasSerializeFn.Contains(propertyType)
154+
&& !JsConfig.HasIncludeDefaultValue.Contains(propertyType);
151155
bool propertySuppressDefaultAttribute = false;
152156

153157
var shouldSerialize = GetShouldSerializeMethod(propertyInfo);
@@ -198,7 +202,10 @@ private static bool Init()
198202
int propertyOrder = -1;
199203
var propertyType = fieldInfo.FieldType;
200204
var defaultValue = propertyType.GetDefaultValue();
201-
bool propertySuppressDefaultConfig = defaultValue != null && propertyType.IsValueType() && !propertyType.IsEnum() && JsConfig.HasSerializeFn.Contains(propertyType);
205+
bool propertySuppressDefaultConfig = defaultValue != null
206+
&& propertyType.IsValueType() && !propertyType.IsEnum()
207+
&& JsConfig.HasSerializeFn.Contains(propertyType)
208+
&& !JsConfig.HasIncludeDefaultValue.Contains(propertyType);
202209
bool propertySuppressDefaultAttribute = false;
203210
#if (NETFX_CORE)
204211
var shouldSerialize = (Func<T, bool>)null;

src/ServiceStack.Text/JsConfig.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ public static bool EscapeUnicode
518518

519519
internal static HashSet<Type> HasSerializeFn = new HashSet<Type>();
520520

521+
internal static HashSet<Type> HasIncludeDefaultValue = new HashSet<Type>();
522+
521523
public static HashSet<Type> TreatValueAsRefTypes = new HashSet<Type>();
522524

523525
private static bool? sPreferInterfaces;
@@ -658,6 +660,10 @@ public static void Reset()
658660
{
659661
Reset(rawSerializeType);
660662
}
663+
foreach (var rawSerializeType in HasIncludeDefaultValue.ToArray())
664+
{
665+
Reset(rawSerializeType);
666+
}
661667
foreach (var uniqueType in __uniqueTypes.ToArray())
662668
{
663669
Reset(uniqueType);
@@ -688,6 +694,7 @@ public static void Reset()
688694
sIncludePublicFields = null;
689695
sReuseStringBuffer = null;
690696
HasSerializeFn = new HashSet<Type>();
697+
HasIncludeDefaultValue = new HashSet<Type>();
691698
TreatValueAsRefTypes = new HashSet<Type> { typeof(KeyValuePair<,>) };
692699
sPropertyConvention = null;
693700
sExcludePropertyReferences = null;
@@ -755,6 +762,20 @@ public class JsConfig<T>
755762

756763
public static bool? EmitLowercaseUnderscoreNames = null;
757764

765+
public static bool IncludeDefaultValue
766+
{
767+
get { return JsConfig.HasIncludeDefaultValue.Contains(typeof(T)); }
768+
set
769+
{
770+
if (value)
771+
JsConfig.HasIncludeDefaultValue.Add(typeof(T));
772+
else
773+
JsConfig.HasIncludeDefaultValue.Remove(typeof(T));
774+
775+
ClearFnCaches();
776+
}
777+
}
778+
758779
/// <summary>
759780
/// Define custom serialization fn for BCL Structs
760781
/// </summary>

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ public void Can_deserialize_json_with_underscores()
232232
dto.PrintDump();
233233
}
234234
}
235+
}
235236

236-
[Test]
237+
public class CustomSerailizerValueTypeTests
238+
{
239+
[Ignore("Need to clear static element caches"), Test]
237240
public void Can_serialize_custom_doubles()
238241
{
239242
JsConfig<double>.RawSerializeFn = d =>
@@ -249,5 +252,25 @@ public void Can_serialize_custom_doubles()
249252

250253
Assert.That(doubles.ToJson(), Is.EqualTo("[0,1,\"-Inf\",\"NaN\",\"+Inf\"]"));
251254
}
255+
256+
public class Model
257+
{
258+
public int Int { get; set; }
259+
}
260+
261+
[Test]
262+
public void Can_serialize_custom_ints()
263+
{
264+
JsConfig<int>.IncludeDefaultValue = true;
265+
JsConfig<int>.RawSerializeFn = i =>
266+
i == 0 ? "-1" : i.ToString();
267+
268+
var dto = new Model { Int = 0 };
269+
270+
Assert.That(dto.ToJson(), Is.EqualTo("{\"Int\":-1}"));
271+
272+
JsConfig.Reset();
273+
}
252274
}
275+
253276
}

0 commit comments

Comments
 (0)