FSharpSetConverter<TSet, TElement>
using System.
Collections.
Generic;
using System.
Diagnostics.
CodeAnalysis;
using System.
Runtime.
CompilerServices;
using System.
Runtime.
InteropServices;
using System.
Text.
Json.
Serialization.
Metadata;
namespace System.
Text.
Json.
Serialization.
Converters
{
internal sealed class FSharpSetConverter<
TSet,
TElement> :
IEnumerableDefaultConverter<
TSet,
TElement>
where TSet :
IEnumerable<
TElement>
{
private readonly Func<
IEnumerable<
TElement>,
TSet>
_setConstructor;
internal override bool SupportsCreateObjectDelegate =>
false;
[
System.
Diagnostics.
CodeAnalysis.
RequiresUnreferencedCode(
"Uses Reflection to access FSharp.Core components at runtime.")]
[
System.
Diagnostics.
CodeAnalysis.
RequiresDynamicCode(
"Uses Reflection to access FSharp.Core components at runtime.")]
public FSharpSetConverter()
{
_setConstructor =
FSharpCoreReflectionProxy.
Instance.
CreateFSharpSetConstructor<
TSet,
TElement>();
}
protected override void Add([
In] [
System.
Runtime.
CompilerServices.
IsReadOnly]
TElement value,
ref ReadStack state)
{
((
List<
TElement>)
state.
Current.
ReturnValue).
Add(
value);
}
protected override void CreateCollection(
ref Utf8JsonReader reader, [
System.
Runtime.
CompilerServices.
ScopedRef]
ref ReadStack state,
JsonSerializerOptions options)
{
state.
Current.
ReturnValue =
new List<
TElement>();
}
protected override void ConvertCollection(
ref ReadStack state,
JsonSerializerOptions options)
{
state.
Current.
ReturnValue =
_setConstructor((
List<
TElement>)
state.
Current.
ReturnValue);
}
}
}