summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
blob: 37108556d3ca5dc9d6286bddeaab8a3d1b37a291 (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Converters;

namespace StardewModdingAPI.Framework.Serialisation
{
    /// <summary>A variant of <see cref="StringEnumConverter"/> which only converts certain enums.</summary>
    internal class SelectiveStringEnumConverter : StringEnumConverter
    {
        /*********
        ** Properties
        *********/
        /// <summary>The enum type names to convert.</summary>
        private readonly HashSet<string> Types;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="types">The enum types to convert.</param>
        public SelectiveStringEnumConverter(params Type[] types)
        {
            this.Types = new HashSet<string>(types.Select(p => p.FullName));
        }

        /// <summary>Get whether this instance can convert the specified object type.</summary>
        /// <param name="type">The object type.</param>
        public override bool CanConvert(Type type)
        {
            return
                base.CanConvert(type)
                && this.Types.Contains((Nullable.GetUnderlyingType(type) ?? type).FullName);
        }
    }
}