| | | 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.Text.Json.Serialization.Metadata; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization.Converters |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Converter for Dictionary{string, TValue} that (de)serializes as a JSON object with properties |
| | | 11 | | /// representing the dictionary element key and value. |
| | | 12 | | /// </summary> |
| | | 13 | | internal sealed class DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue> |
| | | 14 | | : DictionaryDefaultConverter<TCollection, TKey, TValue> |
| | | 15 | | where TCollection : Dictionary<TKey, TValue> |
| | | 16 | | where TKey : notnull |
| | | 17 | | { |
| | 0 | 18 | | internal override bool CanPopulate => true; |
| | | 19 | | |
| | | 20 | | protected override void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state) |
| | 0 | 21 | | { |
| | 0 | 22 | | TCollection dictionary = (TCollection)state.Current.ReturnValue!; |
| | | 23 | | |
| | 0 | 24 | | if (options.AllowDuplicateProperties) |
| | 0 | 25 | | { |
| | 0 | 26 | | dictionary[key] = value; |
| | 0 | 27 | | } |
| | | 28 | | else |
| | 0 | 29 | | { |
| | 0 | 30 | | if (!dictionary.TryAdd(key, value)) |
| | 0 | 31 | | { |
| | 0 | 32 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(); |
| | | 33 | | } |
| | 0 | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | protected internal override bool OnWriteResume( |
| | | 38 | | Utf8JsonWriter writer, |
| | | 39 | | TCollection value, |
| | | 40 | | JsonSerializerOptions options, |
| | | 41 | | ref WriteStack state) |
| | 0 | 42 | | { |
| | | 43 | | Dictionary<TKey, TValue>.Enumerator enumerator; |
| | 0 | 44 | | if (state.Current.CollectionEnumerator == null) |
| | 0 | 45 | | { |
| | 0 | 46 | | enumerator = value.GetEnumerator(); |
| | 0 | 47 | | if (!enumerator.MoveNext()) |
| | 0 | 48 | | { |
| | 0 | 49 | | enumerator.Dispose(); |
| | 0 | 50 | | return true; |
| | | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | else |
| | 0 | 54 | | { |
| | 0 | 55 | | enumerator = (Dictionary<TKey, TValue>.Enumerator)state.Current.CollectionEnumerator; |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | 0 | 59 | | _keyConverter ??= GetConverter<TKey>(typeInfo.KeyTypeInfo!); |
| | 0 | 60 | | _valueConverter ??= GetConverter<TValue>(typeInfo.ElementTypeInfo!); |
| | | 61 | | |
| | 0 | 62 | | if (!state.SupportContinuation && _valueConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == |
| | 0 | 63 | | { |
| | | 64 | | // Fast path that avoids validation and extra indirection. |
| | | 65 | | do |
| | 0 | 66 | | { |
| | 0 | 67 | | TKey key = enumerator.Current.Key; |
| | 0 | 68 | | _keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProp |
| | 0 | 69 | | _valueConverter.Write(writer, enumerator.Current.Value, options); |
| | 0 | 70 | | } while (enumerator.MoveNext()); |
| | 0 | 71 | | } |
| | | 72 | | else |
| | 0 | 73 | | { |
| | | 74 | | do |
| | 0 | 75 | | { |
| | 0 | 76 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 77 | | { |
| | 0 | 78 | | state.Current.CollectionEnumerator = enumerator; |
| | 0 | 79 | | return false; |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 83 | | { |
| | 0 | 84 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | | 85 | | |
| | 0 | 86 | | TKey key = enumerator.Current.Key; |
| | 0 | 87 | | _keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionData |
| | 0 | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | TValue element = enumerator.Current.Value; |
| | 0 | 91 | | if (!_valueConverter.TryWrite(writer, element, options, ref state)) |
| | 0 | 92 | | { |
| | 0 | 93 | | state.Current.CollectionEnumerator = enumerator; |
| | 0 | 94 | | return false; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | state.Current.EndDictionaryEntry(); |
| | 0 | 98 | | } while (enumerator.MoveNext()); |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | enumerator.Dispose(); |
| | 0 | 102 | | return true; |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |