Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions DebugProbe.AspNetCore/Internal/JsonUtils.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
using System.Text.Json;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace DebugProbe.AspNetCore.Internal;

internal static class JsonUtils
{
public static string Format(string json)
{
if (string.IsNullOrWhiteSpace(json))
return json;

try
{
var parsed = JsonSerializer.Deserialize<object>(json);
return JsonSerializer.Serialize(parsed, new JsonSerializerOptions
{
WriteIndented = true
});
using var document = JsonDocument.Parse(json);

return JsonSerializer.Serialize(
document.RootElement,
new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
}
catch
{
return json;
}
}
}
}