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

Commit

Permalink
Merge pull request #253 from paultyng/scope-config
Browse files Browse the repository at this point in the history
Scope config
  • Loading branch information
mythz committed Dec 12, 2012
2 parents 3e95b06 + 69eba8f commit fe3833a
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 17 deletions.
90 changes: 73 additions & 17 deletions src/ServiceStack.Text/JsConfig.cs
Expand Up @@ -23,14 +23,22 @@ static JsConfig()
Reset(); Reset();
} }


public static JsConfigScope BeginScope()
{
return new JsConfigScope();
}

[ThreadStatic] [ThreadStatic]
private static bool? tsConvertObjectTypesIntoStringDictionary; private static bool? tsConvertObjectTypesIntoStringDictionary;
private static bool? sConvertObjectTypesIntoStringDictionary; private static bool? sConvertObjectTypesIntoStringDictionary;
public static bool ConvertObjectTypesIntoStringDictionary public static bool ConvertObjectTypesIntoStringDictionary
{ {
get get
{ {
return tsConvertObjectTypesIntoStringDictionary ?? sConvertObjectTypesIntoStringDictionary ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.ConvertObjectTypesIntoStringDictionary: null)
?? tsConvertObjectTypesIntoStringDictionary
?? sConvertObjectTypesIntoStringDictionary
?? false;
} }
set set
{ {
Expand All @@ -46,7 +54,10 @@ public static bool TryToParsePrimitiveTypeValues
{ {
get get
{ {
return tsTryToParsePrimitiveTypeValues ?? sTryToParsePrimitiveTypeValues ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.TryToParsePrimitiveTypeValues: null)
?? tsTryToParsePrimitiveTypeValues
?? sTryToParsePrimitiveTypeValues
?? false;
} }
set set
{ {
Expand All @@ -62,7 +73,10 @@ public static bool IncludeNullValues
{ {
get get
{ {
return tsIncludeNullValues ?? sIncludeNullValues ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeNullValues: null)
?? tsIncludeNullValues
?? sIncludeNullValues
?? false;
} }
set set
{ {
Expand All @@ -78,7 +92,10 @@ public static bool TreatEnumAsInteger
{ {
get get
{ {
return tsTreatEnumAsInteger ?? sTreatEnumAsInteger ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.TreatEnumAsInteger: null)
?? tsTreatEnumAsInteger
?? sTreatEnumAsInteger
?? false;
} }
set set
{ {
Expand All @@ -94,7 +111,10 @@ public static bool ExcludeTypeInfo
{ {
get get
{ {
return tsExcludeTypeInfo ?? sExcludeTypeInfo ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.ExcludeTypeInfo: null)
?? tsExcludeTypeInfo
?? sExcludeTypeInfo
?? false;
} }
set set
{ {
Expand All @@ -110,7 +130,10 @@ public static bool IncludeTypeInfo
{ {
get get
{ {
return tsForceTypeInfo ?? sForceTypeInfo ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeTypeInfo: null)
?? tsForceTypeInfo
?? sForceTypeInfo
?? false;
} }
set set
{ {
Expand All @@ -126,7 +149,10 @@ public static string TypeAttr
{ {
get get
{ {
return tsTypeAttr ?? sTypeAttr ?? JsWriter.TypeAttr; return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeAttr: null)
?? tsTypeAttr
?? sTypeAttr
?? JsWriter.TypeAttr;
} }
set set
{ {
Expand All @@ -145,7 +171,10 @@ internal static string JsonTypeAttrInObject
{ {
get get
{ {
return tsJsonTypeAttrInObject ?? sJsonTypeAttrInObject ?? defaultJsonTypeAttrInObject; return (JsConfigScope.Current != null ? JsConfigScope.Current.JsonTypeAttrInObject: null)
?? tsJsonTypeAttrInObject
?? sJsonTypeAttrInObject
?? defaultJsonTypeAttrInObject;
} }
set set
{ {
Expand All @@ -162,7 +191,10 @@ internal static string JsvTypeAttrInObject
{ {
get get
{ {
return tsJsvTypeAttrInObject ?? sJsvTypeAttrInObject ?? defaultJsvTypeAttrInObject; return (JsConfigScope.Current != null ? JsConfigScope.Current.JsvTypeAttrInObject: null)
?? tsJsvTypeAttrInObject
?? sJsvTypeAttrInObject
?? defaultJsvTypeAttrInObject;
} }
set set
{ {
Expand All @@ -178,7 +210,10 @@ internal static string JsvTypeAttrInObject
{ {
get get
{ {
return tsTypeWriter ?? sTypeWriter ?? AssemblyUtils.WriteType; return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeWriter: null)
?? tsTypeWriter
?? sTypeWriter
?? AssemblyUtils.WriteType;
} }
set set
{ {
Expand All @@ -194,7 +229,10 @@ internal static string JsvTypeAttrInObject
{ {
get get
{ {
return tsTypeFinder ?? sTypeFinder ?? AssemblyUtils.FindType; return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeFinder: null)
?? tsTypeFinder
?? sTypeFinder
?? AssemblyUtils.FindType;
} }
set set
{ {
Expand All @@ -210,7 +248,10 @@ public static JsonDateHandler DateHandler
{ {
get get
{ {
return tsDateHandler ?? sDateHandler ?? JsonDateHandler.TimestampOffset; return (JsConfigScope.Current != null ? JsConfigScope.Current.DateHandler: null)
?? tsDateHandler
?? sDateHandler
?? JsonDateHandler.TimestampOffset;
} }
set set
{ {
Expand Down Expand Up @@ -238,7 +279,10 @@ public static bool EmitCamelCaseNames
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case // obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
get get
{ {
return tsEmitCamelCaseNames ?? sEmitCamelCaseNames ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.EmitCamelCaseNames: null)
?? tsEmitCamelCaseNames
?? sEmitCamelCaseNames
?? false;
} }
set set
{ {
Expand All @@ -259,7 +303,10 @@ public static bool EmitLowercaseUnderscoreNames
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case // obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
get get
{ {
return tsEmitLowercaseUnderscoreNames ?? sEmitLowercaseUnderscoreNames ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.EmitLowercaseUnderscoreNames: null)
?? tsEmitLowercaseUnderscoreNames
?? sEmitLowercaseUnderscoreNames
?? false;
} }
set set
{ {
Expand Down Expand Up @@ -304,7 +351,10 @@ public static bool ThrowOnDeserializationError
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case // obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
get get
{ {
return tsThrowOnDeserializationError ?? sThrowOnDeserializationError ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.ThrowOnDeserializationError: null)
?? tsThrowOnDeserializationError
?? sThrowOnDeserializationError
?? false;
} }
set set
{ {
Expand All @@ -324,7 +374,10 @@ public static bool AlwaysUseUtc
// obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case // obeying the use of ThreadStatic, but allowing for setting JsConfig once as is the normal case
get get
{ {
return tsAlwaysUseUtc ?? sAlwaysUseUtc ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.AlwaysUseUtc: null)
?? tsAlwaysUseUtc
?? sAlwaysUseUtc
?? false;
} }
set set
{ {
Expand All @@ -347,7 +400,10 @@ public static bool PreferInterfaces
{ {
get get
{ {
return tsPreferInterfaces ?? sPreferInterfaces ?? false; return (JsConfigScope.Current != null ? JsConfigScope.Current.PreferInterfaces: null)
?? tsPreferInterfaces
?? sPreferInterfaces
?? false;
} }
set set
{ {
Expand Down
73 changes: 73 additions & 0 deletions src/ServiceStack.Text/JsConfigScope.cs
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace ServiceStack.Text
{
public sealed class JsConfigScope : IDisposable
{
bool disposed;
JsConfigScope parent;

[ThreadStatic]
private static JsConfigScope head;

internal JsConfigScope()
{
Thread.BeginThreadAffinity();
parent = head;
head = this;
}

internal static JsConfigScope Current
{
get
{
return head;
}
}

public static void DisposeCurrent()
{
if (head != null)
{
head.Dispose();
}
}

public void Dispose()
{
if (!disposed)
{
disposed = true;

Debug.Assert(this == head, "Disposed out of order.");

head = parent;

Thread.EndThreadAffinity();
}
}

public bool? ConvertObjectTypesIntoStringDictionary { get; set; }
public bool? TryToParsePrimitiveTypeValues { get; set; }
public bool? IncludeNullValues { get; set; }
public bool? TreatEnumAsInteger { get; set; }
public bool? ExcludeTypeInfo { get; set; }
public bool? IncludeTypeInfo { get; set; }
public string TypeAttr { get; set; }
internal string JsonTypeAttrInObject { get; set; }
internal string JsvTypeAttrInObject { get; set; }
public Func<Type, string> TypeWriter { get; set; }
public Func<string, Type> TypeFinder { get; set; }
public JsonDateHandler? DateHandler { get; set; }
public bool? EmitCamelCaseNames { get; set; }
public bool? EmitLowercaseUnderscoreNames { get; set; }
public bool? ThrowOnDeserializationError { get; set; }
public bool? AlwaysUseUtc { get; set; }
public bool? PreferInterfaces { get; set; }
}
}
1 change: 1 addition & 0 deletions src/ServiceStack.Text/ServiceStack.Text.csproj
Expand Up @@ -185,6 +185,7 @@
<Compile Include="JsConfig.cs"> <Compile Include="JsConfig.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="JsConfigScope.cs" />
<Compile Include="JsonObject.cs"> <Compile Include="JsonObject.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
Expand Down

0 comments on commit fe3833a

Please sign in to comment.