Repository navigation
Expand file tree
/
Copy pathMinimalJsonEncoder.cs
More file actions
109 lines (97 loc) · 3.66 KB
/
Copy pathMinimalJsonEncoder.cs
File metadata and controls
109 lines (97 loc) · 3.66 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Buffers;
using System.Text;
using System.Text.Encodings.Web;
namespace Ankus.Examples.WalDecoder;
/// <summary>
/// Escapes only JSON syntax and ASCII control characters and writes every other character as it is.
/// </summary>
internal sealed class MinimalJsonEncoder : JavaScriptEncoder
{
/// <summary>
/// Gets the immutable JSON text encoder shared by the sample's writers.
/// </summary>
internal static MinimalJsonEncoder Instance { get; } = new();
/// <summary>
/// Gets the largest ASCII control escape, including its reverse solidus and four hexadecimal digits.
/// </summary>
public override int MaxOutputCharactersPerInputCharacter => 6;
/// <summary>
/// Checks the exact escape set used by serde_json's compact formatter.
/// </summary>
/// <param name="unicodeScalar">The scalar to inspect.</param>
/// <returns>Whether JSON requires that scalar to be escaped.</returns>
public override bool WillEncode(int unicodeScalar)
=> !Rune.IsValid(unicodeScalar) || unicodeScalar is >= 0 and < 0x20 or '"' or '\\';
/// <summary>
/// Finds the first JSON escape or malformed UTF-16 sequence.
/// </summary>
/// <param name="text">The UTF-16 source buffer.</param>
/// <param name="textLength">The number of source characters.</param>
/// <returns>The first required escape offset, or minus one.</returns>
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
{
ReadOnlySpan<char> source = new(text, textLength);
int index = 0;
while (index < source.Length)
{
if (Rune.DecodeFromUtf16(source[index..], out Rune scalar, out int consumed) != OperationStatus.Done || WillEncode(scalar.Value))
{
return index;
}
index += consumed;
}
return -1;
}
/// <summary>
/// Writes a JSON escape with the upstream formatter's lowercase hexadecimal digits.
/// </summary>
/// <param name="unicodeScalar">The scalar to encode.</param>
/// <param name="buffer">The destination buffer.</param>
/// <param name="bufferLength">The destination capacity.</param>
/// <param name="numberOfCharactersWritten">The completed escape length.</param>
/// <returns>Whether the complete scalar or escape fits.</returns>
public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
{
numberOfCharactersWritten = 0;
Span<char> destination = new(buffer, bufferLength);
if (!Rune.TryCreate(unicodeScalar, out Rune scalar))
{
return false;
}
if (!WillEncode(unicodeScalar))
{
return scalar.TryEncodeToUtf16(destination, out numberOfCharactersWritten);
}
char escape = unicodeScalar switch
{
'"' => '"',
'\\' => '\\',
'\b' => 'b',
'\t' => 't',
'\n' => 'n',
'\f' => 'f',
'\r' => 'r',
_ => '\0',
};
int length = escape == '\0' ? 6 : 2;
if (destination.Length < length)
{
return false;
}
destination[0] = '\\';
if (length == 2)
{
destination[1] = escape;
}
else
{
destination[1] = 'u';
destination[2] = '0';
destination[3] = '0';
destination[4] = "0123456789abcdef"[unicodeScalar >> 4];
destination[5] = "0123456789abcdef"[unicodeScalar & 0xF];
}
numberOfCharactersWritten = length;
return true;
}
}