| | | 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.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization.Converters |
| | | 8 | | { |
| | | 9 | | internal sealed class StackOfTConverter<TCollection, TElement> |
| | | 10 | | : IEnumerableDefaultConverter<TCollection, TElement> |
| | | 11 | | where TCollection : Stack<TElement> |
| | | 12 | | { |
| | 0 | 13 | | internal override bool CanPopulate => true; |
| | | 14 | | |
| | | 15 | | protected override void Add(in TElement value, ref ReadStack state) |
| | 0 | 16 | | { |
| | 0 | 17 | | ((TCollection)state.Current.ReturnValue!).Push(value); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 0 | 21 | | { |
| | 0 | 22 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 23 | | { |
| | 0 | 24 | | return; |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | if (state.Current.JsonTypeInfo.CreateObject == null) |
| | 0 | 28 | | { |
| | 0 | 29 | | ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type); |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject(); |
| | 0 | 33 | | } |
| | | 34 | | } |
| | | 35 | | } |