diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-13 00:01:14 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-13 00:01:14 -0400 |
commit | 4fccaa35709440f98e275f0da3d2b65204b7d6e2 (patch) | |
tree | a3e2328af666198485fef17fff8f5f269affe989 | |
parent | a9fcbc686d203b20f8e4b127e213b7028b3a9ae1 (diff) | |
download | SMAPI-4fccaa35709440f98e275f0da3d2b65204b7d6e2.tar.gz SMAPI-4fccaa35709440f98e275f0da3d2b65204b7d6e2.tar.bz2 SMAPI-4fccaa35709440f98e275f0da3d2b65204b7d6e2.zip |
add nullable support in JSON converters
-rw-r--r-- | docs/release-notes.md | 5 | ||||
-rw-r--r-- | src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs | 7 |
2 files changed, 10 insertions, 2 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md index c04ac4e5..0678f6e1 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -3,7 +3,10 @@ # Release notes ## Upcoming release * For players: - * Fixed crash loading mods if they have corrupted translation files. + * Fixed crash loading mods with corrupted translation files. + +* For mod authors: + * Improved SMAPI's crossplatform read/writing of `Color`, `Point`, `Rectangle`, and `Vector2` in JSON to support nullable fields too. ## 3.12.6 Released 03 September 2021 for Stardew Valley 1.5.4 or later. diff --git a/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs index 549f0c18..ccc5158b 100644 --- a/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs +++ b/src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs @@ -22,7 +22,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters /// <param name="objectType">The object type.</param> public override bool CanConvert(Type objectType) { - return objectType == typeof(T); + return objectType == typeof(T) || Nullable.GetUnderlyingType(objectType) == typeof(T); } /// <summary>Writes the JSON representation of the object.</summary> @@ -44,10 +44,15 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters string path = reader.Path; switch (reader.TokenType) { + case JsonToken.Null when Nullable.GetUnderlyingType(objectType) != null: + return null; + case JsonToken.StartObject: return this.ReadObject(JObject.Load(reader), path); + case JsonToken.String: return this.ReadString(JToken.Load(reader).Value<string>(), path); + default: throw new SParseException($"Can't parse {typeof(T).Name} from {reader.TokenType} node (path: {reader.Path})."); } |