ISetOfTConverter<TCollection, TElement>
sealed class ISetOfTConverter<TCollection, TElement> : IEnumerableDefaultConverter<TCollection, TElement> where TCollection : ISet<TElement>
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization.Converters
{
internal sealed class ISetOfTConverter<TCollection, TElement> : IEnumerableDefaultConverter<TCollection, TElement> where TCollection : ISet<TElement>
{
internal override Type RuntimeType {
get {
if (TypeToConvert.IsAbstract || TypeToConvert.IsInterface)
return typeof(HashSet<TElement>);
return TypeToConvert;
}
}
protected override void Add([In] [System.Runtime.CompilerServices.IsReadOnly] TElement value, ref ReadStack state)
{
TCollection val = (TCollection)state.Current.ReturnValue;
val.Add(value);
if (base.IsValueType)
state.Current.ReturnValue = val;
}
protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;
if (TypeToConvert.IsInterface || TypeToConvert.IsAbstract) {
if (!TypeToConvert.IsAssignableFrom(RuntimeType))
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
state.Current.ReturnValue = new HashSet<TElement>();
} else {
if (jsonTypeInfo.CreateObject == null)
ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(TypeToConvert, ref reader, ref state);
TCollection val = (TCollection)jsonTypeInfo.CreateObject();
if (val.IsReadOnly)
ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(TypeToConvert, ref reader, ref state);
state.Current.ReturnValue = val;
}
}
}
}