mirror of
https://github.com/2dust/v2rayN.git
synced 2026-05-30 01:34:08 +00:00
fix(json): remove invalid plus signs before parsing config
This commit is contained in:
parent
0f3fc8e053
commit
9a3ff2dbcf
1 changed files with 72 additions and 1 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes a broken JSON-like string by removing plus signs (+)
|
||||
/// that are located outside quoted string values.
|
||||
/// </summary>
|
||||
/// <param name="json">
|
||||
/// The JSON-like input string that may contain invalid plus signs.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// A normalized JSON string that can be parsed by <see cref="JsonNode.Parse(string?, JsonNodeOptions?, JsonDocumentOptions)"/>.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// This method is designed for cases where the input looks similar to JSON
|
||||
/// but contains invalid plus signs, such as:
|
||||
///
|
||||
/// <code>
|
||||
/// {"scMaxEachPostBytes":+1000000,+"scMaxConcurrentPosts":+100,+"xPaddingBytes":+"100-1000"}
|
||||
/// </code>
|
||||
///
|
||||
/// The method removes only plus signs that are outside string values.
|
||||
///
|
||||
/// It does not remove plus signs inside quoted strings:
|
||||
///
|
||||
/// <code>
|
||||
/// {"phone":"+49123456789"}
|
||||
/// </code>
|
||||
///
|
||||
/// This behavior prevents accidental modification of valid string data.
|
||||
/// </remarks>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue