< Summary

Line coverage
35%
Covered lines: 28
Uncovered lines: 52
Coverable lines: 80
Total lines: 140
Line coverage: 35%
Branch coverage
39%
Covered branches: 11
Total branches: 28
Branch coverage: 39.2%
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(...)50%6668.75%
WriteCore(...)100%110%
ReadAsPropertyNameCore(...)100%110%
WriteAsPropertyNameCore(...)100%110%
ReadNumberWithCustomHandling(...)50%6660%
WriteNumberWithCustomHandling(...)0%220%
GetSchema(...)100%110%
GetSupportedJsonValueTypes(...)100%110%
Format(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt128Converter.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 UInt128Converter : JsonPrimitiveConverter<UInt128>
 13    {
 14        private const int MaxFormatLength = 39;
 15
 116        public UInt128Converter()
 117        {
 118            IsInternalConverterForNumberType = true;
 119        }
 20
 21        public override UInt128 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 822        {
 823            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 024            {
 025                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 26            }
 27
 828            if (reader.TokenType != JsonTokenType.Number)
 429            {
 430                ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType);
 31            }
 32
 433            return ReadCore(ref reader);
 434        }
 35
 36        public override void Write(Utf8JsonWriter writer, UInt128 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 unsafe UInt128 ReadCore(ref Utf8JsonReader reader)
 448        {
 449            int bufferLength = reader.ValueLength;
 50
 451            byte[]? rentedBuffer = null;
 452            Span<byte> buffer = bufferLength <= JsonConstants.StackallocByteThreshold
 453                ? stackalloc byte[JsonConstants.StackallocByteThreshold]
 454                : (rentedBuffer = ArrayPool<byte>.Shared.Rent(bufferLength));
 55
 456            int written = reader.CopyValue(buffer);
 457            if (!UInt128.TryParse(buffer.Slice(0, written), CultureInfo.InvariantCulture, out UInt128 result))
 058            {
 059                ThrowHelper.ThrowFormatException(NumericType.UInt128);
 60            }
 61
 462            if (rentedBuffer is not null)
 063            {
 064                ArrayPool<byte>.Shared.Return(rentedBuffer);
 065            }
 66
 467            return result;
 468        }
 69
 70        private static unsafe void WriteCore(Utf8JsonWriter writer, UInt128 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 UInt128 ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOp
 078        {
 079            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 080            return ReadCore(ref reader);
 081        }
 82
 83        internal override unsafe void WriteAsPropertyNameCore(Utf8JsonWriter writer, UInt128 value, JsonSerializerOption
 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 UInt128 ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, J
 491        {
 492            if (reader.TokenType == JsonTokenType.String &&
 493                (JsonNumberHandling.AllowReadingFromString & handling) != 0)
 094            {
 095                return ReadCore(ref reader);
 96            }
 97
 498            if (reader.TokenType != JsonTokenType.Number)
 499            {
 4100                ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(reader.TokenType);
 101            }
 102
 0103            return ReadCore(ref reader);
 0104        }
 105
 106        internal override unsafe void WriteNumberWithCustomHandling(Utf8JsonWriter writer, UInt128 value, JsonNumberHand
 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        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling numberHandling) =>
 0129            GetSupportedJsonValueTypesForNumericType(numberHandling);
 130
 131        private static void Format(
 132            Span<byte> destination,
 133            UInt128 value, out int written)
 0134        {
 0135            bool formattedSuccessfully = value.TryFormat(destination, out written, provider: CultureInfo.InvariantCultur
 0136            Debug.Assert(formattedSuccessfully);
 0137        }
 138    }
 139}
 140