forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsReader.cs
More file actions
163 lines (132 loc) · 6.84 KB
/
JsReader.cs
File metadata and controls
163 lines (132 loc) · 6.84 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace ServiceStack.Text.Common
{
public class JsReader<TSerializer>
where TSerializer : ITypeSerializer
{
private static readonly ITypeSerializer Serializer = JsWriter.GetTypeSerializer<TSerializer>();
public ParseStringDelegate GetParseFn<T>()
{
var onDeserializedFn = JsConfig<T>.OnDeserializedFn;
if (onDeserializedFn != null)
{
var parseFn = GetCoreParseFn<T>();
return value => onDeserializedFn((T)parseFn(value));
}
return GetCoreParseFn<T>();
}
public ParseStringSpanDelegate GetParseStringSpanFn<T>()
{
var onDeserializedFn = JsConfig<T>.OnDeserializedFn;
if (onDeserializedFn != null)
{
var parseFn = GetCoreParseStringSpanFn<T>();
return value => onDeserializedFn((T)parseFn(value));
}
return GetCoreParseStringSpanFn<T>();
}
private ParseStringDelegate GetCoreParseFn<T>()
{
return v => GetCoreParseStringSpanFn<T>()(v.AsSpan());
}
private ParseStringSpanDelegate GetCoreParseStringSpanFn<T>()
{
var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
if (JsConfig<T>.HasDeserializeFn)
return value => JsConfig<T>.ParseFn(Serializer, value.Value());
if (type.IsEnum)
return x => ParseUtils.TryParseEnum(type, Serializer.UnescapeSafeString(x).Value());
if (type == typeof(string))
return Serializer.UnescapeStringAsObject;
if (type == typeof(object))
return DeserializeType<TSerializer>.ObjectStringToType;
var specialParseFn = ParseUtils.GetSpecialParseMethod(type);
if (specialParseFn != null)
return v => specialParseFn(v.Value());
if (type.IsArray)
{
return DeserializeArray<T, TSerializer>.ParseStringSpan;
}
var builtInMethod = DeserializeBuiltin<T>.ParseStringSpan;
if (builtInMethod != null)
return value => builtInMethod(Serializer.UnescapeSafeString(value));
if (type.HasGenericType())
{
if (type.IsOrHasGenericInterfaceTypeOf(typeof(IList<>)))
return DeserializeList<T, TSerializer>.ParseStringSpan;
if (type.IsOrHasGenericInterfaceTypeOf(typeof(IDictionary<,>)))
return DeserializeDictionary<TSerializer>.GetParseStringSpanMethod(type);
if (type.IsOrHasGenericInterfaceTypeOf(typeof(ICollection<>)))
return DeserializeCollection<TSerializer>.GetParseStringSpanMethod(type);
if (type.HasAnyTypeDefinitionsOf(typeof(Queue<>))
|| type.HasAnyTypeDefinitionsOf(typeof(Stack<>)))
return DeserializeSpecializedCollections<T, TSerializer>.ParseStringSpan;
if (type.IsOrHasGenericInterfaceTypeOf(typeof(KeyValuePair<,>)))
return DeserializeKeyValuePair<TSerializer>.GetParseStringSpanMethod(type);
if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable<>)))
return DeserializeEnumerable<T, TSerializer>.ParseStringSpan;
var customFn = DeserializeCustomGenericType<TSerializer>.GetParseStringSpanMethod(type);
if (customFn != null)
return customFn;
}
var pclParseFn = PclExport.Instance.GetJsReaderParseStringSpanMethod<TSerializer>(typeof(T));
if (pclParseFn != null)
return pclParseFn;
var isDictionary = typeof(T) != typeof(IEnumerable) && typeof(T) != typeof(ICollection)
&& (typeof(T).IsAssignableFrom(typeof(IDictionary)) || typeof(T).HasInterface(typeof(IDictionary)));
if (isDictionary)
{
return DeserializeDictionary<TSerializer>.GetParseStringSpanMethod(type);
}
var isEnumerable = typeof(T).IsAssignableFrom(typeof(IEnumerable))
|| typeof(T).HasInterface(typeof(IEnumerable));
if (isEnumerable)
{
var parseFn = DeserializeSpecializedCollections<T, TSerializer>.ParseStringSpan;
if (parseFn != null)
return parseFn;
}
if (type.IsValueType)
{
//at first try to find more faster `ParseStringSpan` method
var staticParseStringSpanMethod = StaticParseMethod<T>.ParseStringSpan;
if (staticParseStringSpanMethod != null)
return value => staticParseStringSpanMethod(Serializer.UnescapeSafeString(value));
//then try to find `Parse` method
var staticParseMethod = StaticParseMethod<T>.Parse;
if (staticParseMethod != null)
return value => staticParseMethod(Serializer.UnescapeSafeString(value).ToString());
}
else
{
var staticParseStringSpanMethod = StaticParseRefTypeMethod<TSerializer, T>.ParseStringSpan;
if (staticParseStringSpanMethod != null)
return value => staticParseStringSpanMethod(Serializer.UnescapeSafeString(value));
var staticParseMethod = StaticParseRefTypeMethod<TSerializer, T>.Parse;
if (staticParseMethod != null)
return value => staticParseMethod(Serializer.UnescapeSafeString(value).ToString());
}
var typeConstructor = DeserializeType<TSerializer>.GetParseStringSpanMethod(TypeConfig<T>.GetState());
if (typeConstructor != null)
return typeConstructor;
var stringConstructor = DeserializeTypeUtils.GetParseStringSpanMethod(type);
if (stringConstructor != null)
return stringConstructor;
return DeserializeType<TSerializer>.ParseAbstractType<T>;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void InitAot<T>()
{
var hold = DeserializeBuiltin<T>.Parse;
hold = DeserializeArray<T[], TSerializer>.Parse;
DeserializeType<TSerializer>.ExtractType(default(ReadOnlySpan<char>));
DeserializeArrayWithElements<T, TSerializer>.ParseGenericArray(default(ReadOnlySpan<char>), null);
DeserializeCollection<TSerializer>.ParseCollection<T>(default(ReadOnlySpan<char>), null, null);
DeserializeListWithElements<T, TSerializer>.ParseGenericList(default(ReadOnlySpan<char>), null, null);
}
}
}