ImmutableDictionaryOfTKeyTValueConverter<TDictionary, TKey, TValue>
using System.
Collections.
Generic;
using System.
Runtime.
CompilerServices;
using System.
Runtime.
InteropServices;
namespace System.
Text.
Json.
Serialization.
Converters
{
internal class ImmutableDictionaryOfTKeyTValueConverter<
TDictionary,
TKey,
TValue> :
DictionaryDefaultConverter<
TDictionary,
TKey,
TValue>
where TDictionary :
IReadOnlyDictionary<
TKey,
TValue>
{
internal sealed override bool CanHaveMetadata =>
false;
internal override bool SupportsCreateObjectDelegate =>
false;
internal sealed override bool IsConvertibleCollection =>
true;
protected sealed override void Add(
TKey key, [
In] [
System.
Runtime.
CompilerServices.
IsReadOnly]
TValue value,
JsonSerializerOptions options,
ref ReadStack state)
{
((
Dictionary<
TKey,
TValue>)
state.
Current.
ReturnValue)[
key] =
value;
}
protected sealed override void CreateCollection(
ref Utf8JsonReader reader, [
System.
Runtime.
CompilerServices.
ScopedRef]
ref ReadStack state)
{
state.
Current.
ReturnValue =
new Dictionary<
TKey,
TValue>();
}
protected sealed override void ConvertCollection(
ref ReadStack state,
JsonSerializerOptions options)
{
Func<
IEnumerable<
KeyValuePair<
TKey,
TValue>>,
TDictionary>
func = (
Func<
IEnumerable<
KeyValuePair<
TKey,
TValue>>,
TDictionary>)
state.
Current.
JsonTypeInfo.
CreateObjectWithArgs;
state.
Current.
ReturnValue =
func((
Dictionary<
TKey,
TValue>)
state.
Current.
ReturnValue);
}
}
}