-
-
Notifications
You must be signed in to change notification settings - Fork 461
/
Deserializer.cs
334 lines (289 loc) · 14.1 KB
/
Deserializer.cs
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization.Converters;
using YamlDotNet.Serialization.NamingConventions;
using YamlDotNet.Serialization.NodeDeserializers;
using YamlDotNet.Serialization.NodeTypeResolvers;
using YamlDotNet.Serialization.ObjectFactories;
using YamlDotNet.Serialization.TypeInspectors;
using YamlDotNet.Serialization.TypeResolvers;
using YamlDotNet.Serialization.Utilities;
using YamlDotNet.Serialization.ValueDeserializers;
namespace YamlDotNet.Serialization
{
/// <summary>
/// Deserializes objects from the YAML format.
/// To customize the behavior of <see cref="Deserializer" />,
/// use the <see cref="DeserializerBuilder" /> class.
/// </summary>
public sealed class Deserializer
{
#region Backwards compatibility
private class BackwardsCompatibleConfiguration
{
private static readonly Dictionary<string, Type> predefinedTagMappings = new Dictionary<string, Type>
{
{ "tag:yaml.org,2002:map", typeof(Dictionary<object, object>) },
{ "tag:yaml.org,2002:bool", typeof(bool) },
{ "tag:yaml.org,2002:float", typeof(double) },
{ "tag:yaml.org,2002:int", typeof(int) },
{ "tag:yaml.org,2002:str", typeof(string) },
{ "tag:yaml.org,2002:timestamp", typeof(DateTime) }
};
private readonly Dictionary<string, Type> tagMappings;
private readonly List<IYamlTypeConverter> converters;
private TypeDescriptorProxy typeDescriptor = new TypeDescriptorProxy();
public IValueDeserializer valueDeserializer;
public IList<INodeDeserializer> NodeDeserializers { get; private set; }
public IList<INodeTypeResolver> TypeResolvers { get; private set; }
private class TypeDescriptorProxy : ITypeInspector
{
public ITypeInspector TypeDescriptor;
public IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
{
return TypeDescriptor.GetProperties(type, container);
}
public IPropertyDescriptor GetProperty(Type type, object container, string name, bool ignoreUnmatched)
{
return TypeDescriptor.GetProperty(type, container, name, ignoreUnmatched);
}
}
public BackwardsCompatibleConfiguration(
IObjectFactory objectFactory,
INamingConvention namingConvention,
bool ignoreUnmatched,
YamlAttributeOverrides overrides)
{
objectFactory = objectFactory ?? new DefaultObjectFactory();
namingConvention = namingConvention ?? new NullNamingConvention();
typeDescriptor.TypeDescriptor =
new CachedTypeInspector(
new NamingConventionTypeInspector(
new YamlAttributesTypeInspector(
new YamlAttributeOverridesInspector(
new ReadableAndWritablePropertiesTypeInspector(
new ReadablePropertiesTypeInspector(
new StaticTypeResolver()
)
),
overrides
)
),
namingConvention
)
);
converters = new List<IYamlTypeConverter>();
converters.Add(new GuidConverter(false));
converters.Add(new SystemTypeConverter());
NodeDeserializers = new List<INodeDeserializer>();
NodeDeserializers.Add(new YamlConvertibleNodeDeserializer(objectFactory));
NodeDeserializers.Add(new YamlSerializableNodeDeserializer(objectFactory));
NodeDeserializers.Add(new TypeConverterNodeDeserializer(converters));
NodeDeserializers.Add(new NullNodeDeserializer());
NodeDeserializers.Add(new ScalarNodeDeserializer());
NodeDeserializers.Add(new ArrayNodeDeserializer());
NodeDeserializers.Add(new DictionaryNodeDeserializer(objectFactory));
NodeDeserializers.Add(new CollectionNodeDeserializer(objectFactory));
NodeDeserializers.Add(new EnumerableNodeDeserializer());
NodeDeserializers.Add(new ObjectNodeDeserializer(objectFactory, typeDescriptor, ignoreUnmatched));
tagMappings = new Dictionary<string, Type>(predefinedTagMappings);
TypeResolvers = new List<INodeTypeResolver>();
TypeResolvers.Add(new YamlConvertibleTypeResolver());
TypeResolvers.Add(new YamlSerializableTypeResolver());
TypeResolvers.Add(new TagNodeTypeResolver(tagMappings));
TypeResolvers.Add(new TypeNameInTagNodeTypeResolver());
TypeResolvers.Add(new DefaultContainersNodeTypeResolver());
valueDeserializer =
new AliasValueDeserializer(
new NodeValueDeserializer(
NodeDeserializers,
TypeResolvers
)
);
}
public void RegisterTagMapping(string tag, Type type)
{
tagMappings.Add(tag, type);
}
public void RegisterTypeConverter(IYamlTypeConverter typeConverter)
{
converters.Insert(0, typeConverter);
}
}
private readonly BackwardsCompatibleConfiguration backwardsCompatibleConfiguration;
private void ThrowUnlessInBackwardsCompatibleMode()
{
if (backwardsCompatibleConfiguration == null)
{
throw new InvalidOperationException("This method / property exists for backwards compatibility reasons, but the Deserializer was created using the new configuration mechanism. To configure the Deserializer, use the DeserializerBuilder.");
}
}
[Obsolete("Please use DeserializerBuilder to customize the Deserializer. This property will be removed in future releases.")]
public IList<INodeDeserializer> NodeDeserializers
{
get
{
ThrowUnlessInBackwardsCompatibleMode();
return backwardsCompatibleConfiguration.NodeDeserializers;
}
}
[Obsolete("Please use DeserializerBuilder to customize the Deserializer. This property will be removed in future releases.")]
public IList<INodeTypeResolver> TypeResolvers
{
get
{
ThrowUnlessInBackwardsCompatibleMode();
return backwardsCompatibleConfiguration.TypeResolvers;
}
}
[Obsolete("Please use DeserializerBuilder to customize the Deserializer. This constructor will be removed in future releases.")]
public Deserializer(
IObjectFactory objectFactory = null,
INamingConvention namingConvention = null,
bool ignoreUnmatched = false,
YamlAttributeOverrides overrides = null)
{
backwardsCompatibleConfiguration = new BackwardsCompatibleConfiguration(objectFactory, namingConvention, ignoreUnmatched, overrides);
valueDeserializer = backwardsCompatibleConfiguration.valueDeserializer;
}
[Obsolete("Please use DeserializerBuilder to customize the Deserializer. This method will be removed in future releases.")]
public void RegisterTagMapping(string tag, Type type)
{
ThrowUnlessInBackwardsCompatibleMode();
backwardsCompatibleConfiguration.RegisterTagMapping(tag, type);
}
[Obsolete("Please use DeserializerBuilder to customize the Deserializer. This method will be removed in future releases.")]
public void RegisterTypeConverter(IYamlTypeConverter typeConverter)
{
ThrowUnlessInBackwardsCompatibleMode();
backwardsCompatibleConfiguration.RegisterTypeConverter(typeConverter);
}
#endregion
private readonly IValueDeserializer valueDeserializer;
/// <summary>
/// Initializes a new instance of <see cref="Deserializer" /> using the default configuration.
/// </summary>
/// <remarks>
/// To customize the bahavior of the deserializer, use <see cref="DeserializerBuilder" />.
/// </remarks>
public Deserializer()
// TODO: When the backwards compatibility is dropped, uncomment the following line and remove the body of this constructor.
// : this(new DeserializerBuilder().BuildValueDeserializer())
{
backwardsCompatibleConfiguration = new BackwardsCompatibleConfiguration(null, null, false, null);
valueDeserializer = backwardsCompatibleConfiguration.valueDeserializer;
}
/// <remarks>
/// This constructor is private to discourage its use.
/// To invoke it, call the <see cref="FromValueDeserializer"/> method.
/// </remarks>
private Deserializer(IValueDeserializer valueDeserializer)
{
if (valueDeserializer == null)
{
throw new ArgumentNullException("valueDeserializer");
}
this.valueDeserializer = valueDeserializer;
}
/// <summary>
/// Creates a new <see cref="Deserializer" /> that uses the specified <see cref="IValueDeserializer" />.
/// This method is available for advanced scenarios. The preferred way to customize the bahavior of the
/// deserializer is to use <see cref="DeserializerBuilder" />.
/// </summary>
public static Deserializer FromValueDeserializer(IValueDeserializer valueDeserializer)
{
return new Deserializer(valueDeserializer);
}
public T Deserialize<T>(string input)
{
using (var reader = new StringReader(input))
{
return (T)Deserialize(reader, typeof(T));
}
}
public T Deserialize<T>(TextReader input)
{
return (T)Deserialize(input, typeof(T));
}
public object Deserialize(TextReader input)
{
return Deserialize(input, typeof(object));
}
public object Deserialize(string input, Type type)
{
using (var reader = new StringReader(input))
{
return Deserialize(reader, type);
}
}
public object Deserialize(TextReader input, Type type)
{
return Deserialize(new Parser(input), type);
}
public T Deserialize<T>(IParser parser)
{
return (T)Deserialize(parser, typeof(T));
}
public object Deserialize(IParser parser)
{
return Deserialize(parser, typeof(object));
}
/// <summary>
/// Deserializes an object of the specified type.
/// </summary>
/// <param name="parser">The <see cref="IParser" /> from where to deserialize the object.</param>
/// <param name="type">The static type of the object to deserialize.</param>
/// <returns>Returns the deserialized object.</returns>
public object Deserialize(IParser parser, Type type)
{
if (parser == null)
{
throw new ArgumentNullException("reader");
}
if (type == null)
{
throw new ArgumentNullException("type");
}
var hasStreamStart = parser.Allow<StreamStart>() != null;
var hasDocumentStart = parser.Allow<DocumentStart>() != null;
object result = null;
if (!parser.Accept<DocumentEnd>() && !parser.Accept<StreamEnd>())
{
using (var state = new SerializerState())
{
result = valueDeserializer.DeserializeValue(parser, type, state, valueDeserializer);
state.OnDeserialization();
}
}
if (hasDocumentStart)
{
parser.Expect<DocumentEnd>();
}
if (hasStreamStart)
{
parser.Expect<StreamEnd>();
}
return result;
}
}
}