forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryStringSerializer.cs
More file actions
278 lines (236 loc) · 8.6 KB
/
QueryStringSerializer.cs
File metadata and controls
278 lines (236 loc) · 8.6 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// https://github.com/ServiceStack/ServiceStack.Text
// ServiceStack.Text: .NET C# POCO JSON, JSV and CSV Text Serializers.
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2012 Service Stack LLC. All Rights Reserved.
//
// Licensed under the same terms of ServiceStack.
//
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using ServiceStack.Text;
using ServiceStack.Text.Common;
using ServiceStack.Text.Json;
using ServiceStack.Text.Jsv;
namespace ServiceStack
{
public static class QueryStringSerializer
{
static QueryStringSerializer()
{
JsConfig.InitStatics();
Instance = new JsWriter<JsvTypeSerializer>();
}
internal static readonly JsWriter<JsvTypeSerializer> Instance;
private static Dictionary<Type, WriteObjectDelegate> WriteFnCache = new Dictionary<Type, WriteObjectDelegate>();
public static WriteComplexTypeDelegate ComplexTypeStrategy { get; set; }
internal static WriteObjectDelegate GetWriteFn(Type type)
{
try
{
WriteObjectDelegate writeFn;
if (WriteFnCache.TryGetValue(type, out writeFn)) return writeFn;
var genericType = typeof(QueryStringWriter<>).MakeGenericType(type);
var mi = genericType.GetStaticMethod("WriteFn");
var writeFactoryFn = (Func<WriteObjectDelegate>)mi.MakeDelegate(
typeof(Func<WriteObjectDelegate>));
writeFn = writeFactoryFn();
Dictionary<Type, WriteObjectDelegate> snapshot, newCache;
do
{
snapshot = WriteFnCache;
newCache = new Dictionary<Type, WriteObjectDelegate>(WriteFnCache);
newCache[type] = writeFn;
} while (!ReferenceEquals(
Interlocked.CompareExchange(ref WriteFnCache, newCache, snapshot), snapshot));
return writeFn;
}
catch (Exception ex)
{
Tracer.Instance.WriteError(ex);
throw;
}
}
public static void WriteLateBoundObject(TextWriter writer, object value)
{
if (value == null) return;
var writeFn = GetWriteFn(value.GetType());
writeFn(writer, value);
}
internal static WriteObjectDelegate GetValueTypeToStringMethod(Type type)
{
return Instance.GetValueTypeToStringMethod(type);
}
public static string SerializeToString<T>(T value)
{
var writer = StringWriterThreadStatic.Allocate();
GetWriteFn(value.GetType())(writer, value);
return StringWriterThreadStatic.ReturnAndFree(writer);
}
}
/// <summary>
/// Implement the serializer using a more static approach
/// </summary>
/// <typeparam name="T"></typeparam>
public static class QueryStringWriter<T>
{
private static readonly WriteObjectDelegate CacheFn;
public static WriteObjectDelegate WriteFn()
{
return CacheFn;
}
static QueryStringWriter()
{
if (typeof(T) == typeof(object))
{
CacheFn = QueryStringSerializer.WriteLateBoundObject;
}
else if (typeof(T).AssignableFrom(typeof(IDictionary))
|| typeof(T).HasInterface(typeof(IDictionary)))
{
CacheFn = WriteIDictionary;
}
else
{
var isEnumerable = typeof(T).AssignableFrom(typeof(IEnumerable))
|| typeof(T).HasInterface(typeof(IEnumerable));
if ((typeof(T).IsClass() || typeof(T).IsInterface())
&& !isEnumerable)
{
var canWriteType = WriteType<T, JsvTypeSerializer>.Write;
if (canWriteType != null)
{
CacheFn = WriteType<T, JsvTypeSerializer>.WriteQueryString;
return;
}
}
CacheFn = QueryStringSerializer.Instance.GetWriteFn<T>();
}
}
public static void WriteObject(TextWriter writer, object value)
{
if (writer == null) return;
CacheFn(writer, value);
}
private static readonly ITypeSerializer Serializer = JsvTypeSerializer.Instance;
public static void WriteIDictionary(TextWriter writer, object oMap)
{
WriteObjectDelegate writeKeyFn = null;
WriteObjectDelegate writeValueFn = null;
try
{
JsState.QueryStringMode = true;
var map = (IDictionary)oMap;
var ranOnce = false;
foreach (var key in map.Keys)
{
var dictionaryValue = map[key];
if (dictionaryValue == null) continue;
if (writeKeyFn == null)
{
var keyType = key.GetType();
writeKeyFn = Serializer.GetWriteFn(keyType);
}
if (writeValueFn == null)
writeValueFn = Serializer.GetWriteFn(dictionaryValue.GetType());
if (ranOnce)
writer.Write("&");
else
ranOnce = true;
JsState.WritingKeyCount++;
try
{
JsState.IsWritingValue = false;
writeKeyFn(writer, key);
}
finally
{
JsState.WritingKeyCount--;
}
writer.Write("=");
JsState.IsWritingValue = true;
try
{
writeValueFn(writer, dictionaryValue);
}
finally
{
JsState.IsWritingValue = false;
}
}
}
finally
{
JsState.QueryStringMode = false;
}
}
}
public delegate bool WriteComplexTypeDelegate(TextWriter writer, string propertyName, object obj);
internal class PropertyTypeConfig
{
public TypeConfig TypeConfig;
public string[] PropertyNames;
public Action<string, TextWriter, object> WriteFn;
}
internal class PropertyTypeConfig<T>
{
public static PropertyTypeConfig Config;
static PropertyTypeConfig()
{
Config = new PropertyTypeConfig
{
TypeConfig = TypeConfig<T>.GetState(),
WriteFn = WriteType<T, JsvTypeSerializer>.WriteComplexQueryStringProperties,
};
}
}
public static class QueryStringStrategy
{
static readonly ConcurrentDictionary<Type, PropertyTypeConfig> typeConfigCache =
new ConcurrentDictionary<Type, PropertyTypeConfig>();
public static bool FormUrlEncoded(TextWriter writer, string propertyName, object obj)
{
var map = obj as IDictionary;
if (map != null)
{
foreach (var key in map.Keys)
{
var value = map[key];
writer.Write(propertyName);
writer.Write('[');
writer.Write(key.ToString());
writer.Write("]=");
if (value == null)
{
writer.Write(JsonUtils.Null);
}
else
{
var writeFn = JsvWriter.GetWriteFn(value.GetType());
writeFn(writer, value);
}
}
return true;
}
var typeConfig = typeConfigCache.GetOrAdd(obj.GetType(), t =>
{
var genericType = typeof(PropertyTypeConfig<>).MakeGenericType(t);
var fi = genericType.Fields().First(x => x.Name == "Config"); ;
var config = (PropertyTypeConfig)fi.GetValue(null);
return config;
});
typeConfig.WriteFn(propertyName, writer, obj);
return true;
}
}
}