forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJsonFormatter.cs
37 lines (31 loc) · 1.24 KB
/
JsonFormatter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
private static readonly FilePreviewJsonSerializerContext _filePreviewJsonSerializerContext = new(_serializerOptions);
/// <inheritdoc/>
public string Format(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
using (var jDocument = JsonDocument.Parse(value, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip }))
{
return JsonSerializer.Serialize(jDocument, _filePreviewJsonSerializerContext.JsonDocument);
}
}
}
}