| | | 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.Collections.Generic; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization.Converters |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Converter for <cref>System.Array</cref>. |
| | | 11 | | /// </summary> |
| | | 12 | | internal sealed class ArrayConverter<TCollection, TElement> : IEnumerableDefaultConverter<TElement[], TElement> |
| | | 13 | | { |
| | 0 | 14 | | internal override bool CanHaveMetadata => false; |
| | | 15 | | |
| | | 16 | | protected override void Add(in TElement value, ref ReadStack state) |
| | 0 | 17 | | { |
| | 0 | 18 | | ((List<TElement>)state.Current.ReturnValue!).Add(value); |
| | 0 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | internal override bool SupportsCreateObjectDelegate => false; |
| | | 22 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 0 | 23 | | { |
| | 0 | 24 | | state.Current.ReturnValue = new List<TElement>(); |
| | 0 | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | internal sealed override bool IsConvertibleCollection => true; |
| | | 28 | | protected override void ConvertCollection(ref ReadStack state, JsonSerializerOptions options) |
| | 0 | 29 | | { |
| | 0 | 30 | | List<TElement> list = (List<TElement>)state.Current.ReturnValue!; |
| | 0 | 31 | | state.Current.ReturnValue = list.ToArray(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | protected override bool OnWriteResume(Utf8JsonWriter writer, TElement[] array, JsonSerializerOptions options, re |
| | 0 | 35 | | { |
| | 0 | 36 | | int index = state.Current.EnumeratorIndex; |
| | | 37 | | |
| | 0 | 38 | | JsonConverter<TElement> elementConverter = GetElementConverter(ref state); |
| | 0 | 39 | | if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) |
| | 0 | 40 | | { |
| | | 41 | | // Fast path that avoids validation and extra indirection. |
| | 0 | 42 | | for (; index < array.Length; index++) |
| | 0 | 43 | | { |
| | 0 | 44 | | elementConverter.Write(writer, array[index], options); |
| | 0 | 45 | | } |
| | 0 | 46 | | } |
| | | 47 | | else |
| | 0 | 48 | | { |
| | 0 | 49 | | for (; index < array.Length; index++) |
| | 0 | 50 | | { |
| | 0 | 51 | | TElement element = array[index]; |
| | 0 | 52 | | if (!elementConverter.TryWrite(writer, element, options, ref state)) |
| | 0 | 53 | | { |
| | 0 | 54 | | state.Current.EnumeratorIndex = index; |
| | 0 | 55 | | return false; |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | state.Current.EndCollectionElement(); |
| | | 59 | | |
| | 0 | 60 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 61 | | { |
| | 0 | 62 | | state.Current.EnumeratorIndex = ++index; |
| | 0 | 63 | | return false; |
| | | 64 | | } |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return true; |
| | 0 | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |