| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Text.Json.Serialization.Metadata; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Default base class implementation of <cref>JsonDictionaryConverter{TCollection}</cref> . |
| | | 13 | | /// </summary> |
| | | 14 | | internal abstract class DictionaryDefaultConverter<TDictionary, TKey, TValue> |
| | | 15 | | : JsonDictionaryConverter<TDictionary, TKey, TValue> |
| | | 16 | | where TDictionary : IEnumerable<KeyValuePair<TKey, TValue>> |
| | | 17 | | where TKey : notnull |
| | | 18 | | { |
| | 0 | 19 | | internal override bool CanHaveMetadata => true; |
| | | 20 | | |
| | | 21 | | protected internal override bool OnWriteResume( |
| | | 22 | | Utf8JsonWriter writer, |
| | | 23 | | TDictionary value, |
| | | 24 | | JsonSerializerOptions options, |
| | | 25 | | ref WriteStack state) |
| | 0 | 26 | | { |
| | | 27 | | IEnumerator<KeyValuePair<TKey, TValue>> enumerator; |
| | 0 | 28 | | if (state.Current.CollectionEnumerator == null) |
| | 0 | 29 | | { |
| | 0 | 30 | | enumerator = value.GetEnumerator(); |
| | 0 | 31 | | state.Current.CollectionEnumerator = enumerator; |
| | 0 | 32 | | if (!enumerator.MoveNext()) |
| | 0 | 33 | | { |
| | 0 | 34 | | enumerator.Dispose(); |
| | 0 | 35 | | return true; |
| | | 36 | | } |
| | 0 | 37 | | } |
| | | 38 | | else |
| | 0 | 39 | | { |
| | 0 | 40 | | enumerator = (IEnumerator<KeyValuePair<TKey, TValue>>)state.Current.CollectionEnumerator; |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | 0 | 44 | | _keyConverter ??= GetConverter<TKey>(typeInfo.KeyTypeInfo!); |
| | 0 | 45 | | _valueConverter ??= GetConverter<TValue>(typeInfo.ElementTypeInfo!); |
| | | 46 | | |
| | | 47 | | do |
| | 0 | 48 | | { |
| | 0 | 49 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 50 | | { |
| | 0 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 55 | | { |
| | 0 | 56 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | 0 | 57 | | TKey key = enumerator.Current.Key; |
| | 0 | 58 | | _keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProp |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | TValue element = enumerator.Current.Value; |
| | 0 | 62 | | if (!_valueConverter.TryWrite(writer, element, options, ref state)) |
| | 0 | 63 | | { |
| | 0 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | state.Current.EndDictionaryEntry(); |
| | 0 | 68 | | } while (enumerator.MoveNext()); |
| | | 69 | | |
| | 0 | 70 | | enumerator.Dispose(); |
| | 0 | 71 | | return true; |
| | 0 | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |