| | | 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 | | /// Default base class implementation of <cref>JsonIEnumerableConverter{TCollection, TElement}</cref>. |
| | | 11 | | /// </summary> |
| | | 12 | | internal abstract class IEnumerableDefaultConverter<TCollection, TElement> : JsonCollectionConverter<TCollection, TE |
| | | 13 | | where TCollection : IEnumerable<TElement> |
| | | 14 | | { |
| | 0 | 15 | | internal override bool CanHaveMetadata => true; |
| | | 16 | | |
| | | 17 | | protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, r |
| | 0 | 18 | | { |
| | 0 | 19 | | Debug.Assert(value is not null); |
| | | 20 | | |
| | | 21 | | IEnumerator<TElement> enumerator; |
| | 0 | 22 | | if (state.Current.CollectionEnumerator == null) |
| | 0 | 23 | | { |
| | 0 | 24 | | enumerator = value.GetEnumerator(); |
| | 0 | 25 | | state.Current.CollectionEnumerator = enumerator; |
| | 0 | 26 | | if (!enumerator.MoveNext()) |
| | 0 | 27 | | { |
| | 0 | 28 | | enumerator.Dispose(); |
| | 0 | 29 | | return true; |
| | | 30 | | } |
| | 0 | 31 | | } |
| | | 32 | | else |
| | 0 | 33 | | { |
| | 0 | 34 | | Debug.Assert(state.Current.CollectionEnumerator is IEnumerator<TElement>); |
| | 0 | 35 | | enumerator = (IEnumerator<TElement>)state.Current.CollectionEnumerator; |
| | 0 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | JsonConverter<TElement> converter = GetElementConverter(ref state); |
| | | 39 | | do |
| | 0 | 40 | | { |
| | 0 | 41 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 42 | | { |
| | 0 | 43 | | return false; |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | TElement element = enumerator.Current; |
| | 0 | 47 | | if (!converter.TryWrite(writer, element, options, ref state)) |
| | 0 | 48 | | { |
| | 0 | 49 | | return false; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | state.Current.EndCollectionElement(); |
| | 0 | 53 | | } while (enumerator.MoveNext()); |
| | | 54 | | |
| | 0 | 55 | | enumerator.Dispose(); |
| | 0 | 56 | | return true; |
| | 0 | 57 | | } |
| | | 58 | | } |
| | | 59 | | } |