<PackageReference Include="System.Text.Json" Version="5.0.0-preview.1.20120.5" />
CharConverter
using System.
Runtime.
InteropServices;
namespace System.
Text.
Json.
Serialization.
Converters
{
internal sealed class CharConverter :
JsonConverter<
char>
{
public override char Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
string string =
reader.
GetString();
if (
string.
IsNullOrEmpty(
string) ||
string.
Length >
1)
throw ThrowHelper.
GetInvalidOperationException_ExpectedChar(
reader.
TokenType);
return string[
0];
}
public override void Write(
Utf8JsonWriter writer,
char value,
JsonSerializerOptions options)
{
writer.
WriteStringValue(
MemoryMarshal.
CreateSpan(
ref value,
1));
}
}
}