diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-22 21:05:04 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-22 21:05:04 -0500 |
commit | d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6 (patch) | |
tree | 02896d970c7650d8f7c8b84f54e53eddab88b4ca /src/SMAPI/Framework/Serialization | |
parent | 5953fc3bd083ae0a579d2da1ad833e6163848086 (diff) | |
parent | 733750fdc4f5d16069d95880144619c0e31e8a89 (diff) | |
download | SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.tar.gz SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.tar.bz2 SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Serialization')
-rw-r--r-- | src/SMAPI/Framework/Serialization/KeybindConverter.cs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Serialization/KeybindConverter.cs b/src/SMAPI/Framework/Serialization/KeybindConverter.cs new file mode 100644 index 00000000..93a274a8 --- /dev/null +++ b/src/SMAPI/Framework/Serialization/KeybindConverter.cs @@ -0,0 +1,82 @@ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using StardewModdingAPI.Toolkit.Serialization; +using StardewModdingAPI.Utilities; + +namespace StardewModdingAPI.Framework.Serialization +{ + /// <summary>Handles deserialization of <see cref="Keybind"/> and <see cref="KeybindList"/> models.</summary> + internal class KeybindConverter : JsonConverter + { + /********* + ** Accessors + *********/ + /// <inheritdoc /> + public override bool CanRead { get; } = true; + + /// <inheritdoc /> + public override bool CanWrite { get; } = true; + + + /********* + ** Public methods + *********/ + /// <summary>Get whether this instance can convert the specified object type.</summary> + /// <param name="objectType">The object type.</param> + public override bool CanConvert(Type objectType) + { + return + typeof(Keybind).IsAssignableFrom(objectType) + || typeof(KeybindList).IsAssignableFrom(objectType); + } + + /// <summary>Reads the JSON representation of the object.</summary> + /// <param name="reader">The JSON reader.</param> + /// <param name="objectType">The object type.</param> + /// <param name="existingValue">The object being read.</param> + /// <param name="serializer">The calling serializer.</param> + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + string path = reader.Path; + + switch (reader.TokenType) + { + case JsonToken.Null: + return objectType == typeof(Keybind) + ? (object)new Keybind() + : new KeybindList(); + + case JsonToken.String: + { + string str = JToken.Load(reader).Value<string>(); + + if (objectType == typeof(Keybind)) + { + return Keybind.TryParse(str, out Keybind parsed, out string[] errors) + ? parsed + : throw new SParseException($"Can't parse {nameof(Keybind)} from invalid value '{str}' (path: {path}).\n{string.Join("\n", errors)}"); + } + else + { + return KeybindList.TryParse(str, out KeybindList parsed, out string[] errors) + ? parsed + : throw new SParseException($"Can't parse {nameof(KeybindList)} from invalid value '{str}' (path: {path}).\n{string.Join("\n", errors)}"); + } + } + + default: + throw new SParseException($"Can't parse {objectType} from unexpected {reader.TokenType} node (path: {reader.Path})."); + } + } + + /// <summary>Writes the JSON representation of the object.</summary> + /// <param name="writer">The JSON writer.</param> + /// <param name="value">The value.</param> + /// <param name="serializer">The calling serializer.</param> + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + writer.WriteValue(value?.ToString()); + } + } +} |