| | | 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 | | using System.Diagnostics; |
| | | 7 | | using System.Text.Json.Serialization.Metadata; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization.Converters |
| | | 10 | | { |
| | | 11 | | /// Converter for <cref>System.Collections.IList</cref>. |
| | | 12 | | internal sealed class IListConverter<TCollection> |
| | | 13 | | : JsonCollectionConverter<TCollection, object?> |
| | | 14 | | where TCollection : IList |
| | | 15 | | { |
| | 0 | 16 | | internal override bool CanPopulate => true; |
| | | 17 | | |
| | | 18 | | protected override void Add(in object? value, ref ReadStack state) |
| | 0 | 19 | | { |
| | 0 | 20 | | TCollection collection = (TCollection)state.Current.ReturnValue!; |
| | 0 | 21 | | collection.Add(value); |
| | 0 | 22 | | if (IsValueType) |
| | 0 | 23 | | { |
| | 0 | 24 | | state.Current.ReturnValue = collection; |
| | 0 | 25 | | } |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 0 | 29 | | { |
| | 0 | 30 | | base.CreateCollection(ref reader, ref state, options); |
| | 0 | 31 | | TCollection returnValue = (TCollection)state.Current.ReturnValue!; |
| | 0 | 32 | | if (returnValue.IsReadOnly) |
| | 0 | 33 | | { |
| | 0 | 34 | | state.Current.ReturnValue = null; // clear out for more accurate JsonPath reporting. |
| | 0 | 35 | | ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); |
| | | 36 | | } |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | protected override bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, r |
| | 0 | 40 | | { |
| | 0 | 41 | | IList list = value; |
| | | 42 | | |
| | | 43 | | // Using an index is 2x faster than using an enumerator. |
| | 0 | 44 | | int index = state.Current.EnumeratorIndex; |
| | 0 | 45 | | JsonConverter<object?> elementConverter = GetElementConverter(ref state); |
| | | 46 | | |
| | 0 | 47 | | if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null) |
| | 0 | 48 | | { |
| | | 49 | | // Fast path that avoids validation and extra indirection. |
| | 0 | 50 | | for (; index < list.Count; index++) |
| | 0 | 51 | | { |
| | 0 | 52 | | elementConverter.Write(writer, list[index], options); |
| | 0 | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | else |
| | 0 | 56 | | { |
| | 0 | 57 | | for (; index < list.Count; index++) |
| | 0 | 58 | | { |
| | 0 | 59 | | object? element = list[index]; |
| | 0 | 60 | | if (!elementConverter.TryWrite(writer, element, options, ref state)) |
| | 0 | 61 | | { |
| | 0 | 62 | | state.Current.EnumeratorIndex = index; |
| | 0 | 63 | | return false; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | state.Current.EndCollectionElement(); |
| | | 67 | | |
| | 0 | 68 | | if (ShouldFlush(ref state, writer)) |
| | 0 | 69 | | { |
| | 0 | 70 | | state.Current.EnumeratorIndex = ++index; |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | 0 | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return true; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options) |
| | 0 | 80 | | { |
| | | 81 | | // Deserialize as List<object?> for interface types that support it. |
| | 0 | 82 | | if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(List<object?>))) |
| | 0 | 83 | | { |
| | 0 | 84 | | Debug.Assert(Type.IsInterface); |
| | 0 | 85 | | jsonTypeInfo.CreateObject = () => new List<object?>(); |
| | 0 | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |