| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers.Text; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Text.Json.Nodes; |
| | | 7 | | using System.Text.Json.Schema; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | internal sealed class BooleanConverter : JsonPrimitiveConverter<bool> |
| | | 12 | | { |
| | | 13 | | public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 644 | 14 | | { |
| | 644 | 15 | | return reader.GetBoolean(); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) |
| | 0 | 19 | | { |
| | 0 | 20 | | writer.WriteBooleanValue(value); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | internal override bool ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptio |
| | 0 | 24 | | { |
| | 0 | 25 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 26 | | ReadOnlySpan<byte> propertyName = reader.GetUnescapedSpan(); |
| | 0 | 27 | | if (!(Utf8Parser.TryParse(propertyName, out bool value, out int bytesConsumed) |
| | 0 | 28 | | && propertyName.Length == bytesConsumed)) |
| | 0 | 29 | | { |
| | 0 | 30 | | ThrowHelper.ThrowFormatException(DataType.Boolean); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | return value; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, bool value, JsonSerializerOptions options, |
| | 0 | 37 | | { |
| | 0 | 38 | | writer.WritePropertyName(value); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.Boolean }; |
| | | 42 | | } |
| | | 43 | | } |