| | | 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 | | /// Converter for <cref>System.Collections.Generic.List{TElement}</cref>. |
| | | 10 | | internal sealed class ListOfTConverter<TCollection, TElement> |
| | | 11 | | : IEnumerableDefaultConverter<TCollection, TElement> |
| | | 12 | | where TCollection : List<TElement> |
| | | 13 | | { |
| | 0 | 14 | | internal override bool CanPopulate => true; |
| | | 15 | | |
| | | 16 | | protected override void Add(in TElement value, ref ReadStack state) |
| | 10 | 17 | | { |
| | 10 | 18 | | ((TCollection)state.Current.ReturnValue!).Add(value); |
| | 10 | 19 | | } |
| | | 20 | | |
| | | 21 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 212 | 22 | | { |
| | 212 | 23 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 24 | | { |
| | 0 | 25 | | return; |
| | | 26 | | } |
| | | 27 | | |
| | 212 | 28 | | if (state.Current.JsonTypeInfo.CreateObject == null) |
| | 0 | 29 | | { |
| | 0 | 30 | | ThrowHelper.ThrowNotSupportedException_SerializationNotSupported(state.Current.JsonTypeInfo.Type); |
| | | 31 | | } |
| | | 32 | | |
| | 212 | 33 | | state.Current.ReturnValue = state.Current.JsonTypeInfo.CreateObject(); |
| | 212 | 34 | | } |
| | | 35 | | |
| | | 36 | | protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, r |
| | 0 | 37 | | { |
| | 0 | 38 | | List<TElement> list = value; |
| | | 39 | | |
| | | 40 | | // Using an index is 2x faster than using an enumerator. |
| | 0 | 41 | | int index = state.Current.EnumeratorIndex; |
| | 0 | 42 | | JsonConverter<TElement> elementConverter = GetElementConverter(ref state); |
| | | 43 | | |
| | 0 | 44 | | if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) |
| | 0 | 45 | | { |
| | | 46 | | // Fast path that avoids validation and extra indirection. |
| | 0 | 47 | | for (; index < list.Count; index++) |
| | 0 | 48 | | { |
| | 0 | 49 | | elementConverter.Write(writer, list[index], options); |
| | 0 | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | else |
| | 0 | 53 | | { |
| | 0 | 54 | | for (; index < list.Count; index++) |
| | 0 | 55 | | { |
| | 0 | 56 | | TElement element = list[index]; |
| | 0 | 57 | | if (!elementConverter.TryWrite(writer, element, options, ref state)) |
| | 0 | 58 | | { |
| | 0 | 59 | | state.Current.EnumeratorIndex = index; |
| | 0 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | state.Current.EndCollectionElement(); |
| | | 64 | | |
| | 0 | 65 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 66 | | { |
| | 0 | 67 | | state.Current.EnumeratorIndex = ++index; |
| | 0 | 68 | | return false; |
| | | 69 | | } |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return true; |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |