Repository navigation
Expand file tree
/
Copy pathUtf16SpanParsableJsonConverter.cs
More file actions
53 lines (46 loc) · 1.92 KB
/
Copy pathUtf16SpanParsableJsonConverter.cs
File metadata and controls
53 lines (46 loc) · 1.92 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
#if !USELIGHTWEIGHTJSONPARSER
using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Hi3Helper.Plugin.Core.Utility.Json.Converters;
/// <summary>
/// Converts any struct with both <see cref="ISpanParsable{TSelf}"/> and <see cref="ISpanFormattable"/> into the string representation.
/// </summary>
/// <typeparam name="TSpan">Where it's a member of both <see cref="ISpanParsable{TSelf}"/> and <see cref="ISpanFormattable"/></typeparam>
public class Utf16SpanParsableJsonConverter<TSpan> : JsonConverter<TSpan>
where TSpan : struct,
ISpanParsable<TSpan>,
ISpanFormattable,
IEquatable<TSpan>
{
private readonly TSpan _defaultValue = default;
public override TSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Span<char> chars = stackalloc char[256];
if (!Encoding.UTF8.TryGetChars(reader.ValueSpan, chars, out int written))
{
throw new InvalidOperationException("Buffer is too small to copy from UTF-8 to UTF-16 chars!");
}
if (TSpan.TryParse(chars[..written], null, out TSpan spanOutput))
{
return spanOutput;
}
throw new InvalidOperationException($"Value is not a valid {typeof(TSpan).Name}");
}
public override void Write(Utf8JsonWriter writer, TSpan value, JsonSerializerOptions options)
{
if ((options.DefaultIgnoreCondition.HasFlag(JsonIgnoreCondition.WhenWritingDefault) ||
options.DefaultIgnoreCondition.HasFlag(JsonIgnoreCondition.WhenWritingNull)) &&
value.Equals(_defaultValue))
{
return;
}
Span<char> writeBuffer = stackalloc char[256];
if (value.TryFormat(writeBuffer, out int written, ReadOnlySpan<char>.Empty, null))
{
writer.WriteStringValue(writeBuffer[..written]);
}
}
}
#endif