<PackageReference Include="System.Text.Json" Version="5.0.0" />

ConcurrentStackOfTConverter<TCollection, TElement>

sealed class ConcurrentStackOfTConverter<TCollection, TElement> : IEnumerableDefaultConverter<TCollection, TElement> where TCollection : ConcurrentStack<TElement>
using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace System.Text.Json.Serialization.Converters { internal sealed class ConcurrentStackOfTConverter<TCollection, TElement> : IEnumerableDefaultConverter<TCollection, TElement> where TCollection : ConcurrentStack<TElement> { protected override void Add([In] [System.Runtime.CompilerServices.IsReadOnly] TElement value, ref ReadStack state) { ((TCollection)state.Current.ReturnValue).Push(value); } protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options) { if (state.Current.JsonClassInfo.CreateObject == null) ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonClassInfo.Type); state.Current.ReturnValue = state.Current.JsonClassInfo.CreateObject(); } protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state) { IEnumerator<TElement> enumerator; if (state.Current.CollectionEnumerator == null) { enumerator = value.GetEnumerator(); if (!enumerator.MoveNext()) return true; } else enumerator = (IEnumerator<TElement>)state.Current.CollectionEnumerator; JsonConverter<TElement> elementConverter = IEnumerableDefaultConverter<TCollection, TElement>.GetElementConverter(ref state); do { if (ShouldFlush(writer, ref state)) { state.Current.CollectionEnumerator = enumerator; return false; } TElement value2 = enumerator.Current; if (!elementConverter.TryWrite(writer, ref value2, options, ref state)) { state.Current.CollectionEnumerator = enumerator; return false; } } while (enumerator.MoveNext()); return true; } } }