< Summary

Information
Class: System.Text.Json.Serialization.Converters.DictionaryOfTKeyTValueConverter<T1, T2, T3>
Assembly: System.Text.Json
File(s): C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Collection\DictionaryOfTKeyTValueConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 59
Coverable lines: 59
Total lines: 105
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Add(TKey,TValue& modreq(...)0%440%
OnWriteResume(...)0%24240%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Collection\DictionaryOfTKeyTValueConverter.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.Collections.Generic;
 5using System.Text.Json.Serialization.Metadata;
 6
 7namespace System.Text.Json.Serialization.Converters
 8{
 9    /// <summary>
 10    /// Converter for Dictionary{string, TValue} that (de)serializes as a JSON object with properties
 11    /// representing the dictionary element key and value.
 12    /// </summary>
 13    internal sealed class DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>
 14        : DictionaryDefaultConverter<TCollection, TKey, TValue>
 15        where TCollection : Dictionary<TKey, TValue>
 16        where TKey : notnull
 17    {
 018        internal override bool CanPopulate => true;
 19
 20        protected override void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state)
 021        {
 022            TCollection dictionary = (TCollection)state.Current.ReturnValue!;
 23
 024            if (options.AllowDuplicateProperties)
 025            {
 026                dictionary[key] = value;
 027            }
 28            else
 029            {
 030                if (!dictionary.TryAdd(key, value))
 031                {
 032                    ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed();
 33                }
 034            }
 035        }
 36
 37        protected internal override bool OnWriteResume(
 38            Utf8JsonWriter writer,
 39            TCollection value,
 40            JsonSerializerOptions options,
 41            ref WriteStack state)
 042        {
 43            Dictionary<TKey, TValue>.Enumerator enumerator;
 044            if (state.Current.CollectionEnumerator == null)
 045            {
 046                enumerator = value.GetEnumerator();
 047                if (!enumerator.MoveNext())
 048                {
 049                    enumerator.Dispose();
 050                    return true;
 51                }
 052            }
 53            else
 054            {
 055                enumerator = (Dictionary<TKey, TValue>.Enumerator)state.Current.CollectionEnumerator;
 056            }
 57
 058            JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;
 059            _keyConverter ??= GetConverter<TKey>(typeInfo.KeyTypeInfo!);
 060            _valueConverter ??= GetConverter<TValue>(typeInfo.ElementTypeInfo!);
 61
 062            if (!state.SupportContinuation && _valueConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling ==
 063            {
 64                // Fast path that avoids validation and extra indirection.
 65                do
 066                {
 067                    TKey key = enumerator.Current.Key;
 068                    _keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProp
 069                    _valueConverter.Write(writer, enumerator.Current.Value, options);
 070                } while (enumerator.MoveNext());
 071            }
 72            else
 073            {
 74                do
 075                {
 076                    if (ShouldFlush(ref state, writer))
 077                    {
 078                        state.Current.CollectionEnumerator = enumerator;
 079                        return false;
 80                    }
 81
 082                    if (state.Current.PropertyState < StackFramePropertyState.Name)
 083                    {
 084                        state.Current.PropertyState = StackFramePropertyState.Name;
 85
 086                        TKey key = enumerator.Current.Key;
 087                        _keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionData
 088                    }
 89
 090                    TValue element = enumerator.Current.Value;
 091                    if (!_valueConverter.TryWrite(writer, element, options, ref state))
 092                    {
 093                        state.Current.CollectionEnumerator = enumerator;
 094                        return false;
 95                    }
 96
 097                    state.Current.EndDictionaryEntry();
 098                } while (enumerator.MoveNext());
 099            }
 100
 0101            enumerator.Dispose();
 0102            return true;
 0103        }
 104    }
 105}