< Summary

Information
Class: System.Text.Json.Serialization.Converters.IDictionaryOfTKeyTValueConverter<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\IDictionaryOfTKeyTValueConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 64
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
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%660%
CreateCollection(...)0%220%
ConfigureJsonTypeInfo(...)0%660%

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\IDictionaryOfTKeyTValueConverter.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.Diagnostics;
 6using System.Text.Json.Serialization.Metadata;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    /// <summary>
 11    /// Converter for <cref>System.Collections.Generic.IDictionary{TKey, TValue}</cref> that
 12    /// (de)serializes as a JSON object with properties representing the dictionary element key and value.
 13    /// </summary>
 14    internal sealed class IDictionaryOfTKeyTValueConverter<TDictionary, TKey, TValue>
 15        : DictionaryDefaultConverter<TDictionary, TKey, TValue>
 16        where TDictionary : IDictionary<TKey, TValue>
 17        where TKey : notnull
 18    {
 019        internal override bool CanPopulate => true;
 20
 21        protected override void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state)
 022        {
 023            TDictionary collection = (TDictionary)state.Current.ReturnValue!;
 24
 025            if (options.AllowDuplicateProperties)
 026            {
 027                collection[key] = value;
 028            }
 29            else
 030            {
 031                if (!collection.TryAdd(key, value))
 032                {
 033                    ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed();
 34                }
 035            }
 36
 037            if (IsValueType)
 038            {
 039                state.Current.ReturnValue = collection;
 040            };
 041        }
 42
 43        protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state)
 044        {
 045            base.CreateCollection(ref reader, ref state);
 046            TDictionary returnValue = (TDictionary)state.Current.ReturnValue!;
 047            if (returnValue.IsReadOnly)
 048            {
 049                state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting.
 050                ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
 51            }
 052        }
 53
 54        internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
 055        {
 56            // Deserialize as Dictionary<TKey,TValue> for interface types that support it.
 057            if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(Dictionary<TKey, TValue>)))
 058            {
 059                Debug.Assert(Type.IsInterface);
 060                jsonTypeInfo.CreateObject = () => new Dictionary<TKey, TValue>();
 061            }
 062        }
 63    }
 64}