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

Commit 53777c8

Browse files
committed
Make JS<T>-specific options nullable and preferred
1 parent f3d38ea commit 53777c8

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ internal static class WriteType<T, TSerializer>
3131

3232
private static bool IsIncluded
3333
{
34-
get { return (JsConfig.IncludeTypeInfo || JsConfig<T>.IncludeTypeInfo); }
34+
get { return (JsConfig<T>.IncludeTypeInfo.GetValueOrDefault(JsConfig.IncludeTypeInfo)); }
3535
}
36+
3637
private static bool IsExcluded
3738
{
38-
get { return (JsConfig.ExcludeTypeInfo || JsConfig<T>.ExcludeTypeInfo); }
39+
get { return (JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault(JsConfig.ExcludeTypeInfo)); }
3940
}
4041

4142
static WriteType()
@@ -236,9 +237,9 @@ internal string PropertyName
236237
{
237238
get
238239
{
239-
return (JsConfig<T>.EmitCamelCaseNames || JsConfig.EmitCamelCaseNames)
240+
return (JsConfig<T>.EmitCamelCaseNames.GetValueOrDefault(JsConfig.EmitCamelCaseNames))
240241
? propertyNameCLSFriendly
241-
: (JsConfig<T>.EmitLowercaseUnderscoreNames || JsConfig.EmitLowercaseUnderscoreNames)
242+
: (JsConfig<T>.EmitLowercaseUnderscoreNames.GetValueOrDefault(JsConfig.EmitLowercaseUnderscoreNames))
242243
? propertyNameLowercaseUnderscore
243244
: propertyName;
244245
}
@@ -308,9 +309,9 @@ public static void WriteAbstractProperties(TextWriter writer, object value)
308309
}
309310

310311
var writeFn = Serializer.GetWriteFn(valueType);
311-
if (!JsConfig<T>.ExcludeTypeInfo) JsState.IsWritingDynamic = true;
312+
if (!JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault()) JsState.IsWritingDynamic = true;
312313
writeFn(writer, value);
313-
if (!JsConfig<T>.ExcludeTypeInfo) JsState.IsWritingDynamic = false;
314+
if (!JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault()) JsState.IsWritingDynamic = false;
314315
}
315316

316317
public static void WriteProperties(TextWriter writer, object value)

src/ServiceStack.Text/JsConfig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,22 +632,22 @@ public class JsConfig<T>
632632
/// <summary>
633633
/// Always emit type info for this type. Takes precedence over ExcludeTypeInfo
634634
/// </summary>
635-
public static bool IncludeTypeInfo = false;
635+
public static bool? IncludeTypeInfo = null;
636636

637637
/// <summary>
638638
/// Never emit type info for this type
639639
/// </summary>
640-
public static bool ExcludeTypeInfo = false;
640+
public static bool? ExcludeTypeInfo = null;
641641

642642
/// <summary>
643643
/// <see langword="true"/> if the <see cref="ITypeSerializer"/> is configured
644644
/// to take advantage of <see cref="CLSCompliantAttribute"/> specification,
645645
/// to support user-friendly serialized formats, ie emitting camelCasing for JSON
646646
/// and parsing member names and enum values in a case-insensitive manner.
647647
/// </summary>
648-
public static bool EmitCamelCaseNames = false;
648+
public static bool? EmitCamelCaseNames = null;
649649

650-
public static bool EmitLowercaseUnderscoreNames = false;
650+
public static bool? EmitLowercaseUnderscoreNames = null;
651651

652652
/// <summary>
653653
/// Define custom serialization fn for BCL Structs
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NUnit.Framework;
2+
3+
namespace ServiceStack.Text.Tests
4+
{
5+
[TestFixture]
6+
public class JsConfigTests
7+
{
8+
[TestFixtureSetUp]
9+
public void TestFixtureSetUp()
10+
{
11+
JsConfig.EmitLowercaseUnderscoreNames = true;
12+
JsConfig<Bar>.EmitLowercaseUnderscoreNames = false;
13+
}
14+
15+
[TestFixtureTearDown]
16+
public void TestFixtureTearDown()
17+
{
18+
JsConfig.Reset();
19+
}
20+
21+
[Test]
22+
public void Does_use_specific_configuration()
23+
{
24+
Assert.That(new Foo { FooBar = "value" }.ToJson(), Is.EqualTo("{\"foo_bar\":\"value\"}"));
25+
Assert.That(new Bar { FooBar = "value" }.ToJson(), Is.EqualTo("{\"FooBar\":\"value\"}"));
26+
}
27+
28+
[Test]
29+
public void Can_override_default_configuration()
30+
{
31+
using (JsConfig.With(emitLowercaseUnderscoreNames: false))
32+
{
33+
Assert.That(new Foo { FooBar = "value" }.ToJson(), Is.EqualTo("{\"FooBar\":\"value\"}"));
34+
}
35+
}
36+
}
37+
38+
public class Foo
39+
{
40+
public string FooBar { get; set; }
41+
}
42+
43+
public class Bar
44+
{
45+
public string FooBar { get; set; }
46+
}
47+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
<Compile Include="EnumTests.cs" />
195195
<Compile Include="ExpandoTests.cs" />
196196
<Compile Include="HttpUtilsMockTests.cs" />
197+
<Compile Include="JsConfigTests.cs" />
197198
<Compile Include="JsonTests\BasicPropertiesTests.cs" />
198199
<Compile Include="JsonObjectTests.cs" />
199200
<Compile Include="JsonTests\AnonymousDeserializationTests.cs" />

0 commit comments

Comments
 (0)