-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonCompiler.cs
53 lines (45 loc) · 2.08 KB
/
JsonCompiler.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using YetAnotherPacketParser.Ast;
namespace YetAnotherPacketParser.Compiler.Json
{
internal class JsonCompiler : ICompiler
{
public JsonCompiler(JsonCompilerOptions? options = null)
{
this.Options = options ?? JsonCompilerOptions.Default;
}
private JsonCompilerOptions Options { get; }
public async Task<string> CompileAsync(PacketNode packet)
{
Verify.IsNotNull(packet, nameof(packet));
SanitizeHtmlTransformer sanitizer = new SanitizeHtmlTransformer();
PacketNode sanitizedPacket = sanitizer.Sanitize(packet);
// The format that Jerry's parser uses for JSON (and that the reader expects as a result) is different
// than the structure of the PacketNode, so transform it to a structure close to it (minus author and
// packet fields)
JsonPacketNode sanitizedJsonPacket = new JsonPacketNode(sanitizedPacket, this.Options.ModaqFormat);
using (Stream stream = new MemoryStream())
{
JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
{
AllowTrailingCommas = true,
PropertyNamingPolicy = new PascalCaseJsonNamingPolicy(),
WriteIndented = this.Options.PrettyPrint,
IgnoreNullValues = true
};
// TODO: If we decide to host this directly in an ASP.Net context, remove ConfigureAwait calls
// see https://devblogs.microsoft.com/dotnet/configureawait-faq/
await JsonSerializer.SerializeAsync(stream, sanitizedJsonPacket, serializerOptions).ConfigureAwait(false);
// Reset the stream
stream.Position = 0;
using (StreamReader writer = new StreamReader(stream, Encoding.UTF8))
{
return await writer.ReadToEndAsync().ConfigureAwait(false);
}
}
}
}
}