| | | 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 Int128Converter : JsonPrimitiveConverter<Int128> |
| | | 13 | | { |
| | | 14 | | private const int MaxFormatLength = 40; |
| | | 15 | | |
| | 1 | 16 | | public Int128Converter() |
| | 1 | 17 | | { |
| | 1 | 18 | | IsInternalConverterForNumberType = true; |
| | 1 | 19 | | } |
| | | 20 | | |
| | | 21 | | public override Int128 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 176 | 22 | | { |
| | 176 | 23 | | if (options?.NumberHandling is not null and not JsonNumberHandling.Strict) |
| | 0 | 24 | | { |
| | 0 | 25 | | return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options); |
| | | 26 | | } |
| | | 27 | | |
| | 176 | 28 | | if (reader.TokenType != JsonTokenType.Number) |
| | 150 | 29 | | { |
| | 150 | 30 | | ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType); |
| | | 31 | | } |
| | | 32 | | |
| | 26 | 33 | | return ReadCore(ref reader); |
| | 26 | 34 | | } |
| | | 35 | | |
| | | 36 | | public override void Write(Utf8JsonWriter writer, Int128 value, JsonSerializerOptions options) |
| | 0 | 37 | | { |
| | 0 | 38 | | if (options?.NumberHandling is not null and not JsonNumberHandling.Strict) |
| | 0 | 39 | | { |
| | 0 | 40 | | WriteNumberWithCustomHandling(writer, value, options.NumberHandling); |
| | 0 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | WriteCore(writer, value); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static Int128 ReadCore(ref Utf8JsonReader reader) |
| | 276 | 48 | | { |
| | 276 | 49 | | int bufferLength = reader.ValueLength; |
| | | 50 | | |
| | 276 | 51 | | byte[]? rentedBuffer = null; |
| | 276 | 52 | | Span<byte> buffer = bufferLength <= JsonConstants.StackallocByteThreshold |
| | 276 | 53 | | ? stackalloc byte[JsonConstants.StackallocByteThreshold] |
| | 276 | 54 | | : (rentedBuffer = ArrayPool<byte>.Shared.Rent(bufferLength)); |
| | | 55 | | |
| | 276 | 56 | | int written = reader.CopyValue(buffer); |
| | 228 | 57 | | if (!Int128.TryParse(buffer.Slice(0, written), CultureInfo.InvariantCulture, out Int128 result)) |
| | 94 | 58 | | { |
| | 94 | 59 | | ThrowHelper.ThrowFormatException(NumericType.Int128); |
| | 0 | 60 | | } |
| | | 61 | | |
| | 134 | 62 | | if (rentedBuffer != null) |
| | 0 | 63 | | { |
| | 0 | 64 | | ArrayPool<byte>.Shared.Return(rentedBuffer); |
| | 0 | 65 | | } |
| | | 66 | | |
| | 134 | 67 | | return result; |
| | 134 | 68 | | } |
| | | 69 | | |
| | | 70 | | private static void WriteCore(Utf8JsonWriter writer, Int128 value) |
| | 0 | 71 | | { |
| | 0 | 72 | | Span<byte> buffer = stackalloc byte[MaxFormatLength]; |
| | 0 | 73 | | Format(buffer, value, out int written); |
| | 0 | 74 | | writer.WriteRawValue(buffer.Slice(0, written)); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal override Int128 ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpt |
| | 0 | 78 | | { |
| | 0 | 79 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 80 | | return ReadCore(ref reader); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Int128 value, JsonSerializerOptions option |
| | 0 | 84 | | { |
| | 0 | 85 | | Span<byte> buffer = stackalloc byte[MaxFormatLength]; |
| | 0 | 86 | | Format(buffer, value, out int written); |
| | 0 | 87 | | writer.WritePropertyName(buffer.Slice(0, written)); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | internal override Int128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, Js |
| | 478 | 91 | | { |
| | 478 | 92 | | if (reader.TokenType == JsonTokenType.String && |
| | 478 | 93 | | (JsonNumberHandling.AllowReadingFromString & handling) != 0) |
| | 146 | 94 | | { |
| | 146 | 95 | | return ReadCore(ref reader); |
| | | 96 | | } |
| | | 97 | | |
| | 332 | 98 | | if (reader.TokenType != JsonTokenType.Number) |
| | 228 | 99 | | { |
| | 228 | 100 | | ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType); |
| | | 101 | | } |
| | | 102 | | |
| | 104 | 103 | | return ReadCore(ref reader); |
| | 108 | 104 | | } |
| | | 105 | | |
| | | 106 | | internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, Int128 value, JsonNumberHandling han |
| | 0 | 107 | | { |
| | 0 | 108 | | if ((JsonNumberHandling.WriteAsString & handling) != 0) |
| | 0 | 109 | | { |
| | | 110 | | const byte Quote = JsonConstants.Quote; |
| | 0 | 111 | | Span<byte> buffer = stackalloc byte[MaxFormatLength + 2]; |
| | 0 | 112 | | buffer[0] = Quote; |
| | 0 | 113 | | Format(buffer.Slice(1), value, out int written); |
| | | 114 | | |
| | 0 | 115 | | int length = written + 2; |
| | 0 | 116 | | buffer[length - 1] = Quote; |
| | 0 | 117 | | writer.WriteRawValue(buffer.Slice(0, length)); |
| | 0 | 118 | | } |
| | | 119 | | else |
| | 0 | 120 | | { |
| | 0 | 121 | | WriteCore(writer, value); |
| | 0 | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) => |
| | 0 | 126 | | GetSchemaForNumericType(JsonSchemaType.Integer, numberHandling); |
| | | 127 | | |
| | | 128 | | private static void Format( |
| | | 129 | | Span<byte> destination, |
| | | 130 | | Int128 value, out int written) |
| | 0 | 131 | | { |
| | 0 | 132 | | bool formattedSuccessfully = value.TryFormat(destination, out written, provider: CultureInfo.InvariantCultur |
| | 0 | 133 | | Debug.Assert(formattedSuccessfully); |
| | 0 | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |