|
@ -178,91 +178,91 @@ namespace IPA.Config.Stores.Converters |
|
|
public NullableConverter() : base(new TConverter()) { } |
|
|
public NullableConverter() : base(new TConverter()) { } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back.
|
|
|
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T">the enum type</typeparam>
|
|
|
/// <typeparam name="T">the enum type</typeparam>
|
|
|
public sealed class EnumConverter<T> : ValueConverter<T> |
|
|
public sealed class EnumConverter<T> : ValueConverter<T> |
|
|
where T : Enum |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">the <see cref="Value"/> to convert</param>
|
|
|
|
|
|
/// <param name="parent">the object which will own the created object</param>
|
|
|
|
|
|
/// <returns>the deserialized enum value</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
|
|
|
|
|
|
public override T FromValue(Value value, object parent) |
|
|
|
|
|
=> value is Text t |
|
|
|
|
|
? (T)Enum.Parse(typeof(T), t.Value) |
|
|
|
|
|
: throw new ArgumentException("Value not a string", nameof(value)); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">the value to serialize</param>
|
|
|
|
|
|
/// <param name="parent">the object which owns <paramref name="obj"/></param>
|
|
|
|
|
|
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
|
|
|
|
|
|
public override Value ToValue(T obj, object parent) |
|
|
|
|
|
=> Value.Text(obj.ToString()); |
|
|
|
|
|
|
|
|
where T : Enum |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">the <see cref="Value"/> to convert</param>
|
|
|
|
|
|
/// <param name="parent">the object which will own the created object</param>
|
|
|
|
|
|
/// <returns>the deserialized enum value</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
|
|
|
|
|
|
public override T FromValue(Value value, object parent) |
|
|
|
|
|
=> value is Text t |
|
|
|
|
|
? (T)Enum.Parse(typeof(T), t.Value) |
|
|
|
|
|
: throw new ArgumentException("Value not a string", nameof(value)); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">the value to serialize</param>
|
|
|
|
|
|
/// <param name="parent">the object which owns <paramref name="obj"/></param>
|
|
|
|
|
|
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
|
|
|
|
|
|
public override Value ToValue(T obj, object parent) |
|
|
|
|
|
=> Value.Text(obj.ToString()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back,
|
|
|
|
|
|
/// ignoring the case of the serialized value for deseiralization.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back,
|
|
|
|
|
|
/// ignoring the case of the serialized value for deseiralization.
|
|
|
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T">the enum type</typeparam>
|
|
|
/// <typeparam name="T">the enum type</typeparam>
|
|
|
public sealed class CaseInsensitiveEnumConverter<T> : ValueConverter<T> |
|
|
public sealed class CaseInsensitiveEnumConverter<T> : ValueConverter<T> |
|
|
where T : Enum |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">the <see cref="Value"/> to convert</param>
|
|
|
|
|
|
/// <param name="parent">the object which will own the created object</param>
|
|
|
|
|
|
/// <returns>the deserialized enum value</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
|
|
|
|
|
|
public override T FromValue(Value value, object parent) |
|
|
|
|
|
=> value is Text t |
|
|
|
|
|
? (T)Enum.Parse(typeof(T), t.Value, true) |
|
|
|
|
|
: throw new ArgumentException("Value not a string", nameof(value)); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">the value to serialize</param>
|
|
|
|
|
|
/// <param name="parent">the object which owns <paramref name="obj"/></param>
|
|
|
|
|
|
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
|
|
|
|
|
|
public override Value ToValue(T obj, object parent) |
|
|
|
|
|
=> Value.Text(obj.ToString()); |
|
|
|
|
|
|
|
|
where T : Enum |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">the <see cref="Value"/> to convert</param>
|
|
|
|
|
|
/// <param name="parent">the object which will own the created object</param>
|
|
|
|
|
|
/// <returns>the deserialized enum value</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
|
|
|
|
|
|
public override T FromValue(Value value, object parent) |
|
|
|
|
|
=> value is Text t |
|
|
|
|
|
? (T)Enum.Parse(typeof(T), t.Value, true) |
|
|
|
|
|
: throw new ArgumentException("Value not a string", nameof(value)); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">the value to serialize</param>
|
|
|
|
|
|
/// <param name="parent">the object which owns <paramref name="obj"/></param>
|
|
|
|
|
|
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
|
|
|
|
|
|
public override Value ToValue(T obj, object parent) |
|
|
|
|
|
=> Value.Text(obj.ToString()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its underlying value for serialization.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its underlying value for serialization.
|
|
|
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T">the enum type</typeparam>
|
|
|
/// <typeparam name="T">the enum type</typeparam>
|
|
|
public sealed class NumericEnumConverter<T> : ValueConverter<T> |
|
|
public sealed class NumericEnumConverter<T> : ValueConverter<T> |
|
|
where T : Enum |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a <see cref="Value"/> that is a numeric node to the corresponding enum value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">the <see cref="Value"/> to convert</param>
|
|
|
|
|
|
/// <param name="parent">the object which will own the created object</param>
|
|
|
|
|
|
/// <returns>the deserialized enum value</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a numeric node</exception>
|
|
|
|
|
|
public override T FromValue(Value value, object parent) |
|
|
|
|
|
=> (T)Enum.ToObject(typeof(T), Converter.IntValue(value) |
|
|
|
|
|
?? throw new ArgumentException("Value not a numeric node", nameof(value))); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">the value to serialize</param>
|
|
|
|
|
|
/// <param name="parent">the object which owns <paramref name="obj"/></param>
|
|
|
|
|
|
/// <returns>an <see cref="Integer"/> node representing <paramref name="obj"/></returns>
|
|
|
|
|
|
public override Value ToValue(T obj, object parent) |
|
|
|
|
|
=> Value.Integer(Convert.ToInt64(obj)); |
|
|
|
|
|
|
|
|
where T : Enum |
|
|
|
|
|
{ |
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts a <see cref="Value"/> that is a numeric node to the corresponding enum value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value">the <see cref="Value"/> to convert</param>
|
|
|
|
|
|
/// <param name="parent">the object which will own the created object</param>
|
|
|
|
|
|
/// <returns>the deserialized enum value</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a numeric node</exception>
|
|
|
|
|
|
public override T FromValue(Value value, object parent) |
|
|
|
|
|
=> (T)Enum.ToObject(typeof(T), Converter.IntValue(value) |
|
|
|
|
|
?? throw new ArgumentException("Value not a numeric node", nameof(value))); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">the value to serialize</param>
|
|
|
|
|
|
/// <param name="parent">the object which owns <paramref name="obj"/></param>
|
|
|
|
|
|
/// <returns>an <see cref="Integer"/> node representing <paramref name="obj"/></returns>
|
|
|
|
|
|
public override Value ToValue(T obj, object parent) |
|
|
|
|
|
=> Value.Integer(Convert.ToInt64(obj)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
internal class StringConverter : ValueConverter<string> |
|
|
internal class StringConverter : ValueConverter<string> |
|
|