| | | 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.Diagnostics; |
| | | 6 | | using System.Text.Json.Serialization.Metadata; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | internal class StackOrQueueConverter<TCollection> |
| | | 11 | | : JsonCollectionConverter<TCollection, object?> |
| | | 12 | | where TCollection : IEnumerable |
| | | 13 | | { |
| | 0 | 14 | | internal override bool CanPopulate => true; |
| | | 15 | | |
| | | 16 | | protected sealed override void Add(in object? value, ref ReadStack state) |
| | 0 | 17 | | { |
| | 0 | 18 | | var addMethodDelegate = ((Action<TCollection, object?>?)state.Current.JsonTypeInfo.AddMethodDelegate); |
| | 0 | 19 | | Debug.Assert(addMethodDelegate != null); |
| | 0 | 20 | | addMethodDelegate((TCollection)state.Current.ReturnValue!, value); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | protected sealed override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSeria |
| | 0 | 24 | | { |
| | 0 | 25 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 26 | | { |
| | 0 | 27 | | return; |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | 0 | 31 | | Func<object>? constructorDelegate = typeInfo.CreateObject; |
| | | 32 | | |
| | 0 | 33 | | if (constructorDelegate == null) |
| | 0 | 34 | | { |
| | 0 | 35 | | ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | state.Current.ReturnValue = constructorDelegate(); |
| | | 39 | | |
| | 0 | 40 | | Debug.Assert(typeInfo.AddMethodDelegate != null); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | protected sealed override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions opt |
| | 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 | | } |