Skip to content

Commit 7a75750

Browse files
committed
Add ThreadStatic/Static backing fields so a thread can globally set an unset config option
1 parent 8b078aa commit 7a75750

10 files changed

Lines changed: 111 additions & 35 deletions

File tree

NuGet/lib/net35/ServiceStack.Text.XML

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1 KB
Binary file not shown.

NuGet/servicestack.text.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
33
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
44
<id>ServiceStack.Text</id>
5-
<version>3.5.2</version>
5+
<version>3.5.3</version>
66
<authors>Demis Bellot</authors>
77
<owners>Demis Bellot</owners>
88
<summary>.NET's fastest JSON, JSV and CSV Text Serializers (3x faster than JSON.NET)</summary>

src/ServiceStack.Text/Env.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static Env()
2323
+ (IsMonoTouch ? " MonoTouch" : "");
2424
}
2525

26-
public static decimal ServiceStackVersion = 3.52m;
26+
public static decimal ServiceStackVersion = 3.53m;
2727

2828
public static bool IsUnix { get; set; }
2929

src/ServiceStack.Text/JsConfig.cs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,81 @@ static JsConfig()
1616
//JsConfig<System.Drawing.Color>.SerializeFn = c => c.ToString().Replace("Color ", "").Replace("[", "").Replace("]", "");
1717
//JsConfig<System.Drawing.Color>.DeSerializeFn = System.Drawing.Color.FromName;
1818
}
19-
19+
2020
[ThreadStatic]
21-
public static bool ConvertObjectTypesIntoStringDictionary = false;
21+
private static bool? tsConvertObjectTypesIntoStringDictionary;
22+
private static bool? sConvertObjectTypesIntoStringDictionary;
23+
public static bool ConvertObjectTypesIntoStringDictionary
24+
{
25+
get
26+
{
27+
return tsConvertObjectTypesIntoStringDictionary ?? sConvertObjectTypesIntoStringDictionary ?? false;
28+
}
29+
set
30+
{
31+
if (!tsConvertObjectTypesIntoStringDictionary.HasValue) tsConvertObjectTypesIntoStringDictionary = value;
32+
if (!sConvertObjectTypesIntoStringDictionary.HasValue) sConvertObjectTypesIntoStringDictionary = value;
33+
}
34+
}
2235

2336
[ThreadStatic]
24-
public static bool IncludeNullValues = false;
37+
private static bool? tsIncludeNullValues;
38+
private static bool? sIncludeNullValues;
39+
public static bool IncludeNullValues
40+
{
41+
get
42+
{
43+
return tsIncludeNullValues ?? sIncludeNullValues ?? false;
44+
}
45+
set
46+
{
47+
if (!tsIncludeNullValues.HasValue) tsIncludeNullValues = value;
48+
if (!sIncludeNullValues.HasValue) sIncludeNullValues = value;
49+
}
50+
}
2551

2652
[ThreadStatic]
27-
public static bool ExcludeTypeInfo = false;
53+
private static bool? tsExcludeTypeInfo;
54+
private static bool? sExcludeTypeInfo;
55+
public static bool ExcludeTypeInfo
56+
{
57+
get
58+
{
59+
return tsExcludeTypeInfo ?? sExcludeTypeInfo ?? false;
60+
}
61+
set
62+
{
63+
if (!tsExcludeTypeInfo.HasValue) tsExcludeTypeInfo = value;
64+
if (!sExcludeTypeInfo.HasValue) sExcludeTypeInfo = value;
65+
}
66+
}
2867

2968
[ThreadStatic]
30-
public static JsonDateHandler DateHandler = JsonDateHandler.TimestampOffset;
69+
private static JsonDateHandler? tsDateHandler;
70+
private static JsonDateHandler? sDateHandler;
71+
public static JsonDateHandler DateHandler
72+
{
73+
get
74+
{
75+
return tsDateHandler ?? sDateHandler ?? JsonDateHandler.TimestampOffset;
76+
}
77+
set
78+
{
79+
if (!tsDateHandler.HasValue) tsDateHandler = value;
80+
if (!sDateHandler.HasValue) sDateHandler = value;
81+
}
82+
}
83+
3184

3285
/// <summary>
3386
/// <see langword="true"/> if the <see cref="ITypeSerializer"/> is configured
3487
/// to take advantage of <see cref="CLSCompliantAttribute"/> specification,
3588
/// to support user-friendly serialized formats, ie emitting camelCasing for JSON
3689
/// and parsing member names and enum values in a case-insensitive manner.
3790
/// </summary>
91+
[ThreadStatic]
92+
private static bool? tsEmitCamelCaseNames;
93+
private static bool? sEmitCamelCaseNames;
3894
public static bool EmitCamelCaseNames
3995
{
4096
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
@@ -49,16 +105,15 @@ public static bool EmitCamelCaseNames
49105
}
50106
}
51107

52-
[ThreadStatic]
53-
private static bool? tsEmitCamelCaseNames;
54-
private static bool? sEmitCamelCaseNames;
55-
56108
internal static HashSet<Type> HasSerializeFn = new HashSet<Type>();
57109

58110
public static void Reset()
59111
{
60-
ConvertObjectTypesIntoStringDictionary = IncludeNullValues = ExcludeTypeInfo = false;
112+
tsConvertObjectTypesIntoStringDictionary = sConvertObjectTypesIntoStringDictionary = null;
113+
tsIncludeNullValues = sIncludeNullValues = null;
114+
tsExcludeTypeInfo = sExcludeTypeInfo = null;
61115
tsEmitCamelCaseNames = sEmitCamelCaseNames = null;
116+
tsDateHandler = sDateHandler = null;
62117
HasSerializeFn = new HashSet<Type>();
63118
}
64119

src/ServiceStack.Text/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("3.5.2.*")]
35+
[assembly: AssemblyVersion("3.5.3.*")]
3636
//[assembly: AssemblyFileVersion("1.0.0.0")]

tests/ServiceStack.Text.Tests/AdhocModelTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public void Can_Serialize_Array_with_nulls()
531531
ServiceStack.Text.JsConfig.IncludeNullValues = true;
532532
var json = ServiceStack.Text.JsonSerializer.SerializeToString(t);
533533
Assert.That(json, Is.EqualTo("{\"Name\":\"MyName\",\"Number\":null,\"Data\":[5,null,\"text\"]}"));
534-
ServiceStack.Text.JsConfig.IncludeNullValues = false;
534+
JsConfig.Reset();
535535
}
536536

537537
class A

tests/ServiceStack.Text.Tests/DictionaryTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public void Can_serialise_null_values_from_dictionary_correctly()
157157
Log(json);
158158

159159
Assert.That(json, Is.EqualTo("{\"value\":null}"));
160+
JsConfig.Reset();
160161
}
161162

162163
[Test]
@@ -171,6 +172,7 @@ public void Will_ignore_null_values_from_dictionary_correctly()
171172
Log(json);
172173

173174
Assert.That(json, Is.EqualTo("{}"));
175+
JsConfig.Reset();
174176
}
175177

176178
public class FooSlash
@@ -196,7 +198,8 @@ public void Can_serialise_null_values_from_nested_dictionary_correctly()
196198
var foo = new FooSlash();
197199
var json = JsonSerializer.SerializeToString(foo);
198200
Assert.That(json, Is.EqualTo("{\"Nested\":null,\"Bar\":null}"));
199-
}
201+
JsConfig.Reset();
202+
}
200203

201204
[Test]
202205
public void Can_serialize_Dictionary_with_quotes()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void Serialize_can_include_null_values()
9999

100100
JsConfig.IncludeNullValues = true;
101101
var s = JsonSerializer.SerializeToString(o);
102-
JsConfig.IncludeNullValues = false;
102+
JsConfig.Reset();
103103
Assert.That(s, Is.EqualTo("{\"Name\":\"Brandon\",\"Type\":\"Programmer\",\"SampleKey\":12,\"Nothing\":null}"));
104104
}
105105

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public void Can_serialize_json_date_timestampOffset_utc()
1515
var dateTime = new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Utc);
1616
var ssJson = JsonSerializer.SerializeToString(dateTime);
1717

18-
Assert.That(ssJson, Is.EqualTo(@"""\/Date(785635200000)\/"""));
18+
Assert.That(ssJson, Is.EqualTo(@"""\/Date(785635200000)\/"""));
19+
JsConfig.Reset();
1920
}
2021

2122
[Test]
@@ -30,7 +31,8 @@ public void Can_serialize_json_date_timestampOffset_local()
3031
var ticks = 785635200000 - offsetSpan.TotalMilliseconds;
3132
var offset = offsetSpan.ToTimeOffsetString();
3233

33-
Assert.That(ssJson, Is.EqualTo(@"""\/Date(" + ticks + offset + @")\/"""));
34+
Assert.That(ssJson, Is.EqualTo(@"""\/Date(" + ticks + offset + @")\/"""));
35+
JsConfig.Reset();
3436
}
3537

3638
[Test]
@@ -46,7 +48,8 @@ public void Can_serialize_json_date_timestampOffset_unspecified()
4648
var dateTime2 = new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Local);
4749
var ssJson2 = JsonSerializer.SerializeToString(dateTime2);
4850

49-
Assert.That(ssJson1, Is.EqualTo(ssJson2));
51+
Assert.That(ssJson1, Is.EqualTo(ssJson2));
52+
JsConfig.Reset();
5053
}
5154

5255
[Test]
@@ -59,7 +62,8 @@ public void Can_deserialize_json_date_timestampOffset_withoutOffset_asUtc()
5962

6063
var dateTime = new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Utc);
6164
Assert.That(fromJson, Is.EqualTo(dateTime));
62-
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
65+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
66+
JsConfig.Reset();
6367
}
6468

6569
[Test]
@@ -72,7 +76,8 @@ public void Can_deserialize_json_date_timestampOffset_withOffset_asUnspecified()
7276

7377
var dateTime = new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Unspecified);
7478
Assert.That(fromJson, Is.EqualTo(dateTime));
75-
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
79+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
80+
JsConfig.Reset();
7681
}
7782

7883
[Test]
@@ -85,7 +90,8 @@ public void Can_deserialize_json_date_timestampOffset_withZeroOffset_asUnspecifi
8590

8691
var dateTime = new DateTime(1994, 11, 24, 0, 0, 0, DateTimeKind.Unspecified);
8792
Assert.That(fromJson, Is.EqualTo(dateTime));
88-
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
93+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
94+
JsConfig.Reset();
8995
}
9096

9197
#endregion
@@ -100,7 +106,8 @@ public void Can_serialize_json_date_dcjsCompatible_utc()
100106
var ssJson = JsonSerializer.SerializeToString(dateTime);
101107
var bclJson = BclJsonDataContractSerializer.Instance.Parse(dateTime);
102108

103-
Assert.That(ssJson, Is.EqualTo(bclJson));
109+
Assert.That(ssJson, Is.EqualTo(bclJson));
110+
JsConfig.Reset();
104111
}
105112

106113
[Test]
@@ -112,7 +119,8 @@ public void Can_serialize_json_date_dcjsCompatible_local()
112119
var ssJson = JsonSerializer.SerializeToString(dateTime);
113120
var bclJson = BclJsonDataContractSerializer.Instance.Parse(dateTime);
114121

115-
Assert.That(ssJson, Is.EqualTo(bclJson));
122+
Assert.That(ssJson, Is.EqualTo(bclJson));
123+
JsConfig.Reset();
116124
}
117125

118126
[Test]
@@ -124,7 +132,8 @@ public void Can_serialize_json_date_dcjsCompatible_unspecified()
124132
var ssJson = JsonSerializer.SerializeToString(dateTime);
125133
var bclJson = BclJsonDataContractSerializer.Instance.Parse(dateTime);
126134

127-
Assert.That(ssJson, Is.EqualTo(bclJson));
135+
Assert.That(ssJson, Is.EqualTo(bclJson));
136+
JsConfig.Reset();
128137
}
129138

130139
[Test]
@@ -138,7 +147,8 @@ public void Can_deserialize_json_date_dcjsCompatible_utc()
138147
var fromBclJson = BclJsonDataContractDeserializer.Instance.Parse<DateTime>(ssJson);
139148

140149
Assert.That(fromJson, Is.EqualTo(fromBclJson));
141-
Assert.That(fromJson.Kind, Is.EqualTo(fromBclJson.Kind));
150+
Assert.That(fromJson.Kind, Is.EqualTo(fromBclJson.Kind));
151+
JsConfig.Reset();
142152
}
143153

144154
[Test]
@@ -152,7 +162,8 @@ public void Can_deserialize_json_date_dcjsCompatible_local()
152162
var fromBclJson = BclJsonDataContractDeserializer.Instance.Parse<DateTime>(ssJson);
153163

154164
Assert.That(fromJson, Is.EqualTo(fromBclJson));
155-
Assert.That(fromJson.Kind, Is.EqualTo(fromBclJson.Kind));
165+
Assert.That(fromJson.Kind, Is.EqualTo(fromBclJson.Kind));
166+
JsConfig.Reset();
156167
}
157168

158169
[Test]
@@ -166,7 +177,8 @@ public void Can_deserialize_json_date_dcjsCompatible_unspecified()
166177
var fromBclJson = BclJsonDataContractDeserializer.Instance.Parse<DateTime>(ssJson);
167178

168179
Assert.That(fromJson, Is.EqualTo(fromBclJson));
169-
Assert.That(fromJson.Kind, Is.EqualTo(fromBclJson.Kind));
180+
Assert.That(fromJson.Kind, Is.EqualTo(fromBclJson.Kind));
181+
JsConfig.Reset();
170182
}
171183
#endregion
172184

@@ -179,7 +191,8 @@ public void Can_serialize_json_date_iso8601_utc()
179191
var dateTime = new DateTime(1994, 11, 24, 12, 34, 56, DateTimeKind.Utc);
180192
var ssJson = JsonSerializer.SerializeToString(dateTime);
181193

182-
Assert.That(ssJson, Is.EqualTo(@"""\/Date(1994-11-24T12:34:56.0000000Z)\/"""));
194+
Assert.That(ssJson, Is.EqualTo(@"""\/Date(1994-11-24T12:34:56.0000000Z)\/"""));
195+
JsConfig.Reset();
183196
}
184197

185198
[Test]
@@ -193,7 +206,8 @@ public void Can_serialize_json_date_iso8601_local()
193206
var offsetSpan = TimeZoneInfo.Local.GetUtcOffset(dateTime);
194207
var offset = offsetSpan.ToTimeOffsetString(true);
195208

196-
Assert.That(ssJson, Is.EqualTo(@"""\/Date(1994-11-24T12:34:56.0000000" + offset + @")\/"""));
209+
Assert.That(ssJson, Is.EqualTo(@"""\/Date(1994-11-24T12:34:56.0000000" + offset + @")\/"""));
210+
JsConfig.Reset();
197211
}
198212

199213
[Test]
@@ -204,7 +218,8 @@ public void Can_serialize_json_date_iso8601_unspecified()
204218
var dateTime = new DateTime(1994, 11, 24, 12, 34, 56, DateTimeKind.Unspecified);
205219
var ssJson = JsonSerializer.SerializeToString(dateTime);
206220

207-
Assert.That(ssJson, Is.EqualTo(@"""\/Date(1994-11-24T12:34:56.0000000)\/"""));
221+
Assert.That(ssJson, Is.EqualTo(@"""\/Date(1994-11-24T12:34:56.0000000)\/"""));
222+
JsConfig.Reset();
208223
}
209224

210225
[Test]
@@ -217,7 +232,8 @@ public void Can_deserialize_json_date_iso8601_withZOffset_asUtc()
217232

218233
var dateTime = new DateTime(1994, 11, 24, 12, 34, 56, DateTimeKind.Utc);
219234
Assert.That(fromJson, Is.EqualTo(dateTime));
220-
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
235+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
236+
JsConfig.Reset();
221237
}
222238

223239
[Test]
@@ -230,7 +246,8 @@ public void Can_deserialize_json_date_iso8601_withoutOffset_asUnspecified()
230246

231247
var dateTime = new DateTime(1994, 11, 24, 12, 34, 56, DateTimeKind.Unspecified);
232248
Assert.That(fromJson, Is.EqualTo(dateTime));
233-
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
249+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
250+
JsConfig.Reset();
234251
}
235252

236253
[Test]
@@ -246,7 +263,8 @@ public void Can_deserialize_json_date_iso8601_withOffset_asLocal()
246263

247264

248265
Assert.That(fromJson, Is.EqualTo(dateTime));
249-
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
266+
Assert.That(fromJson.Kind, Is.EqualTo(dateTime.Kind));
267+
JsConfig.Reset();
250268
}
251269

252270
#endregion

0 commit comments

Comments
 (0)