-
Notifications
You must be signed in to change notification settings - Fork 3
/
YamlConverter.cs
99 lines (86 loc) · 4.13 KB
/
YamlConverter.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
using System;
using Cuemon.Extensions.YamlDotNet.Formatters;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
namespace Cuemon.Extensions.YamlDotNet.Converters
{
/// <summary>
/// Converts an object to and from YAML (YAML ain't markup language).
/// </summary>
public abstract class YamlConverter : IYamlTypeConverter
{
internal abstract void WriteYamlCore(IEmitter writer, object value);
internal abstract object ReadYamlCore(IParser reader, Type typeToConvert);
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="typeToConvert">The <seealso cref="Type"/> of the object.</param>
/// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns>
public abstract bool CanConvert(Type typeToConvert);
/// <summary>
/// Gets or sets the <see cref="YamlFormatterOptions"/> associated with this instance. Normally this is done from <see cref="Formatters.YamlFormatter"/>
/// </summary>
/// <value>The <see cref="YamlFormatterOptions"/> associated with this instance.</value>
public YamlFormatterOptions FormatterOptions { get; set; }
/// <summary>
/// Returns the specified <paramref name="name"/> adhering to the underlying <see cref="YamlSerializerOptions.NamingConvention"/> policy on <see cref="FormatterOptions"/>.
/// </summary>
/// <param name="name">The name of the property.</param>
/// <returns>If <see cref="FormatterOptions"/> is null, the specified <paramref name="name"/> is returned unaltered; otherwise it is converted according to the <see cref="YamlSerializerOptions.NamingConvention"/>.</returns>
public string SetPropertyName(string name)
{
Validator.ThrowIfNull(name);
return FormatterOptions?.Settings.NamingConvention.Apply(name) ?? name;
}
bool IYamlTypeConverter.Accepts(Type type)
{
return CanConvert(type);
}
object IYamlTypeConverter.ReadYaml(IParser parser, Type type)
{
return ReadYamlCore(parser, type);
}
void IYamlTypeConverter.WriteYaml(IEmitter emitter, object value, Type type) // odd decision with type parameter
{
WriteYamlCore(emitter, value);
}
}
/// <summary>
/// Converts an object to or from YAML (YAML ain't markup language).
/// </summary>
/// <typeparam name="T">The type of object or value handled by the converter.</typeparam>
/// <seealso cref="YamlConverter" />
public abstract class YamlConverter<T> : YamlConverter
{
/// <summary>
/// Writes a specified <paramref name="value"/> as YAML.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="value">The value to convert to YAML.</param>
public abstract void WriteYaml(IEmitter writer, T value);
/// <summary>
/// Reads and converts the YAML to type <typeparamref name="T"/>.
/// </summary>
/// <param name="reader">The reader to read from.</param>
/// <param name="typeToConvert">The type to convert.</param>
/// <returns>The converted value.</returns>
public abstract T ReadYaml(IParser reader, Type typeToConvert);
internal override object ReadYamlCore(IParser reader, Type typeToConvert)
{
return ReadYaml(reader, typeToConvert);
}
internal override void WriteYamlCore(IEmitter writer, object value)
{
WriteYaml(writer, (T)value);
}
/// <summary>
/// Determines whether this instance can convert the specified object type.
/// </summary>
/// <param name="typeToConvert">The <seealso cref="Type" /> of the object.</param>
/// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns>
public override bool CanConvert(Type typeToConvert)
{
return typeof(T).IsAssignableFrom(typeToConvert);
}
}
}