IDictionaryConverter<TDictionary>
sealed class IDictionaryConverter<TDictionary> : JsonDictionaryConverter<TDictionary, string, object> where TDictionary : IDictionary
using System.Collections;
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 IDictionaryConverter<TDictionary> : JsonDictionaryConverter<TDictionary, string, object> where TDictionary : IDictionary
{
internal override bool CanPopulate => true;
protected override void Add(string key, [In] [System.Runtime.CompilerServices.IsReadOnly] object value, JsonSerializerOptions options, ref ReadStack state)
{
TDictionary val = (TDictionary)state.Current.ReturnValue;
val[key] = value;
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);
}
}
protected internal override bool OnWriteResume(Utf8JsonWriter writer, TDictionary value, JsonSerializerOptions options, ref WriteStack state)
{
IDictionaryEnumerator dictionaryEnumerator;
if (state.Current.CollectionEnumerator == null) {
dictionaryEnumerator = value.GetEnumerator();
state.Current.CollectionEnumerator = dictionaryEnumerator;
if (!dictionaryEnumerator.MoveNext())
return true;
} else
dictionaryEnumerator = (IDictionaryEnumerator)state.Current.CollectionEnumerator;
JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;
if (_valueConverter == null)
_valueConverter = JsonDictionaryConverter<TDictionary, string, object>.GetConverter<object>(jsonTypeInfo.ElementTypeInfo);
do {
if (JsonConverter.ShouldFlush(ref state, writer))
return false;
if ((int)state.Current.PropertyState < 2) {
state.Current.PropertyState = StackFramePropertyState.Name;
object key = dictionaryEnumerator.Key;
string text = key as string;
if (text != null) {
if (_keyConverter == null)
_keyConverter = JsonDictionaryConverter<TDictionary, string, object>.GetConverter<string>(jsonTypeInfo.KeyTypeInfo);
_keyConverter.WriteAsPropertyNameCore(writer, text, options, state.Current.IsWritingExtensionDataProperty);
} else
_valueConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProperty);
}
object value2 = dictionaryEnumerator.Value;
if (!_valueConverter.TryWrite(writer, ref value2, options, ref state))
return false;
state.Current.EndDictionaryEntry();
} while (dictionaryEnumerator.MoveNext());
return true;
}
internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options)
{
if (jsonTypeInfo.CreateObject == null && Type.IsAssignableFrom(typeof(Dictionary<string, object>)))
jsonTypeInfo.CreateObject = (() => new Dictionary<string, object>());
}
}
}