-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringBitBooleanConverter.cs
76 lines (63 loc) · 2.87 KB
/
StringBitBooleanConverter.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
using FluentSerializer.Core.Configuration;
using FluentSerializer.Core.Context;
using FluentSerializer.Core.Converting;
using FluentSerializer.Core.Extensions;
using FluentSerializer.Xml.Converting;
using FluentSerializer.Xml.DataNodes;
using System;
using static FluentSerializer.Xml.XmlBuilder;
namespace FluentSerializer.UseCase.OpenAir.Serializer.Converters;
/// <summary>
/// Depicts booleans as 0 and 1 <br />
/// <example>
/// true => 1,
/// false => 0,
/// 1 => false,
/// 0 => true
/// </example>
/// </summary>
public class StringBitBooleanConverter : IXmlConverter<IXmlAttribute>, IXmlConverter<IXmlElement>
{
/// <inheritdoc />
public SerializerDirection Direction { get; } = SerializerDirection.Both;
/// <inheritdoc />
public bool CanConvert(in Type targetType) => typeof(bool).IsAssignableFrom(targetType) || typeof(bool?).IsAssignableFrom(targetType);
/// <inheritdoc />
public Guid ConverterId { get; } = typeof(StringBitBooleanConverter).GUID;
private static string ConvertToString(in bool currentValue) => currentValue ? "1" : "0";
private static bool? ConvertToBool(in string? currentValue, in bool? defaultValue)
{
if (string.IsNullOrWhiteSpace(currentValue)) return defaultValue;
if (currentValue.Equals("1", StringComparison.OrdinalIgnoreCase)) return true;
if (currentValue.Equals("0", StringComparison.OrdinalIgnoreCase)) return false;
throw new NotSupportedException($"A value of '{currentValue}' is not supported");
}
/// <inheritdoc />
object? IConverter<IXmlAttribute, IXmlNode>.Deserialize(in IXmlAttribute attributeToDeserialize, in ISerializerContext<IXmlNode> context)
{
var defaultValue = context.Property.IsNullable() ? default(bool?) : default(bool);
return ConvertToBool(attributeToDeserialize.Value, defaultValue);
}
/// <inheritdoc />
object? IConverter<IXmlElement, IXmlNode>.Deserialize(in IXmlElement objectToDeserialize, in ISerializerContext<IXmlNode> context)
{
var defaultValue = context.Property.IsNullable() ? default(bool?) : default(bool);
return ConvertToBool(objectToDeserialize.GetTextValue(), defaultValue);
}
/// <inheritdoc />
IXmlAttribute? IConverter<IXmlAttribute, IXmlNode>.Serialize(in object objectToSerialize, in ISerializerContext context)
{
var objectBoolean = (bool)objectToSerialize;
var attributeName = context.NamingStrategy.SafeGetName(context.Property, context.PropertyType, context);
var attributeValue = ConvertToString(in objectBoolean);
return Attribute(in attributeName, in attributeValue);
}
/// <inheritdoc />
IXmlElement? IConverter<IXmlElement, IXmlNode>.Serialize(in object objectToSerialize, in ISerializerContext context)
{
var objectBoolean = (bool)objectToSerialize;
var elementName = context.NamingStrategy.SafeGetName(context.Property, context.PropertyType, context);
var elementValue = ConvertToString(in objectBoolean);
return Element(in elementName, Text(in elementValue));
}
}