Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 73 additions & 17 deletions src/ServiceStack.Text/JsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ static JsConfig()
Reset();
}

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

[ThreadStatic]
private static bool? tsConvertObjectTypesIntoStringDictionary;
private static bool? sConvertObjectTypesIntoStringDictionary;
public static bool ConvertObjectTypesIntoStringDictionary
{
get
{
return tsConvertObjectTypesIntoStringDictionary ?? sConvertObjectTypesIntoStringDictionary ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.ConvertObjectTypesIntoStringDictionary: null)
?? tsConvertObjectTypesIntoStringDictionary
?? sConvertObjectTypesIntoStringDictionary
?? false;
}
set
{
Expand All @@ -46,7 +54,10 @@ public static bool TryToParsePrimitiveTypeValues
{
get
{
return tsTryToParsePrimitiveTypeValues ?? sTryToParsePrimitiveTypeValues ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.TryToParsePrimitiveTypeValues: null)
?? tsTryToParsePrimitiveTypeValues
?? sTryToParsePrimitiveTypeValues
?? false;
}
set
{
Expand All @@ -62,7 +73,10 @@ public static bool IncludeNullValues
{
get
{
return tsIncludeNullValues ?? sIncludeNullValues ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeNullValues: null)
?? tsIncludeNullValues
?? sIncludeNullValues
?? false;
}
set
{
Expand All @@ -78,7 +92,10 @@ public static bool TreatEnumAsInteger
{
get
{
return tsTreatEnumAsInteger ?? sTreatEnumAsInteger ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.TreatEnumAsInteger: null)
?? tsTreatEnumAsInteger
?? sTreatEnumAsInteger
?? false;
}
set
{
Expand All @@ -94,7 +111,10 @@ public static bool ExcludeTypeInfo
{
get
{
return tsExcludeTypeInfo ?? sExcludeTypeInfo ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.ExcludeTypeInfo: null)
?? tsExcludeTypeInfo
?? sExcludeTypeInfo
?? false;
}
set
{
Expand All @@ -110,7 +130,10 @@ public static bool IncludeTypeInfo
{
get
{
return tsForceTypeInfo ?? sForceTypeInfo ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.IncludeTypeInfo: null)
?? tsForceTypeInfo
?? sForceTypeInfo
?? false;
}
set
{
Expand All @@ -126,7 +149,10 @@ public static string TypeAttr
{
get
{
return tsTypeAttr ?? sTypeAttr ?? JsWriter.TypeAttr;
return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeAttr: null)
?? tsTypeAttr
?? sTypeAttr
?? JsWriter.TypeAttr;
}
set
{
Expand All @@ -145,7 +171,10 @@ internal static string JsonTypeAttrInObject
{
get
{
return tsJsonTypeAttrInObject ?? sJsonTypeAttrInObject ?? defaultJsonTypeAttrInObject;
return (JsConfigScope.Current != null ? JsConfigScope.Current.JsonTypeAttrInObject: null)
?? tsJsonTypeAttrInObject
?? sJsonTypeAttrInObject
?? defaultJsonTypeAttrInObject;
}
set
{
Expand All @@ -162,7 +191,10 @@ internal static string JsvTypeAttrInObject
{
get
{
return tsJsvTypeAttrInObject ?? sJsvTypeAttrInObject ?? defaultJsvTypeAttrInObject;
return (JsConfigScope.Current != null ? JsConfigScope.Current.JsvTypeAttrInObject: null)
?? tsJsvTypeAttrInObject
?? sJsvTypeAttrInObject
?? defaultJsvTypeAttrInObject;
}
set
{
Expand All @@ -178,7 +210,10 @@ public static Func<Type, string> TypeWriter
{
get
{
return tsTypeWriter ?? sTypeWriter ?? AssemblyUtils.WriteType;
return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeWriter: null)
?? tsTypeWriter
?? sTypeWriter
?? AssemblyUtils.WriteType;
}
set
{
Expand All @@ -194,7 +229,10 @@ public static Func<string, Type> TypeFinder
{
get
{
return tsTypeFinder ?? sTypeFinder ?? AssemblyUtils.FindType;
return (JsConfigScope.Current != null ? JsConfigScope.Current.TypeFinder: null)
?? tsTypeFinder
?? sTypeFinder
?? AssemblyUtils.FindType;
}
set
{
Expand All @@ -210,7 +248,10 @@ public static JsonDateHandler DateHandler
{
get
{
return tsDateHandler ?? sDateHandler ?? JsonDateHandler.TimestampOffset;
return (JsConfigScope.Current != null ? JsConfigScope.Current.DateHandler: null)
?? tsDateHandler
?? sDateHandler
?? JsonDateHandler.TimestampOffset;
}
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
get
{
return tsEmitCamelCaseNames ?? sEmitCamelCaseNames ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.EmitCamelCaseNames: null)
?? tsEmitCamelCaseNames
?? sEmitCamelCaseNames
?? false;
}
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
get
{
return tsEmitLowercaseUnderscoreNames ?? sEmitLowercaseUnderscoreNames ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.EmitLowercaseUnderscoreNames: null)
?? tsEmitLowercaseUnderscoreNames
?? sEmitLowercaseUnderscoreNames
?? false;
}
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
get
{
return tsThrowOnDeserializationError ?? sThrowOnDeserializationError ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.ThrowOnDeserializationError: null)
?? tsThrowOnDeserializationError
?? sThrowOnDeserializationError
?? false;
}
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
get
{
return tsAlwaysUseUtc ?? sAlwaysUseUtc ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.AlwaysUseUtc: null)
?? tsAlwaysUseUtc
?? sAlwaysUseUtc
?? false;
}
set
{
Expand All @@ -347,7 +400,10 @@ public static bool PreferInterfaces
{
get
{
return tsPreferInterfaces ?? sPreferInterfaces ?? false;
return (JsConfigScope.Current != null ? JsConfigScope.Current.PreferInterfaces: null)
?? tsPreferInterfaces
?? sPreferInterfaces
?? false;
}
set
{
Expand Down
73 changes: 73 additions & 0 deletions src/ServiceStack.Text/JsConfigScope.cs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<Compile Include="JsConfig.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="JsConfigScope.cs" />
<Compile Include="JsonObject.cs">
<SubType>Code</SubType>
</Compile>
Expand Down