DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>
sealed class DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue> : DictionaryDefaultConverter<TCollection, TKey, TValue> where TCollection : Dictionary<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 DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue> : DictionaryDefaultConverter<TCollection, TKey, TValue> where TCollection : Dictionary<TKey, TValue>
{
internal override bool CanPopulate => true;
protected override void Add(TKey key, [In] [IsReadOnly] TValue value, JsonSerializerOptions options, ref ReadStack state)
{
((TCollection)state.Current.ReturnValue)[key] = value;
}
protected internal override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
{
Dictionary<TKey, TValue>.Enumerator enumerator;
if (state.Current.CollectionEnumerator == null) {
enumerator = value.GetEnumerator();
if (!enumerator.MoveNext()) {
enumerator.Dispose();
return true;
}
} else
enumerator = (Dictionary<TKey, TValue>.Enumerator)state.Current.CollectionEnumerator;
JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;
if (_keyConverter == null)
_keyConverter = JsonDictionaryConverter<TCollection, TKey, TValue>.GetConverter<TKey>(jsonTypeInfo.KeyTypeInfo);
if (_valueConverter == null)
_valueConverter = JsonDictionaryConverter<TCollection, TKey, TValue>.GetConverter<TValue>(jsonTypeInfo.ElementTypeInfo);
KeyValuePair<TKey, TValue> current;
if (state.SupportContinuation || !_valueConverter.CanUseDirectReadOrWrite || state.Current.NumberHandling.HasValue) {
do {
if (JsonConverter.ShouldFlush(ref state, writer)) {
state.Current.CollectionEnumerator = enumerator;
return false;
}
if ((int)state.Current.PropertyState < 2) {
state.Current.PropertyState = StackFramePropertyState.Name;
current = enumerator.Current;
TKey key = current.Key;
_keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProperty);
}
current = enumerator.Current;
TValue value2 = current.Value;
if (!_valueConverter.TryWrite(writer, ref value2, options, ref state)) {
state.Current.CollectionEnumerator = enumerator;
return false;
}
state.Current.EndDictionaryEntry();
} while (enumerator.MoveNext());
} else {
do {
current = enumerator.Current;
TKey key2 = current.Key;
_keyConverter.WriteAsPropertyNameCore(writer, key2, options, state.Current.IsWritingExtensionDataProperty);
JsonConverter<TValue> valueConverter = _valueConverter;
current = enumerator.Current;
valueConverter.Write(writer, current.Value, options);
} while (enumerator.MoveNext());
}
enumerator.Dispose();
return true;
}
}
}