From 4fccaa35709440f98e275f0da3d2b65204b7d6e2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 13 Sep 2021 00:01:14 -0400 Subject: add nullable support in JSON converters --- .../Serialization/Converters/SimpleReadOnlyConverter.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/SMAPI.Toolkit/Serialization/Converters/SimpleReadOnlyConverter.cs') 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 /// The object type. public override bool CanConvert(Type objectType) { - return objectType == typeof(T); + return objectType == typeof(T) || Nullable.GetUnderlyingType(objectType) == typeof(T); } /// Writes the JSON representation of the object. @@ -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(), path); + default: throw new SParseException($"Can't parse {typeof(T).Name} from {reader.TokenType} node (path: {reader.Path})."); } -- cgit