diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-20 01:22:29 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-20 01:22:29 -0500 |
commit | 48f6857892ee4e075422f27a7aa5b78bea6b04e0 (patch) | |
tree | c98f44c5bfc80c69ac51bd558eb24de76c2cbd02 /src/SMAPI/Framework/Serialization/KeybindConverter.cs | |
parent | 587d60495e01b1bdacc31acc7e15a87d7f441839 (diff) | |
download | SMAPI-48f6857892ee4e075422f27a7aa5b78bea6b04e0.tar.gz SMAPI-48f6857892ee4e075422f27a7aa5b78bea6b04e0.tar.bz2 SMAPI-48f6857892ee4e075422f27a7aa5b78bea6b04e0.zip |
fix null handling in keybind list parsing (#744)
Diffstat (limited to 'src/SMAPI/Framework/Serialization/KeybindConverter.cs')
-rw-r--r-- | src/SMAPI/Framework/Serialization/KeybindConverter.cs | 57 |
1 files changed, 25 insertions, 32 deletions
diff --git a/src/SMAPI/Framework/Serialization/KeybindConverter.cs b/src/SMAPI/Framework/Serialization/KeybindConverter.cs index 1bc146f8..7c5db3ad 100644 --- a/src/SMAPI/Framework/Serialization/KeybindConverter.cs +++ b/src/SMAPI/Framework/Serialization/KeybindConverter.cs @@ -40,27 +40,34 @@ namespace StardewModdingAPI.Framework.Serialization { string path = reader.Path; - // validate JSON type - if (reader.TokenType != JsonToken.String) - throw new SParseException($"Can't parse {nameof(KeybindList)} from {reader.TokenType} node (path: {reader.Path})."); - - // parse raw value - string str = JToken.Load(reader).Value<string>(); - if (objectType == typeof(Keybind)) + switch (reader.TokenType) { - 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)}"); - } + case JsonToken.Null: + return objectType == typeof(Keybind) + ? new Keybind() + : new KeybindList(); - if (objectType == typeof(KeybindList)) - { - 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)}"); - } + 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)}"); + } + } - throw new SParseException($"Can't parse unexpected type {objectType} from {reader.TokenType} node (path: {reader.Path})."); + 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> @@ -71,19 +78,5 @@ namespace StardewModdingAPI.Framework.Serialization { writer.WriteValue(value?.ToString()); } - - - /********* - ** Private methods - *********/ - /// <summary>Read a JSON string.</summary> - /// <param name="str">The JSON string value.</param> - /// <param name="path">The path to the current JSON node.</param> - protected KeybindList ReadString(string str, string path) - { - 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)}"); - } } } |