<PackageReference Include="System.Text.Json" Version="8.0.0-preview.5.23280.8" />

StackOrQueueConverter<TCollection>

class StackOrQueueConverter<TCollection> : JsonCollectionConverter<TCollection, object> where TCollection : IEnumerable
using System.Collections; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text.Json.Serialization.Metadata; namespace System.Text.Json.Serialization.Converters { internal class StackOrQueueConverter<TCollection> : JsonCollectionConverter<TCollection, object> where TCollection : IEnumerable { internal override bool CanPopulate => true; protected sealed override void Add([In] [IsReadOnly] object value, ref ReadStack state) { Action<TCollection, object> action = (Action<TCollection, object>)state.Current.JsonTypeInfo.AddMethodDelegate; action((TCollection)state.Current.ReturnValue, value); } protected sealed override void CreateCollection(ref Utf8JsonReader reader, [System.Runtime.CompilerServices.ScopedRef] ref ReadStack state, JsonSerializerOptions options) { if (!(state.ParentProperty?.TryGetPrePopulatedValue(ref state) ?? false)) { JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; Func<object> createObject = jsonTypeInfo.CreateObject; if (createObject == null) ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state); state.Current.ReturnValue = createObject(); } } protected sealed override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state) { IEnumerator enumerator; if (state.Current.CollectionEnumerator == null) { enumerator = value.GetEnumerator(); if (!enumerator.MoveNext()) return true; } else enumerator = state.Current.CollectionEnumerator; JsonConverter<object> elementConverter = JsonCollectionConverter<TCollection, object>.GetElementConverter(ref state); do { if (JsonConverter.ShouldFlush(writer, ref state)) { state.Current.CollectionEnumerator = enumerator; return false; } object value2 = enumerator.Current; if (!elementConverter.TryWrite(writer, ref value2, options, ref state)) { state.Current.CollectionEnumerator = enumerator; return false; } state.Current.EndCollectionElement(); } while (enumerator.MoveNext()); return true; } } }