forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsConfigScope.cs
More file actions
238 lines (211 loc) · 9.38 KB
/
JsConfigScope.cs
File metadata and controls
238 lines (211 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using ServiceStack.Text.Json;
using ServiceStack.Text.Jsv;
using ServiceStack.Text.Common;
namespace ServiceStack.Text
{
public sealed class JsConfigScope : Config, IDisposable
{
bool disposed;
readonly JsConfigScope parent;
[ThreadStatic]
private static JsConfigScope head;
internal JsConfigScope()
{
PclExport.Instance.BeginThreadAffinity();
parent = head;
head = this;
}
internal static JsConfigScope Current => head;
public static void DisposeCurrent()
{
head?.Dispose();
}
public void Dispose()
{
if (!disposed)
{
disposed = true;
head = parent;
PclExport.Instance.EndThreadAffinity();
}
}
}
public class Config
{
private static Config instance;
internal static Config Instance => instance ?? (instance = new Config(Defaults));
internal static bool HasInit = false;
public static Config AssertNotInit() => HasInit
? throw new NotSupportedException("JsConfig can't be mutated after JsConfig.Init(). Use BeginScope() or CreateScope() to use custom config after Init().")
: Instance;
private static string InitStackTrace = null;
public static void Init() => Init(null);
public static void Init(Config config)
{
if (HasInit && Env.StrictMode)
throw new NotSupportedException($"JsConfig has already been initialized at: {InitStackTrace}");
if (config != null)
instance = config;
HasInit = true;
InitStackTrace = Environment.StackTrace;
}
/// <summary>
/// Bypass Init checks. Only call on Startup.
/// </summary>
/// <param name="config"></param>
public static void UnsafeInit(Config config)
{
if (config != null)
instance = config;
}
internal static void Reset()
{
HasInit = false;
Instance.Populate(Defaults);
}
public Config()
{
Populate(Instance);
}
private Config(Config config)
{
if (config != null) //Defaults=null, instance=Defaults
Populate(config);
}
public bool ConvertObjectTypesIntoStringDictionary { get; set; }
public bool TryToParsePrimitiveTypeValues { get; set; }
public bool TryToParseNumericType { get; set; }
public bool TryParseIntoBestFit { get; set; }
public ParseAsType ParsePrimitiveFloatingPointTypes { get; set; }
public ParseAsType ParsePrimitiveIntegerTypes { get; set; }
public bool ExcludeDefaultValues { get; set; }
public bool IncludeNullValues { get; set; }
public bool IncludeNullValuesInDictionaries { get; set; }
public bool IncludeDefaultEnums { get; set; }
public bool TreatEnumAsInteger { get; set; }
public bool ExcludeTypeInfo { get; set; }
public bool IncludeTypeInfo { get; set; }
public string TypeAttr { get; set; }
public string DateTimeFormat { 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 Func<string, object> ParsePrimitiveFn { get; set; }
public DateHandler DateHandler { get; set; }
public TimeSpanHandler TimeSpanHandler { get; set; }
public PropertyConvention PropertyConvention { get; set; }
public TextCase TextCase { get; set; }
[Obsolete("Use TextCase = TextCase.CamelCase")]
public bool EmitCamelCaseNames
{
get => TextCase == TextCase.CamelCase;
set => TextCase = value ? TextCase.CamelCase : TextCase;
}
[Obsolete("Use TextCase = TextCase.SnakeCase")]
public bool EmitLowercaseUnderscoreNames
{
get => TextCase == TextCase.SnakeCase;
set => TextCase = value ? TextCase.SnakeCase : TextCase.Default;
}
public bool ThrowOnError { get; set; }
public bool SkipDateTimeConversion { get; set; }
public bool AlwaysUseUtc { get; set; }
public bool AssumeUtc { get; set; }
public bool AppendUtcOffset { get; set; }
public bool PreferInterfaces { get; set; }
public bool IncludePublicFields { get; set; }
public int MaxDepth { get; set; }
public DeserializationErrorDelegate OnDeserializationError { get; set; }
public EmptyCtorFactoryDelegate ModelFactory { get; set; }
public string[] ExcludePropertyReferences { get; set; }
public HashSet<Type> ExcludeTypes { get; set; }
public bool EscapeUnicode { get; set; }
public bool EscapeHtmlChars { get; set; }
public static Config Defaults => new Config(null) {
ConvertObjectTypesIntoStringDictionary = false,
TryToParsePrimitiveTypeValues = false,
TryToParseNumericType = false,
TryParseIntoBestFit = false,
ParsePrimitiveFloatingPointTypes = ParseAsType.Decimal,
ParsePrimitiveIntegerTypes = ParseAsType.Byte | ParseAsType.SByte | ParseAsType.Int16 | ParseAsType.UInt16 |
ParseAsType.Int32 | ParseAsType.UInt32 | ParseAsType.Int64 | ParseAsType.UInt64,
ExcludeDefaultValues = false,
ExcludePropertyReferences = null,
IncludeNullValues = false,
IncludeNullValuesInDictionaries = false,
IncludeDefaultEnums = true,
TreatEnumAsInteger = false,
ExcludeTypeInfo = false,
IncludeTypeInfo = false,
TypeAttr = JsWriter.TypeAttr,
DateTimeFormat = null,
JsonTypeAttrInObject = JsonTypeSerializer.GetTypeAttrInObject(JsWriter.TypeAttr),
JsvTypeAttrInObject = JsvTypeSerializer.GetTypeAttrInObject(JsWriter.TypeAttr),
TypeWriter = AssemblyUtils.WriteType,
TypeFinder = AssemblyUtils.FindType,
ParsePrimitiveFn = null,
DateHandler = Text.DateHandler.TimestampOffset,
TimeSpanHandler = Text.TimeSpanHandler.DurationFormat,
TextCase = TextCase.Default,
PropertyConvention = Text.PropertyConvention.Strict,
ThrowOnError = Env.StrictMode,
SkipDateTimeConversion = false,
AlwaysUseUtc = false,
AssumeUtc = false,
AppendUtcOffset = false,
EscapeUnicode = false,
EscapeHtmlChars = false,
PreferInterfaces = false,
IncludePublicFields = false,
MaxDepth = 50,
OnDeserializationError = null,
ModelFactory = ReflectionExtensions.GetConstructorMethodToCache,
ExcludeTypes = new HashSet<Type> { typeof(System.IO.Stream) },
};
public Config Populate(Config config)
{
ConvertObjectTypesIntoStringDictionary = config.ConvertObjectTypesIntoStringDictionary;
TryToParsePrimitiveTypeValues = config.TryToParsePrimitiveTypeValues;
TryToParseNumericType = config.TryToParseNumericType;
TryParseIntoBestFit = config.TryParseIntoBestFit;
ParsePrimitiveFloatingPointTypes = config.ParsePrimitiveFloatingPointTypes;
ParsePrimitiveIntegerTypes = config.ParsePrimitiveIntegerTypes;
ExcludeDefaultValues = config.ExcludeDefaultValues;
ExcludePropertyReferences = config.ExcludePropertyReferences;
IncludeNullValues = config.IncludeNullValues;
IncludeNullValuesInDictionaries = config.IncludeNullValuesInDictionaries;
IncludeDefaultEnums = config.IncludeDefaultEnums;
TreatEnumAsInteger = config.TreatEnumAsInteger;
ExcludeTypeInfo = config.ExcludeTypeInfo;
IncludeTypeInfo = config.IncludeTypeInfo;
TypeAttr = config.TypeAttr;
DateTimeFormat = config.DateTimeFormat;
JsonTypeAttrInObject = config.JsonTypeAttrInObject;
JsvTypeAttrInObject = config.JsvTypeAttrInObject;
TypeWriter = config.TypeWriter;
TypeFinder = config.TypeFinder;
ParsePrimitiveFn = config.ParsePrimitiveFn;
DateHandler = config.DateHandler;
TimeSpanHandler = config.TimeSpanHandler;
TextCase = config.TextCase;
PropertyConvention = config.PropertyConvention;
ThrowOnError = config.ThrowOnError;
SkipDateTimeConversion = config.SkipDateTimeConversion;
AlwaysUseUtc = config.AlwaysUseUtc;
AssumeUtc = config.AssumeUtc;
AppendUtcOffset = config.AppendUtcOffset;
EscapeUnicode = config.EscapeUnicode;
EscapeHtmlChars = config.EscapeHtmlChars;
PreferInterfaces = config.PreferInterfaces;
IncludePublicFields = config.IncludePublicFields;
MaxDepth = config.MaxDepth;
OnDeserializationError = config.OnDeserializationError;
ModelFactory = config.ModelFactory;
ExcludeTypes = config.ExcludeTypes;
return this;
}
}
}