| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Text.Json.Serialization.Metadata; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Converter for <cref>System.Collections.IDictionary</cref> that (de)serializes as a JSON object with properties |
| | | 13 | | /// representing the dictionary element key and value. |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class IDictionaryConverter<TDictionary> |
| | | 16 | | : JsonDictionaryConverter<TDictionary, string, object?> |
| | | 17 | | where TDictionary : IDictionary |
| | | 18 | | { |
| | 0 | 19 | | internal override bool CanPopulate => true; |
| | | 20 | | |
| | | 21 | | protected override void Add(string key, in object? value, JsonSerializerOptions options, ref ReadStack state) |
| | 0 | 22 | | { |
| | 0 | 23 | | TDictionary collection = (TDictionary)state.Current.ReturnValue!; |
| | | 24 | | |
| | 0 | 25 | | if (!options.AllowDuplicateProperties && collection.Contains(key)) |
| | 0 | 26 | | { |
| | 0 | 27 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(key); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | collection[key] = value; |
| | | 31 | | |
| | 0 | 32 | | if (IsValueType) |
| | 0 | 33 | | { |
| | 0 | 34 | | state.Current.ReturnValue = collection; |
| | 0 | 35 | | } |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state) |
| | 0 | 39 | | { |
| | 0 | 40 | | base.CreateCollection(ref reader, ref state); |
| | 0 | 41 | | TDictionary returnValue = (TDictionary)state.Current.ReturnValue!; |
| | 0 | 42 | | if (returnValue.IsReadOnly) |
| | 0 | 43 | | { |
| | 0 | 44 | | state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting. |
| | 0 | 45 | | ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); |
| | | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | protected internal override bool OnWriteResume(Utf8JsonWriter writer, TDictionary value, JsonSerializerOptions o |
| | 0 | 50 | | { |
| | | 51 | | IDictionaryEnumerator enumerator; |
| | 0 | 52 | | if (state.Current.CollectionEnumerator == null) |
| | 0 | 53 | | { |
| | 0 | 54 | | enumerator = value.GetEnumerator(); |
| | 0 | 55 | | state.Current.CollectionEnumerator = enumerator; |
| | 0 | 56 | | if (!enumerator.MoveNext()) |
| | 0 | 57 | | { |
| | 0 | 58 | | return true; |
| | | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | else |
| | 0 | 62 | | { |
| | 0 | 63 | | enumerator = (IDictionaryEnumerator)state.Current.CollectionEnumerator; |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | 0 | 67 | | _valueConverter ??= GetConverter<object?>(typeInfo.ElementTypeInfo!); |
| | | 68 | | |
| | | 69 | | do |
| | 0 | 70 | | { |
| | 0 | 71 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 72 | | { |
| | 0 | 73 | | return false; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 77 | | { |
| | 0 | 78 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | 0 | 79 | | object key = enumerator.Key; |
| | | 80 | | // Optimize for string since that's the hot path. |
| | 0 | 81 | | if (key is string keyString) |
| | 0 | 82 | | { |
| | 0 | 83 | | _keyConverter ??= GetConverter<string>(typeInfo.KeyTypeInfo!); |
| | 0 | 84 | | _keyConverter.WriteAsPropertyNameCore(writer, keyString, options, state.Current.IsWritingExtensi |
| | 0 | 85 | | } |
| | | 86 | | else |
| | 0 | 87 | | { |
| | | 88 | | // IDictionary is a special case since it has polymorphic object semantics on serialization |
| | | 89 | | // but needs to use JsonConverter<string> on deserialization. |
| | 0 | 90 | | _valueConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDa |
| | 0 | 91 | | } |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | object? element = enumerator.Value; |
| | 0 | 95 | | if (!_valueConverter.TryWrite(writer, element, options, ref state)) |
| | 0 | 96 | | { |
| | 0 | 97 | | return false; |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | state.Current.EndDictionaryEntry(); |
| | 0 | 101 | | } while (enumerator.MoveNext()); |
| | | 102 | | |
| | 0 | 103 | | return true; |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options) |
| | 0 | 107 | | { |
| | | 108 | | // Deserialize as Dictionary<TKey,TValue> for interface types that support it. |
| | 0 | 109 | | if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(Dictionary<string, object?>))) |
| | 0 | 110 | | { |
| | 0 | 111 | | Debug.Assert(Type.IsInterface); |
| | 0 | 112 | | jsonTypeInfo.CreateObject = () => new Dictionary<string, object?>(); |
| | 0 | 113 | | } |
| | 0 | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |