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
155 lines (124 loc) · 6.3 KB
/
Copy pathJsReader.cs
File metadata and controls
155 lines (124 loc) · 6.3 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
#if NETSTANDARD1_1
using Microsoft.Extensions.Primitives;
#else
using ServiceStack.Text.Support;
#endif
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 ParseStringSegmentDelegate GetParseStringSegmentFn<T>()
{
var onDeserializedFn = JsConfig<T>.OnDeserializedFn;
if (onDeserializedFn != null)
{
var parseFn = GetCoreParseStringSegmentFn<T>();
return value => onDeserializedFn((T)parseFn(value));
}
return GetCoreParseStringSegmentFn<T>();
}
private ParseStringDelegate GetCoreParseFn<T>()
{
return v => GetCoreParseStringSegmentFn<T>()(new StringSegment(v));
}
private ParseStringSegmentDelegate GetCoreParseStringSegmentFn<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 v => Serializer.UnescapeString(v).Value;
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>.ParseStringSegment;
}
var builtInMethod = DeserializeBuiltin<T>.ParseStringSegment;
if (builtInMethod != null)
return value => builtInMethod(Serializer.UnescapeSafeString(value));
if (type.HasGenericType())
{
if (type.IsOrHasGenericInterfaceTypeOf(typeof(IList<>)))
return DeserializeList<T, TSerializer>.ParseStringSegment;
if (type.IsOrHasGenericInterfaceTypeOf(typeof(IDictionary<,>)))
return DeserializeDictionary<TSerializer>.GetParseStringSegmentMethod(type);
if (type.IsOrHasGenericInterfaceTypeOf(typeof(ICollection<>)))
return DeserializeCollection<TSerializer>.GetParseStringSegmentMethod(type);
if (type.HasAnyTypeDefinitionsOf(typeof(Queue<>))
|| type.HasAnyTypeDefinitionsOf(typeof(Stack<>)))
return DeserializeSpecializedCollections<T, TSerializer>.ParseStringSegment;
if (type.IsOrHasGenericInterfaceTypeOf(typeof(KeyValuePair<,>)))
return DeserializeKeyValuePair<TSerializer>.GetParseStringSegmentMethod(type);
if (type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable<>)))
return DeserializeEnumerable<T, TSerializer>.ParseStringSegment;
var customFn = DeserializeCustomGenericType<TSerializer>.GetParseStringSegmentMethod(type);
if (customFn != null)
return customFn;
}
var pclParseFn = PclExport.Instance.GetJsReaderParseStringSegmentMethod<TSerializer>(typeof(T));
if (pclParseFn != null)
return pclParseFn;
var isDictionary = typeof(T) != typeof(IEnumerable) && typeof(T) != typeof(ICollection)
&& (typeof(T).AssignableFrom(typeof(IDictionary)) || typeof(T).HasInterface(typeof(IDictionary)));
if (isDictionary)
{
return DeserializeDictionary<TSerializer>.GetParseStringSegmentMethod(type);
}
var isEnumerable = typeof(T).AssignableFrom(typeof(IEnumerable))
|| typeof(T).HasInterface(typeof(IEnumerable));
if (isEnumerable)
{
var parseFn = DeserializeSpecializedCollections<T, TSerializer>.ParseStringSegment;
if (parseFn != null) return parseFn;
}
if (type.IsValueType())
{
//at first try to find more faster `ParseStringSegment` method
var staticParseStringSegmentMethod = StaticParseMethod<T>.ParseStringSegment;
if (staticParseStringSegmentMethod != null)
return value => staticParseStringSegmentMethod(Serializer.UnescapeSafeString(value));
//then try to find `Parse` method
var staticParseMethod = StaticParseMethod<T>.Parse;
if (staticParseMethod != null)
return value => staticParseMethod(Serializer.UnescapeSafeString(value).Value);
}
else
{
var staticParseStringSegmentMethod = StaticParseRefTypeMethod<TSerializer, T>.ParseStringSegment;
if (staticParseStringSegmentMethod != null)
return value => staticParseStringSegmentMethod(Serializer.UnescapeSafeString(value));
var staticParseMethod = StaticParseRefTypeMethod<TSerializer, T>.Parse;
if (staticParseMethod != null)
return value => staticParseMethod(Serializer.UnescapeSafeString(value).Value);
}
var typeConstructor = DeserializeType<TSerializer>.GetParseStringSegmentMethod(TypeConfig<T>.GetState());
if (typeConstructor != null)
return typeConstructor;
var stringConstructor = DeserializeTypeUtils.GetParseStringSegmentMethod(type);
if (stringConstructor != null) return stringConstructor;
return DeserializeType<TSerializer>.ParseAbstractType<T>;
}
}
}