diff --git a/v2rayN/ServiceLib/Common/JsonUtils.cs b/v2rayN/ServiceLib/Common/JsonUtils.cs index 951cdbd7..7e11b5da 100644 --- a/v2rayN/ServiceLib/Common/JsonUtils.cs +++ b/v2rayN/ServiceLib/Common/JsonUtils.cs @@ -93,7 +93,8 @@ public class JsonUtils { return null; } - return JsonNode.Parse(strJson, nodeOptions: null, _defaultDocumentOptions); + + return JsonNode.Parse(NormalizeBrokenJson(strJson), nodeOptions: null, _defaultDocumentOptions); } catch { @@ -167,4 +168,74 @@ public class JsonUtils { return JsonSerializer.SerializeToNode(obj, options); } + + /// + /// Normalizes a broken JSON-like string by removing plus signs (+) + /// that are located outside quoted string values. + /// + /// + /// The JSON-like input string that may contain invalid plus signs. + /// + /// + /// A normalized JSON string that can be parsed by . + /// + /// + /// This method is designed for cases where the input looks similar to JSON + /// but contains invalid plus signs, such as: + /// + /// + /// {"scMaxEachPostBytes":+1000000,+"scMaxConcurrentPosts":+100,+"xPaddingBytes":+"100-1000"} + /// + /// + /// The method removes only plus signs that are outside string values. + /// + /// It does not remove plus signs inside quoted strings: + /// + /// + /// {"phone":"+49123456789"} + /// + /// + /// This behavior prevents accidental modification of valid string data. + /// + private static string NormalizeBrokenJson(string json) + { + var result = new StringBuilder(json.Length); + + bool insideString = false; + bool escaped = false; + + foreach (char ch in json) + { + if (escaped) + { + result.Append(ch); + escaped = false; + continue; + } + + if (ch == '\\' && insideString) + { + result.Append(ch); + escaped = true; + continue; + } + + if (ch == '"') + { + insideString = !insideString; + result.Append(ch); + continue; + } + + // Remove plus signs only when they are outside string values. + if (!insideString && ch == '+') + { + continue; + } + + result.Append(ch); + } + + return result.ToString(); + } }