|
| 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; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Globalization; |
| 7 | + |
| 8 | +namespace System.Text.Json.Serialization.Converters |
| 9 | +{ |
| 10 | + internal sealed class HalfConverter : JsonPrimitiveConverter<Half> |
| 11 | + { |
| 12 | + private const int MaxFormatLength = 20; |
| 13 | + |
| 14 | + public HalfConverter() |
| 15 | + { |
| 16 | + IsInternalConverterForNumberType = true; |
| 17 | + } |
| 18 | + |
| 19 | + public override Half Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 20 | + { |
| 21 | + if (reader.TokenType != JsonTokenType.Number) |
| 22 | + { |
| 23 | + ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType); |
| 24 | + } |
| 25 | + |
| 26 | + return ReadCore(ref reader); |
| 27 | + } |
| 28 | + |
| 29 | + public override void Write(Utf8JsonWriter writer, Half value, JsonSerializerOptions options) |
| 30 | + { |
| 31 | + WriteCore(writer, value); |
| 32 | + } |
| 33 | + |
| 34 | + private static Half ReadCore(ref Utf8JsonReader reader) |
| 35 | + { |
| 36 | + Half result; |
| 37 | + |
| 38 | + byte[]? rentedByteBuffer = null; |
| 39 | + int bufferLength = reader.ValueLength; |
| 40 | + |
| 41 | + Span<byte> byteBuffer = bufferLength <= JsonConstants.StackallocByteThreshold |
| 42 | + ? stackalloc byte[JsonConstants.StackallocByteThreshold] |
| 43 | + : (rentedByteBuffer = ArrayPool<byte>.Shared.Rent(bufferLength)); |
| 44 | + |
| 45 | + int written = reader.CopyValue(byteBuffer); |
| 46 | + byteBuffer = byteBuffer.Slice(0, written); |
| 47 | + |
| 48 | + bool success = TryParse(byteBuffer, out result); |
| 49 | + if (rentedByteBuffer != null) |
| 50 | + { |
| 51 | + ArrayPool<byte>.Shared.Return(rentedByteBuffer); |
| 52 | + } |
| 53 | + |
| 54 | + if (!success) |
| 55 | + { |
| 56 | + ThrowHelper.ThrowFormatException(NumericType.Half); |
| 57 | + } |
| 58 | + |
| 59 | + Debug.Assert(!Half.IsNaN(result) && !Half.IsInfinity(result)); |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + private static void WriteCore(Utf8JsonWriter writer, Half value) |
| 64 | + { |
| 65 | +#if NET8_0_OR_GREATER |
| 66 | + Span<byte> buffer = stackalloc byte[MaxFormatLength]; |
| 67 | +#else |
| 68 | + Span<char> buffer = stackalloc char[MaxFormatLength]; |
| 69 | +#endif |
| 70 | + Format(buffer, value, out int written); |
| 71 | + writer.WriteRawValue(buffer.Slice(0, written)); |
| 72 | + } |
| 73 | + |
| 74 | + internal override Half ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 75 | + { |
| 76 | + Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| 77 | + return ReadCore(ref reader); |
| 78 | + } |
| 79 | + |
| 80 | + internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Half value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) |
| 81 | + { |
| 82 | +#if NET8_0_OR_GREATER |
| 83 | + Span<byte> buffer = stackalloc byte[MaxFormatLength]; |
| 84 | +#else |
| 85 | + Span<char> buffer = stackalloc char[MaxFormatLength]; |
| 86 | +#endif |
| 87 | + Format(buffer, value, out int written); |
| 88 | + writer.WritePropertyName(buffer.Slice(0, written)); |
| 89 | + } |
| 90 | + |
| 91 | + internal override Half ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options) |
| 92 | + { |
| 93 | + if (reader.TokenType == JsonTokenType.String) |
| 94 | + { |
| 95 | + if ((JsonNumberHandling.AllowReadingFromString & handling) != 0) |
| 96 | + { |
| 97 | + if (TryGetFloatingPointConstant(ref reader, out Half value)) |
| 98 | + { |
| 99 | + return value; |
| 100 | + } |
| 101 | + |
| 102 | + return ReadCore(ref reader); |
| 103 | + } |
| 104 | + else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0) |
| 105 | + { |
| 106 | + if (!TryGetFloatingPointConstant(ref reader, out Half value)) |
| 107 | + { |
| 108 | + ThrowHelper.ThrowFormatException(NumericType.Half); |
| 109 | + } |
| 110 | + |
| 111 | + return value; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return Read(ref reader, Type, options); |
| 116 | + } |
| 117 | + |
| 118 | + internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, Half value, JsonNumberHandling handling) |
| 119 | + { |
| 120 | + if ((JsonNumberHandling.WriteAsString & handling) != 0) |
| 121 | + { |
| 122 | +#if NET8_0_OR_GREATER |
| 123 | + const byte Quote = JsonConstants.Quote; |
| 124 | + Span<byte> buffer = stackalloc byte[MaxFormatLength + 2]; |
| 125 | +#else |
| 126 | + const char Quote = (char)JsonConstants.Quote; |
| 127 | + Span<char> buffer = stackalloc char[MaxFormatLength + 2]; |
| 128 | +#endif |
| 129 | + buffer[0] = Quote; |
| 130 | + Format(buffer.Slice(1), value, out int written); |
| 131 | + |
| 132 | + int length = written + 2; |
| 133 | + buffer[length - 1] = Quote; |
| 134 | + writer.WriteRawValue(buffer.Slice(0, length)); |
| 135 | + } |
| 136 | + else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0) |
| 137 | + { |
| 138 | + WriteFloatingPointConstant(writer, value); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + WriteCore(writer, value); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static bool TryGetFloatingPointConstant(ref Utf8JsonReader reader, out Half value) |
| 147 | + { |
| 148 | + Span<byte> buffer = stackalloc byte[MaxFormatLength]; |
| 149 | + int written = reader.CopyValue(buffer); |
| 150 | + |
| 151 | + return JsonReaderHelper.TryGetFloatingPointConstant(buffer.Slice(0, written), out value); |
| 152 | + } |
| 153 | + |
| 154 | + private static void WriteFloatingPointConstant(Utf8JsonWriter writer, Half value) |
| 155 | + { |
| 156 | + if (Half.IsNaN(value)) |
| 157 | + { |
| 158 | + writer.WriteNumberValueAsStringUnescaped(JsonConstants.NaNValue); |
| 159 | + } |
| 160 | + else if (Half.IsPositiveInfinity(value)) |
| 161 | + { |
| 162 | + writer.WriteNumberValueAsStringUnescaped(JsonConstants.PositiveInfinityValue); |
| 163 | + } |
| 164 | + else if (Half.IsNegativeInfinity(value)) |
| 165 | + { |
| 166 | + writer.WriteNumberValueAsStringUnescaped(JsonConstants.NegativeInfinityValue); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + WriteCore(writer, value); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static bool TryParse(ReadOnlySpan<byte> buffer, out Half result) |
| 175 | + { |
| 176 | +#if NET8_0_OR_GREATER |
| 177 | + bool success = Half.TryParse(buffer, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result); |
| 178 | +#else |
| 179 | + // Half.TryFormat/TryParse(ROS<byte>) are not available on .NET 7 |
| 180 | + // we need to use Half.TryFormat/TryParse(ROS<char>) in that case. |
| 181 | + char[]? rentedCharBuffer = null; |
| 182 | + |
| 183 | + Span<char> charBuffer = buffer.Length <= JsonConstants.StackallocCharThreshold |
| 184 | + ? stackalloc char[JsonConstants.StackallocCharThreshold] |
| 185 | + : (rentedCharBuffer = ArrayPool<char>.Shared.Rent(buffer.Length)); |
| 186 | + |
| 187 | + int written = JsonReaderHelper.TranscodeHelper(buffer, charBuffer); |
| 188 | + |
| 189 | + bool success = Half.TryParse(charBuffer, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result); |
| 190 | + |
| 191 | + if (rentedCharBuffer != null) |
| 192 | + { |
| 193 | + ArrayPool<char>.Shared.Return(rentedCharBuffer); |
| 194 | + } |
| 195 | +#endif |
| 196 | + |
| 197 | + // Half.TryParse is more lax with floating-point literals than other S.T.Json floating-point types |
| 198 | + // e.g: it parses "naN" successfully. Only succeed with the exact match. |
| 199 | + return success && |
| 200 | + (!Half.IsNaN(result) || buffer.SequenceEqual(JsonConstants.NaNValue)) && |
| 201 | + (!Half.IsPositiveInfinity(result) || buffer.SequenceEqual(JsonConstants.PositiveInfinityValue)) && |
| 202 | + (!Half.IsNegativeInfinity(result) || buffer.SequenceEqual(JsonConstants.NegativeInfinityValue)); |
| 203 | + } |
| 204 | + |
| 205 | + private static void Format( |
| 206 | +#if NET8_0_OR_GREATER |
| 207 | + Span<byte> destination, |
| 208 | +#else |
| 209 | + Span<char> destination, |
| 210 | +#endif |
| 211 | + Half value, out int written) |
| 212 | + { |
| 213 | + bool formattedSuccessfully = value.TryFormat(destination, out written, provider: CultureInfo.InvariantCulture); |
| 214 | + Debug.Assert(formattedSuccessfully); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments