From 7adec9f7258714f7c0bb667211404ff73ebf1715 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Thu, 5 Jan 2023 08:45:26 -0800 Subject: [PATCH] Increase JSON size limit for project files The JavaScriptSerializer class throws an exception if the JSON data exceeds a certain size. The default limit is 2MB, which is relatively easy to hit because of the verbosity of the serialized data. Super Mario Bros needs about 1MB of JSON for a binary with 32KB + gfx. Using that as a guide, we need 32x the source file size for a thoroughly-documented project. We currently limit the max binary size to 1MB for practical reasons, so a cap of 32MB should cover us. This change raises the limit to 64MB, with a slightly higher limit on the deserialization side because we add newlines for readability. This is more of a workaround than a fix, but it should do for now. (issue #137) --- SourceGen/ProjectFile.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/SourceGen/ProjectFile.cs b/SourceGen/ProjectFile.cs index 98b58c1..04d56c2 100644 --- a/SourceGen/ProjectFile.cs +++ b/SourceGen/ProjectFile.cs @@ -54,6 +54,9 @@ namespace SourceGen { // may get lost as soon as they save the file. public const int CONTENT_VERSION = 5; + // Max JSON file length. + internal const int MAX_JSON_LENGTH = 64 * 1024 * 1024; + private static readonly bool ADD_CRLF = true; @@ -567,6 +570,7 @@ namespace SourceGen { } JavaScriptSerializer ser = new JavaScriptSerializer(); + ser.MaxJsonLength = ProjectFile.MAX_JSON_LENGTH; // increase max len beyond 2MB string cereal = ser.Serialize(spf); sb.Append(cereal); @@ -586,6 +590,10 @@ namespace SourceGen { public static bool DeserializeProject(string cereal, DisasmProject proj, FileLoadReport report) { JavaScriptSerializer ser = new JavaScriptSerializer(); + // Increase max size of JSON data. We need to make the deserialization limit larger + // than the serialization limit because we insert additional newlines to make the + // file more readable. 1/64th of max is probably about right. TODO: fix this properly. + ser.MaxJsonLength = ProjectFile.MAX_JSON_LENGTH + (ProjectFile.MAX_JSON_LENGTH / 64); SerializableProjectFile1 spf; try { spf = ser.Deserialize(cereal);