IDictionaryOfTKeyTValueConverter<TDictionary, TKey, TValue>
sealed class IDictionaryOfTKeyTValueConverter<TDictionary, TKey, TValue> : DictionaryDefaultConverter<TDictionary, TKey, TValue> where TDictionary : IDictionary<TKey, TValue>
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization.Converters
{
internal sealed class IDictionaryOfTKeyTValueConverter<TDictionary, TKey, TValue> : DictionaryDefaultConverter<TDictionary, TKey, TValue> where TDictionary : IDictionary<TKey, TValue>
{
internal override bool CanPopulate => true;
protected override void Add(TKey key, [In] [System.Runtime.CompilerServices.IsReadOnly] TValue value, JsonSerializerOptions options, ref ReadStack state)
{
TDictionary val = (TDictionary)state.Current.ReturnValue;
if (options.AllowDuplicateProperties)
val[key] = value;
else if (!JsonHelpers.TryAdd<TKey, TValue>((IDictionary<TKey, TValue>)(object)val, key, value)) {
ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed();
}
if (base.IsValueType)
state.Current.ReturnValue = val;
}
protected override void CreateCollection(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state)
{
base.CreateCollection(ref reader, ref state);
if (((TDictionary)state.Current.ReturnValue).IsReadOnly) {
state.Current.ReturnValue = null;
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state);
}
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
if (jsonTypeInfo.CreateObject == null && Type.IsAssignableFrom(typeof(Dictionary<TKey, TValue>)))
jsonTypeInfo.CreateObject = (() => new Dictionary<TKey, TValue>());
}
}
}