mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Json;
|
|
|
|
namespace Microsoft.PowerToys.FilePreviewCommon.Monaco.Formatters
|
|
{
|
|
public class JsonFormatter : IFormatter
|
|
{
|
|
/// <inheritdoc/>
|
|
public string LangSet => "json";
|
|
|
|
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
|
|
{
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
};
|
|
|
|
/// <inheritdoc/>
|
|
public string Format(string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
using (var jDocument = JsonDocument.Parse(value, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip }))
|
|
{
|
|
FilePreviewJsonSerializerContext context = new(_serializerOptions);
|
|
return JsonSerializer.Serialize(jDocument, context.JsonDocument);
|
|
}
|
|
}
|
|
}
|
|
}
|