Skip to content

Commit fe3833a

Browse files
committed
Merge pull request ServiceStack#253 from paultyng/scope-config
Scope config
2 parents 3e95b06 + 69eba8f commit fe3833a

File tree

3 files changed

+147
-17
lines changed

3 files changed

+147
-17
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ static JsConfig()
2323
Reset();
2424
}
2525

26+
public static JsConfigScope BeginScope()
27+
{
28+
return new JsConfigScope();
29+
}
30+
2631
[ThreadStatic]
2732
private static bool? tsConvertObjectTypesIntoStringDictionary;
2833
private static bool? sConvertObjectTypesIntoStringDictionary;
2934
public static bool ConvertObjectTypesIntoStringDictionary
3035
{
3136
get
3237
{
33-
return tsConvertObjectTypesIntoStringDictionary ?? sConvertObjectTypesIntoStringDictionary ?? false;
38+
return (JsConfigScope.Current != null ? JsConfigScope.Current.ConvertObjectTypesIntoStringDictionary: null)
39+
?? tsConvertObjectTypesIntoStringDictionary
40+
?? sConvertObjectTypesIntoStringDictionary
41+
?? false;
3442
}
3543
set
3644
{
@@ -46,7 +54,10 @@ public static bool TryToParsePrimitiveTypeValues
4654
{
4755
get
4856
{
49-
return tsTryToParsePrimitiveTypeValues ?? sTryToParsePrimitiveTypeValues ?? false;
57+
return (JsConfigScope.Current != null ? JsConfigScope.Current.TryToParsePrimitiveTypeValues: null)
58+
?? tsTryToParsePrimitiveTypeValues
59+
?? sTryToParsePrimitiveTypeValues
60+
?? false;
5061
}
5162
set
5263
{
@@ -62,7 +73,10 @@ public static bool IncludeNullValues
6273
{
6374
get
6475
{
65-
return tsIncludeNullValues ?? sIncludeNullValues ?? false;
76+
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeNullValues: null)
77+
?? tsIncludeNullValues
78+
?? sIncludeNullValues
79+
?? false;
6680
}
6781
set
6882
{
@@ -78,7 +92,10 @@ public static bool TreatEnumAsInteger
7892
{
7993
get
8094
{
81-
return tsTreatEnumAsInteger ?? sTreatEnumAsInteger ?? false;
95+
return (JsConfigScope.Current != null ? JsConfigScope.Current.TreatEnumAsInteger: null)
96+
?? tsTreatEnumAsInteger
97+
?? sTreatEnumAsInteger
98+
?? false;
8299
}
83100
set
84101
{
@@ -94,7 +111,10 @@ public static bool ExcludeTypeInfo
94111
{
95112
get
96113
{
97-
return tsExcludeTypeInfo ?? sExcludeTypeInfo ?? false;
114+
return (JsConfigScope.Current != null ? JsConfigScope.Current.ExcludeTypeInfo: null)
115+
?? tsExcludeTypeInfo
116+
?? sExcludeTypeInfo
117+
?? false;
98118
}
99119
set
100120
{
@@ -110,7 +130,10 @@ public static bool IncludeTypeInfo
110130
{
111131
get
112132
{
113-
return tsForceTypeInfo ?? sForceTypeInfo ?? false;
133+
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeTypeInfo: null)
134+
?? tsForceTypeInfo
135+
?? sForceTypeInfo
136+
?? false;
114137
}
115138
set
116139
{
@@ -126,7 +149,10 @@ public static string TypeAttr
126149
{
127150
get
128151
{
129-
return tsTypeAttr ?? sTypeAttr ?? JsWriter.TypeAttr;
152+
return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeAttr: null)
153+
?? tsTypeAttr
154+
?? sTypeAttr
155+
?? JsWriter.TypeAttr;
130156
}
131157
set
132158
{
@@ -145,7 +171,10 @@ internal static string JsonTypeAttrInObject
145171
{
146172
get
147173
{
148-
return tsJsonTypeAttrInObject ?? sJsonTypeAttrInObject ?? defaultJsonTypeAttrInObject;
174+
return (JsConfigScope.Current != null ? JsConfigScope.Current.JsonTypeAttrInObject: null)
175+
?? tsJsonTypeAttrInObject
176+
?? sJsonTypeAttrInObject
177+
?? defaultJsonTypeAttrInObject;
149178
}
150179
set
151180
{
@@ -162,7 +191,10 @@ internal static string JsvTypeAttrInObject
162191
{
163192
get
164193
{
165-
return tsJsvTypeAttrInObject ?? sJsvTypeAttrInObject ?? defaultJsvTypeAttrInObject;
194+
return (JsConfigScope.Current != null ? JsConfigScope.Current.JsvTypeAttrInObject: null)
195+
?? tsJsvTypeAttrInObject
196+
?? sJsvTypeAttrInObject
197+
?? defaultJsvTypeAttrInObject;
166198
}
167199
set
168200
{
@@ -178,7 +210,10 @@ public static Func<Type, string> TypeWriter
178210
{
179211
get
180212
{
181-
return tsTypeWriter ?? sTypeWriter ?? AssemblyUtils.WriteType;
213+
return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeWriter: null)
214+
?? tsTypeWriter
215+
?? sTypeWriter
216+
?? AssemblyUtils.WriteType;
182217
}
183218
set
184219
{
@@ -194,7 +229,10 @@ public static Func<string, Type> TypeFinder
194229
{
195230
get
196231
{
197-
return tsTypeFinder ?? sTypeFinder ?? AssemblyUtils.FindType;
232+
return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeFinder: null)
233+
?? tsTypeFinder
234+
?? sTypeFinder
235+
?? AssemblyUtils.FindType;
198236
}
199237
set
200238
{
@@ -210,7 +248,10 @@ public static JsonDateHandler DateHandler
210248
{
211249
get
212250
{
213-
return tsDateHandler ?? sDateHandler ?? JsonDateHandler.TimestampOffset;
251+
return (JsConfigScope.Current != null ? JsConfigScope.Current.DateHandler: null)
252+
?? tsDateHandler
253+
?? sDateHandler
254+
?? JsonDateHandler.TimestampOffset;
214255
}
215256
set
216257
{
@@ -238,7 +279,10 @@ public static bool EmitCamelCaseNames
238279
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
239280
get
240281
{
241-
return tsEmitCamelCaseNames ?? sEmitCamelCaseNames ?? false;
282+
return (JsConfigScope.Current != null ? JsConfigScope.Current.EmitCamelCaseNames: null)
283+
?? tsEmitCamelCaseNames
284+
?? sEmitCamelCaseNames
285+
?? false;
242286
}
243287
set
244288
{
@@ -259,7 +303,10 @@ public static bool EmitLowercaseUnderscoreNames
259303
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
260304
get
261305
{
262-
return tsEmitLowercaseUnderscoreNames ?? sEmitLowercaseUnderscoreNames ?? false;
306+
return (JsConfigScope.Current != null ? JsConfigScope.Current.EmitLowercaseUnderscoreNames: null)
307+
?? tsEmitLowercaseUnderscoreNames
308+
?? sEmitLowercaseUnderscoreNames
309+
?? false;
263310
}
264311
set
265312
{
@@ -304,7 +351,10 @@ public static bool ThrowOnDeserializationError
304351
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
305352
get
306353
{
307-
return tsThrowOnDeserializationError ?? sThrowOnDeserializationError ?? false;
354+
return (JsConfigScope.Current != null ? JsConfigScope.Current.ThrowOnDeserializationError: null)
355+
?? tsThrowOnDeserializationError
356+
?? sThrowOnDeserializationError
357+
?? false;
308358
}
309359
set
310360
{
@@ -324,7 +374,10 @@ public static bool AlwaysUseUtc
324374
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
325375
get
326376
{
327-
return tsAlwaysUseUtc ?? sAlwaysUseUtc ?? false;
377+
return (JsConfigScope.Current != null ? JsConfigScope.Current.AlwaysUseUtc: null)
378+
?? tsAlwaysUseUtc
379+
?? sAlwaysUseUtc
380+
?? false;
328381
}
329382
set
330383
{
@@ -347,7 +400,10 @@ public static bool PreferInterfaces
347400
{
348401
get
349402
{
350-
return tsPreferInterfaces ?? sPreferInterfaces ?? false;
403+
return (JsConfigScope.Current != null ? JsConfigScope.Current.PreferInterfaces: null)
404+
?? tsPreferInterfaces
405+
?? sPreferInterfaces
406+
?? false;
351407
}
352408
set
353409
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Diagnostics;
7+
8+
namespace ServiceStack.Text
9+
{
10+
public sealed class JsConfigScope : IDisposable
11+
{
12+
bool disposed;
13+
JsConfigScope parent;
14+
15+
[ThreadStatic]
16+
private static JsConfigScope head;
17+
18+
internal JsConfigScope()
19+
{
20+
Thread.BeginThreadAffinity();
21+
parent = head;
22+
head = this;
23+
}
24+
25+
internal static JsConfigScope Current
26+
{
27+
get
28+
{
29+
return head;
30+
}
31+
}
32+
33+
public static void DisposeCurrent()
34+
{
35+
if (head != null)
36+
{
37+
head.Dispose();
38+
}
39+
}
40+
41+
public void Dispose()
42+
{
43+
if (!disposed)
44+
{
45+
disposed = true;
46+
47+
Debug.Assert(this == head, "Disposed out of order.");
48+
49+
head = parent;
50+
51+
Thread.EndThreadAffinity();
52+
}
53+
}
54+
55+
public bool? ConvertObjectTypesIntoStringDictionary { get; set; }
56+
public bool? TryToParsePrimitiveTypeValues { get; set; }
57+
public bool? IncludeNullValues { get; set; }
58+
public bool? TreatEnumAsInteger { get; set; }
59+
public bool? ExcludeTypeInfo { get; set; }
60+
public bool? IncludeTypeInfo { get; set; }
61+
public string TypeAttr { get; set; }
62+
internal string JsonTypeAttrInObject { get; set; }
63+
internal string JsvTypeAttrInObject { get; set; }
64+
public Func<Type, string> TypeWriter { get; set; }
65+
public Func<string, Type> TypeFinder { get; set; }
66+
public JsonDateHandler? DateHandler { get; set; }
67+
public bool? EmitCamelCaseNames { get; set; }
68+
public bool? EmitLowercaseUnderscoreNames { get; set; }
69+
public bool? ThrowOnDeserializationError { get; set; }
70+
public bool? AlwaysUseUtc { get; set; }
71+
public bool? PreferInterfaces { get; set; }
72+
}
73+
}

src/ServiceStack.Text/ServiceStack.Text.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
<Compile Include="JsConfig.cs">
186186
<SubType>Code</SubType>
187187
</Compile>
188+
<Compile Include="JsConfigScope.cs" />
188189
<Compile Include="JsonObject.cs">
189190
<SubType>Code</SubType>
190191
</Compile>

0 commit comments

Comments
 (0)