<PackageReference Include="System.Text.Json" Version="5.0.0-preview.2.20160.6" />

ImmutableDictionaryOfStringTValueConverter<TCollection, TValue>

sealed class ImmutableDictionaryOfStringTValueConverter<TCollection, TValue> : DictionaryDefaultConverter<TCollection, TValue> where TCollection : IReadOnlyDictionary<string, TValue>
using System.Collections.Generic; namespace System.Text.Json.Serialization.Converters { internal sealed class ImmutableDictionaryOfStringTValueConverter<TCollection, TValue> : DictionaryDefaultConverter<TCollection, TValue> where TCollection : IReadOnlyDictionary<string, TValue> { private Func<IEnumerable<KeyValuePair<string, TValue>>, TCollection> _creatorDelegate; internal override bool CanHaveIdMetadata => false; protected override void Add(TValue value, JsonSerializerOptions options, ref ReadStack state) { string jsonPropertyNameAsString = state.Current.JsonPropertyNameAsString; ((Dictionary<string, TValue>)state.Current.ReturnValue)[jsonPropertyNameAsString] = value; } protected override void CreateCollection(ref ReadStack state) { state.Current.ReturnValue = new Dictionary<string, TValue>(); } protected override void ConvertCollection(ref ReadStack state, JsonSerializerOptions options) { state.Current.ReturnValue = GetCreatorDelegate(options)((Dictionary<string, TValue>)state.Current.ReturnValue); } protected internal override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state) { IEnumerator<KeyValuePair<string, TValue>> enumerator; if (state.Current.CollectionEnumerator == null) { enumerator = value.GetEnumerator(); if (!enumerator.MoveNext()) return true; } else enumerator = (Dictionary<string, TValue>.Enumerator)state.Current.CollectionEnumerator; JsonConverter<TValue> valueConverter = DictionaryDefaultConverter<TCollection, TValue>.GetValueConverter(ref state); do { if (ShouldFlush(writer, ref state)) { state.Current.CollectionEnumerator = enumerator; return false; } KeyValuePair<string, TValue> current = enumerator.Current; string keyName = GetKeyName(current.Key, ref state, options); writer.WritePropertyName(keyName); current = enumerator.Current; TValue value2 = current.Value; if (!valueConverter.TryWrite(writer, value2, options, ref state)) { state.Current.CollectionEnumerator = enumerator; return false; } state.Current.EndDictionaryElement(); } while (enumerator.MoveNext()); return true; } private Func<IEnumerable<KeyValuePair<string, TValue>>, TCollection> GetCreatorDelegate(JsonSerializerOptions options) { if (_creatorDelegate == null) _creatorDelegate = options.MemberAccessorStrategy.CreateImmutableDictionaryCreateRangeDelegate<TValue, TCollection>(); return _creatorDelegate; } } }