Skip to content

Fix JSON deserialization crash by adding JsonStringEnumConverter#30

Merged
dmcrider merged 2 commits intopatch/2025-01from
copilot/fix-mod-crash-on-load
Nov 14, 2025
Merged

Fix JSON deserialization crash by adding JsonStringEnumConverter#30
dmcrider merged 2 commits intopatch/2025-01from
copilot/fix-mod-crash-on-load

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 14, 2025

Mod crashes on load with JsonException when deserializing Crops.json because System.Text.Json cannot convert string enum values without explicit configuration.

Changes

  • Added JsonStringEnumConverter to deserialization options in LoadBaseGameCrops()
  • Added using System.Text.Json.Serialization directive
var options = new JsonSerializerOptions
{
    Converters = { new JsonStringEnumConverter() }
};
var list = JsonSerializer.Deserialize<List<Crop>>(json, options);

This allows the deserializer to convert "spring"Season.spring for the Seasons property.

Original prompt

This section details on the original issue you should resolve

<issue_title>Mod crashes on load</issue_title>
<issue_description>Mod crashed on entry and might not work correctly. Technical details: System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List1[LastDayToPlant.Season]. Path: $[0].Seasons[0] | LineNumber: 3 | BytePositionInLine: 25. at System.Text.Json.ThrowHelper.ThrowJsonException(String message) at System.Text.Json.Serialization.Converters.EnumConverter1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonCollectionConverter2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonCollectionConverter2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable1 actualByteCount) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo) at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) at LastDayToPlant.ModEntry.LoadBaseGameCrops() in D:_Projects\StardewMods\LastDayToPlant.backup\LastDayToPlant\ModEntry.cs:line 83 at LastDayToPlant.ModEntry.Entry(IModHelper helper) in D:_Projects\StardewMods\LastDayToPlant.backup\LastDayToPlant\ModEntry.cs:line 27 at StardewModdingAPI.Framework.SCore.LoadMods(IModMetadata[] mods, JsonHelper jsonHelper, ContentCoordinator contentCore, ModDatabase modDatabase) in SMAPI\Framework\SCore.cs:line 1950</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: dmcrider <9904609+dmcrider@users.noreply.github.com>
@dmcrider dmcrider removed their assignment Nov 14, 2025
Copilot AI changed the title [WIP] Fix mod crashing on load due to JSON exception Fix JSON deserialization crash by adding JsonStringEnumConverter Nov 14, 2025
Copilot AI requested a review from dmcrider November 14, 2025 14:04
@dmcrider dmcrider marked this pull request as ready for review November 14, 2025 14:05
Copilot AI review requested due to automatic review settings November 14, 2025 14:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical crash on mod load caused by System.Text.Json being unable to deserialize string enum values without explicit configuration. The fix adds JsonStringEnumConverter to enable proper conversion of season strings (e.g., "spring") to their corresponding Season enum values.

  • Added JsonStringEnumConverter to deserialization options in LoadBaseGameCrops()
  • Added the required using System.Text.Json.Serialization directive

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dmcrider
Copy link
Copy Markdown
Owner

@copilot Does this fix need to be applied to loading crop files for other mods?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 14, 2025

@copilot Does this fix need to be applied to loading crop files for other mods?

No, the fix is not needed for loading mod crops. The FromModFile() method in Crop.cs uses a different approach - it parses JSON with JsonNode.Parse() and manually converts strings to Season enums using Enum.TryParse<Season>(s, ignoreCase: true, ...) (lines 112, 128). This already handles string-to-enum conversion correctly without requiring JsonStringEnumConverter.

@dmcrider dmcrider merged commit 89dc1dc into patch/2025-01 Nov 14, 2025
7 checks passed
@dmcrider dmcrider deleted the copilot/fix-mod-crash-on-load branch November 14, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants