<PackageReference Include="System.Text.Json" Version="9.0.5" />

CharConverter

sealed class CharConverter : JsonPrimitiveConverter<char>
using System.Text.Json.Schema; namespace System.Text.Json.Serialization.Converters { internal sealed class CharConverter : JsonPrimitiveConverter<char> { private const int MaxEscapedCharacterLength = 6; public unsafe override char Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { JsonTokenType tokenType = reader.TokenType; if ((tokenType != JsonTokenType.PropertyName && tokenType != JsonTokenType.String) || 1 == 0) ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType); if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, 1, 6)) ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType); Span<char> destination = new Span<char>(stackalloc byte[12], 6); if (reader.CopyString(destination) != 1) ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType); return destination[0]; } public override void Write(Utf8JsonWriter writer, char value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString()); } internal override char ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return Read(ref reader, typeToConvert, options); } internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, char value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) { writer.WritePropertyName(value.ToString()); } internal override JsonSchema GetSchema(JsonNumberHandling _) { return new JsonSchema { Type = JsonSchemaType.String, MinLength = new int?(1), MaxLength = new int?(1) }; } } }