| | | 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 | | |
| | | 7 | | namespace System.Text.Json.Serialization.Converters |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Converter for <cref>System.Collections.Generic.IEnumerable{TElement}</cref>. |
| | | 11 | | /// </summary> |
| | | 12 | | internal sealed class IEnumerableOfTConverter<TCollection, TElement> |
| | | 13 | | : IEnumerableDefaultConverter<TCollection, TElement> |
| | | 14 | | where TCollection : IEnumerable<TElement> |
| | | 15 | | { |
| | 0 | 16 | | private readonly bool _isDeserializable = typeof(TCollection).IsAssignableFrom(typeof(List<TElement>)); |
| | | 17 | | |
| | | 18 | | protected override void Add(in TElement value, ref ReadStack state) |
| | 0 | 19 | | { |
| | 0 | 20 | | ((List<TElement>)state.Current.ReturnValue!).Add(value); |
| | 0 | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | internal override bool SupportsCreateObjectDelegate => false; |
| | | 24 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 0 | 25 | | { |
| | 0 | 26 | | if (!_isDeserializable) |
| | 0 | 27 | | { |
| | 0 | 28 | | ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | state.Current.ReturnValue = new List<TElement>(); |
| | 0 | 32 | | } |
| | | 33 | | } |
| | | 34 | | } |