From 3d564ae42c838ece3e2a5cb641ea02d103f3da68 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 7 Apr 2025 21:38:54 -0400 Subject: [PATCH] fix server error if a JSON file contains nested comments --- docs/release-notes.md | 1 + .../Controllers/JsonValidatorController.cs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 896b3169..ea1c296b 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -14,6 +14,7 @@ * Reduced response times with a new analysis cache and client-side fetch. * Removed support for very old SMAPI logs. * You can now download a JSON representation of the parsed log (see the download link at the bottom of the log page). + * Fixed server error if a JSON file contains nested comment syntax. * Third-party libraries are now served from `smapi.io` instead of external CDNs. ## 4.2.1 diff --git a/src/SMAPI.Web/Controllers/JsonValidatorController.cs b/src/SMAPI.Web/Controllers/JsonValidatorController.cs index 231e28e2..54c99b8c 100644 --- a/src/SMAPI.Web/Controllers/JsonValidatorController.cs +++ b/src/SMAPI.Web/Controllers/JsonValidatorController.cs @@ -110,9 +110,19 @@ internal class JsonValidatorController : Controller } // format JSON - string formatted = parsed.ToString(Formatting.Indented); - result.SetContent(formatted, oldExpiry: file.OldExpiry, newExpiry: file.NewExpiry, uploadWarning: file.Warning); - parsed = JToken.Parse(formatted); // update line number references + try + { + string formatted = parsed.ToString(Formatting.Indented); + result.SetContent(formatted, oldExpiry: file.OldExpiry, newExpiry: file.NewExpiry, uploadWarning: file.Warning); + parsed = JToken.Parse(formatted); // update line number references + } + catch + { + // In rare cases, Json.NET will convert valid JSON to invalid JSON. For example, + // it will change `// remove /* */` to /*remove /* */*/` so it can't be parsed + // anymore. If that happens, just keep the JSON as-is; at worst line numbers will + // be slightly off if there are schema errors. + } } // skip if no schema selected