| | | 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.Text.Json.Serialization.Metadata; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Converter for <cref>System.Collections.IEnumerable</cref>. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="TCollection"></typeparam> |
| | | 14 | | internal sealed class IEnumerableConverter<TCollection> |
| | | 15 | | : JsonCollectionConverter<TCollection, object?> |
| | | 16 | | where TCollection : IEnumerable |
| | | 17 | | { |
| | 0 | 18 | | private readonly bool _isDeserializable = typeof(TCollection).IsAssignableFrom(typeof(List<object?>)); |
| | | 19 | | |
| | | 20 | | protected override void Add(in object? value, ref ReadStack state) |
| | 0 | 21 | | { |
| | 0 | 22 | | ((List<object?>)state.Current.ReturnValue!).Add(value); |
| | 0 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | internal override bool SupportsCreateObjectDelegate => false; |
| | | 26 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 0 | 27 | | { |
| | 0 | 28 | | if (!_isDeserializable) |
| | 0 | 29 | | { |
| | 0 | 30 | | ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | state.Current.ReturnValue = new List<object?>(); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | // Consider overriding ConvertCollection to convert the list to an array since a List is mutable. |
| | | 37 | | // However, converting from the temporary list to an array will be slower. |
| | | 38 | | |
| | | 39 | | protected override bool OnWriteResume( |
| | | 40 | | Utf8JsonWriter writer, |
| | | 41 | | TCollection value, |
| | | 42 | | JsonSerializerOptions options, |
| | | 43 | | ref WriteStack state) |
| | 0 | 44 | | { |
| | | 45 | | IEnumerator enumerator; |
| | 0 | 46 | | if (state.Current.CollectionEnumerator == null) |
| | 0 | 47 | | { |
| | 0 | 48 | | enumerator = value.GetEnumerator(); |
| | 0 | 49 | | state.Current.CollectionEnumerator = enumerator; |
| | 0 | 50 | | if (!enumerator.MoveNext()) |
| | 0 | 51 | | { |
| | 0 | 52 | | return true; |
| | | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | else |
| | 0 | 56 | | { |
| | 0 | 57 | | enumerator = state.Current.CollectionEnumerator; |
| | 0 | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | JsonConverter<object?> converter = GetElementConverter(ref state); |
| | | 61 | | do |
| | 0 | 62 | | { |
| | 0 | 63 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 64 | | { |
| | 0 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | object? element = enumerator.Current; |
| | 0 | 69 | | if (!converter.TryWrite(writer, element, options, ref state)) |
| | 0 | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | state.Current.EndCollectionElement(); |
| | 0 | 75 | | } while (enumerator.MoveNext()); |
| | | 76 | | |
| | 0 | 77 | | return true; |
| | 0 | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |