< Summary

Information
Line coverage
42%
Covered lines: 34
Uncovered lines: 46
Coverable lines: 80
Total lines: 136
Line coverage: 42.5%
Branch coverage
53%
Covered branches: 15
Total branches: 28
Branch coverage: 53.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
Read(...)62.5%8877.77%
Write(...)0%660%
ReadCore(...)66.66%6676.47%
WriteCore(...)100%110%
ReadAsPropertyNameCore(...)100%110%
WriteAsPropertyNameCore(...)100%110%
ReadNumberWithCustomHandling(...)100%66100%
WriteNumberWithCustomHandling(...)0%220%
GetSchema(...)100%110%
Format(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Value\Int128Converter.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Globalization;
 7using System.Text.Json.Nodes;
 8using System.Text.Json.Schema;
 9
 10namespace System.Text.Json.Serialization.Converters
 11{
 12    internal sealed class Int128Converter : JsonPrimitiveConverter<Int128>
 13    {
 14        private const int MaxFormatLength = 40;
 15
 116        public Int128Converter()
 117        {
 118            IsInternalConverterForNumberType = true;
 119        }
 20
 21        public override Int128 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 17622        {
 17623            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 024            {
 025                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 26            }
 27
 17628            if (reader.TokenType != JsonTokenType.Number)
 15029            {
 15030                ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType);
 31            }
 32
 2633            return ReadCore(ref reader);
 2634        }
 35
 36        public override void Write(Utf8JsonWriter writer, Int128 value, JsonSerializerOptions options)
 037        {
 038            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 039            {
 040                WriteNumberWithCustomHandling(writer, value, options.NumberHandling);
 041                return;
 42            }
 43
 044            WriteCore(writer, value);
 045        }
 46
 47        private static Int128 ReadCore(ref Utf8JsonReader reader)
 27648        {
 27649            int bufferLength = reader.ValueLength;
 50
 27651            byte[]? rentedBuffer = null;
 27652            Span<byte> buffer = bufferLength <= JsonConstants.StackallocByteThreshold
 27653                ? stackalloc byte[JsonConstants.StackallocByteThreshold]
 27654                : (rentedBuffer = ArrayPool<byte>.Shared.Rent(bufferLength));
 55
 27656            int written = reader.CopyValue(buffer);
 22857            if (!Int128.TryParse(buffer.Slice(0, written), CultureInfo.InvariantCulture, out Int128 result))
 9458            {
 9459                ThrowHelper.ThrowFormatException(NumericType.Int128);
 060            }
 61
 13462            if (rentedBuffer != null)
 063            {
 064                ArrayPool<byte>.Shared.Return(rentedBuffer);
 065            }
 66
 13467            return result;
 13468        }
 69
 70        private static void WriteCore(Utf8JsonWriter writer, Int128 value)
 071        {
 072            Span<byte> buffer = stackalloc byte[MaxFormatLength];
 073            Format(buffer, value, out int written);
 074            writer.WriteRawValue(buffer.Slice(0, written));
 075        }
 76
 77        internal override Int128 ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpt
 078        {
 079            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 080            return ReadCore(ref reader);
 081        }
 82
 83        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Int128 value, JsonSerializerOptions option
 084        {
 085            Span<byte> buffer = stackalloc byte[MaxFormatLength];
 086            Format(buffer, value, out int written);
 087            writer.WritePropertyName(buffer.Slice(0, written));
 088        }
 89
 90        internal override Int128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, Js
 47891        {
 47892            if (reader.TokenType == JsonTokenType.String &&
 47893                (JsonNumberHandling.AllowReadingFromString & handling) != 0)
 14694            {
 14695                return ReadCore(ref reader);
 96            }
 97
 33298            if (reader.TokenType != JsonTokenType.Number)
 22899            {
 228100                ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType);
 101            }
 102
 104103            return ReadCore(ref reader);
 108104        }
 105
 106        internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, Int128 value, JsonNumberHandling han
 0107        {
 0108            if ((JsonNumberHandling.WriteAsString & handling) != 0)
 0109            {
 110                const byte Quote = JsonConstants.Quote;
 0111                Span<byte> buffer = stackalloc byte[MaxFormatLength + 2];
 0112                buffer[0] = Quote;
 0113                Format(buffer.Slice(1), value, out int written);
 114
 0115                int length = written + 2;
 0116                buffer[length - 1] = Quote;
 0117                writer.WriteRawValue(buffer.Slice(0, length));
 0118            }
 119            else
 0120            {
 0121                WriteCore(writer, value);
 0122            }
 0123        }
 124
 125        internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) =>
 0126            GetSchemaForNumericType(JsonSchemaType.Integer, numberHandling);
 127
 128        private static void Format(
 129            Span<byte> destination,
 130            Int128 value, out int written)
 0131        {
 0132            bool formattedSuccessfully = value.TryFormat(destination, out written, provider: CultureInfo.InvariantCultur
 0133            Debug.Assert(formattedSuccessfully);
 0134        }
 135    }
 136}