summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-02-17 11:33:22 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-02-17 11:33:22 -0500
commit41ee8990f81a63c686953b2c28e7af8627fd8098 (patch)
treefd3a997f5dcf1b186bd8a4364b40ca86192fac25 /src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
parent1dfedd2d1a032d357bf42e9edbd7a797beb2e124 (diff)
downloadSMAPI-41ee8990f81a63c686953b2c28e7af8627fd8098.tar.gz
SMAPI-41ee8990f81a63c686953b2c28e7af8627fd8098.tar.bz2
SMAPI-41ee8990f81a63c686953b2c28e7af8627fd8098.zip
write XNA input enums to JSON as strings automatically
Mods often reference Json.NET to do this, so this lets many mods remove Json.NET as a dependency.
Diffstat (limited to 'src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs')
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
new file mode 100644
index 00000000..e9c5496d
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Serialisation/SelectiveStringEnumConverter.cs
@@ -0,0 +1,35 @@
+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(type.FullName);
+ }
+ }
+}