Skip to content

Commit e2cd863

Browse files
[Bug fix] Add a format validation step before format conversion. (#36404)
This PR aims to fix the bug #35225 by introducing a new method IsJson to determine if a given text is in JSON format. The IsJson method is then utilized in the ToJsonFromXmlOrCsvAsync method to optimize the processing logic. If the text is already in JSON format, it is returned directly without further conversion from XML or CSV. Co-authored-by: Heiko <[email protected]> --------- Signed-off-by: Shawn Yuan <[email protected]> Co-authored-by: Heiko <[email protected]>
1 parent 2a6dcb9 commit e2cd863

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/modules/AdvancedPaste/AdvancedPaste/Helpers/JsonHelper.cs

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Text.Json;
89
using System.Text.RegularExpressions;
910
using System.Threading.Tasks;
1011
using System.Xml;
@@ -33,6 +34,19 @@ internal static class JsonHelper
3334
private static readonly Regex CsvRemoveStartAndEndQuotationMarksRegex = new Regex(@"^""(?=(""{2})+)|(?<=(""{2})+)""$");
3435
private static readonly Regex CsvReplaceDoubleQuotationMarksRegex = new Regex(@"""{2}");
3536

37+
private static bool IsJson(string text)
38+
{
39+
try
40+
{
41+
_ = JsonDocument.Parse(text);
42+
return true;
43+
}
44+
catch (Exception)
45+
{
46+
return false;
47+
}
48+
}
49+
3650
internal static async Task<string> ToJsonFromXmlOrCsvAsync(DataPackageView clipboardData)
3751
{
3852
Logger.LogTrace();
@@ -46,6 +60,12 @@ internal static async Task<string> ToJsonFromXmlOrCsvAsync(DataPackageView clipb
4660
var text = await clipboardData.GetTextAsync();
4761
string jsonText = string.Empty;
4862

63+
// If the text is already JSON, return it
64+
if (IsJson(text))
65+
{
66+
return text;
67+
}
68+
4969
// Try convert XML
5070
try
5171
{

0 commit comments

Comments
 (0)