ImmutableEnumerableOfTConverter<TCollection, TElement>
using System.
Collections.
Generic;
using System.
Runtime.
CompilerServices;
using System.
Runtime.
InteropServices;
namespace System.
Text.
Json.
Serialization.
Converters
{
internal class ImmutableEnumerableOfTConverter<
TCollection,
TElement> :
IEnumerableDefaultConverter<
TCollection,
TElement>
where TCollection :
IEnumerable<
TElement>
{
internal sealed override bool CanHaveMetadata =>
false;
internal override bool SupportsCreateObjectDelegate =>
false;
internal sealed override bool IsConvertibleCollection =>
true;
protected sealed override void Add([
In] [
System.
Runtime.
CompilerServices.
IsReadOnly]
TElement value,
ref ReadStack state)
{
((
List<
TElement>)
state.
Current.
ReturnValue).
Add(
value);
}
protected sealed override void CreateCollection(
ref Utf8JsonReader reader, [
System.
Runtime.
CompilerServices.
ScopedRef]
ref ReadStack state,
JsonSerializerOptions options)
{
state.
Current.
ReturnValue =
new List<
TElement>();
}
protected sealed override void ConvertCollection(
ref ReadStack state,
JsonSerializerOptions options)
{
Func<
IEnumerable<
TElement>,
TCollection>
func = (
Func<
IEnumerable<
TElement>,
TCollection>)
state.
Current.
JsonTypeInfo.
CreateObjectWithArgs;
state.
Current.
ReturnValue =
func((
List<
TElement>)
state.
Current.
ReturnValue);
}
}
}