DictionaryDefaultConverter<TDictionary, TKey, TValue>
abstract class DictionaryDefaultConverter<TDictionary, TKey, TValue> : JsonDictionaryConverter<TDictionary, TKey, TValue> where TDictionary : IEnumerable<KeyValuePair<TKey, TValue>>
using System.Collections.Generic;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization.Converters
{
internal abstract class DictionaryDefaultConverter<TDictionary, TKey, TValue> : JsonDictionaryConverter<TDictionary, TKey, TValue> where TDictionary : IEnumerable<KeyValuePair<TKey, TValue>>
{
internal override bool CanHaveMetadata => true;
protected internal override bool OnWriteResume(Utf8JsonWriter writer, TDictionary value, JsonSerializerOptions options, ref WriteStack state)
{
IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
if (state.Current.CollectionEnumerator == null) {
enumerator = value.GetEnumerator();
state.Current.CollectionEnumerator = enumerator;
if (!enumerator.MoveNext()) {
enumerator.Dispose();
return true;
}
} else
enumerator = (IEnumerator<KeyValuePair<TKey, TValue>>)state.Current.CollectionEnumerator;
JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;
if (_keyConverter == null)
_keyConverter = JsonDictionaryConverter<TDictionary, TKey, TValue>.GetConverter<TKey>(jsonTypeInfo.KeyTypeInfo);
if (_valueConverter == null)
_valueConverter = JsonDictionaryConverter<TDictionary, TKey, TValue>.GetConverter<TValue>(jsonTypeInfo.ElementTypeInfo);
do {
if (JsonConverter.ShouldFlush(ref state, writer))
return false;
KeyValuePair<TKey, TValue> current;
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))
return false;
state.Current.EndDictionaryEntry();
} while (enumerator.MoveNext());
enumerator.Dispose();
return true;
}
}
}