using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IPA.Config.Stores.Attributes { /// /// Causes a field or property in an object being wrapped by to be /// ignored during serialization and deserialization. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public sealed class IgnoreAttribute : Attribute { } /// /// Indicates that a field or property in an object being wrapped by /// that would otherwise be nullable (i.e. a reference type or a type) should never be null, and the /// member will be ignored if the deserialized value is . /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public sealed class NonNullableAttribute : Attribute { } /// /// Indicates that a given field or property in an object being wrapped by /// should be serialized and deserialized using the provided converter instead of the default mechanism. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public sealed class UseConverterAttribute : Attribute { /// /// Gets the type of the converter to use. /// public Type ConverterType { get; private set; } /// /// Gets the target type of the converter if it is avaliable at instantiation time, otherwise /// . /// public Type ConverterTargetType => ConverterType.BaseType.IsGenericType ? ConverterType.BaseType.GetGenericArguments()[0] : null; /// /// Gets whether or not this converter is a generic . /// public bool IsGenericConverter => ConverterTargetType != null; /// /// Creates a new with a given . /// /// tpy type to assign to public UseConverterAttribute(Type converterType) { ConverterType = converterType; var implInterface = ConverterType.GetInterfaces().Contains(typeof(IValueConverter)); var inheritGeneric = ConverterType.BaseType.IsGenericType && ConverterType.BaseType.GetGenericTypeDefinition() == typeof(ValueConverter<>); if (!implInterface && !inheritGeneric) throw new ArgumentException("Type is not a value converter!"); } } /// /// Specifies a name for the serialized field or property in an object being wrapped by /// that is different from the member name itself. /// /// /// /// When serializing the following object, we might get the JSON that follows. /// /// public class PluginConfig /// { /// public virtual bool BooleanField { get; set; } = true; /// } /// /// /// { /// "BooleanField": true /// } /// /// /// /// However, if we were to add a to that field, we would get the following. /// /// public class PluginConfig /// { /// [SerializedName("bool")] /// public virtual bool BooleanField { get; set; } = true; /// } /// /// /// { /// "bool": true /// } /// /// /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public sealed class SerializedNameAttribute : Attribute { /// /// Gets the name to replace the member name with. /// public string Name { get; private set; } /// /// Creates a new with the given . /// /// the value to assign to public SerializedNameAttribute(string name) { Name = name; } } }