| | | 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.Text.Json.Serialization.Metadata; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Converter for <cref>System.Collections.Generic.IDictionary{TKey, TValue}</cref> that |
| | | 12 | | /// (de)serializes as a JSON object with properties representing the dictionary element key and value. |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class IDictionaryOfTKeyTValueConverter<TDictionary, TKey, TValue> |
| | | 15 | | : DictionaryDefaultConverter<TDictionary, TKey, TValue> |
| | | 16 | | where TDictionary : IDictionary<TKey, TValue> |
| | | 17 | | where TKey : notnull |
| | | 18 | | { |
| | 0 | 19 | | internal override bool CanPopulate => true; |
| | | 20 | | |
| | | 21 | | protected override void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state) |
| | 0 | 22 | | { |
| | 0 | 23 | | TDictionary collection = (TDictionary)state.Current.ReturnValue!; |
| | | 24 | | |
| | 0 | 25 | | if (options.AllowDuplicateProperties) |
| | 0 | 26 | | { |
| | 0 | 27 | | collection[key] = value; |
| | 0 | 28 | | } |
| | | 29 | | else |
| | 0 | 30 | | { |
| | 0 | 31 | | if (!collection.TryAdd(key, value)) |
| | 0 | 32 | | { |
| | 0 | 33 | | ThrowHelper.ThrowJsonException_DuplicatePropertyNotAllowed(); |
| | | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | if (IsValueType) |
| | 0 | 38 | | { |
| | 0 | 39 | | state.Current.ReturnValue = collection; |
| | 0 | 40 | | }; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state) |
| | 0 | 44 | | { |
| | 0 | 45 | | base.CreateCollection(ref reader, ref state); |
| | 0 | 46 | | TDictionary returnValue = (TDictionary)state.Current.ReturnValue!; |
| | 0 | 47 | | if (returnValue.IsReadOnly) |
| | 0 | 48 | | { |
| | 0 | 49 | | state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting. |
| | 0 | 50 | | ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); |
| | | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options) |
| | 0 | 55 | | { |
| | | 56 | | // Deserialize as Dictionary<TKey,TValue> for interface types that support it. |
| | 0 | 57 | | if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(Dictionary<TKey, TValue>))) |
| | 0 | 58 | | { |
| | 0 | 59 | | Debug.Assert(Type.IsInterface); |
| | 0 | 60 | | jsonTypeInfo.CreateObject = () => new Dictionary<TKey, TValue>(); |
| | 0 | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |