Skip to content
Open
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
11 changes: 9 additions & 2 deletions src/MiniProfiler.Shared/Internal/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
#elif STJSON
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
#endif
Expand Down Expand Up @@ -134,6 +135,13 @@ public static string ToJson(this List<Guid>? guids)
private static readonly JsonSerializerOptions defaultSettings = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

private static readonly JsonSerializerOptions htmlEscapeSettings = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = JavaScriptEncoder.Default,
};

/// <summary>
Expand All @@ -142,10 +150,9 @@ public static string ToJson(this List<Guid>? guids)
/// <param name="profiler">The <see cref="MiniProfiler"/> to serialize.</param>
/// <param name="htmlEscape">Whether to HTML escape the output.</param>
[return: NotNullIfNotNull(nameof(profiler))]
[SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Compatibility across versions")]
public static string? ToJson(this MiniProfiler? profiler, bool htmlEscape = false) =>
profiler != default
? JsonSerializer.Serialize(profiler, defaultSettings)
? JsonSerializer.Serialize(profiler, htmlEscape ? htmlEscapeSettings : defaultSettings)
: null;

/// <summary>
Expand Down