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