| | | 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.Generic; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Text.Json.Serialization.Metadata; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | // Converter for F# sets: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-fsharpset-1.html |
| | | 11 | | internal sealed class FSharpSetConverter<TSet, TElement> : IEnumerableDefaultConverter<TSet, TElement> |
| | | 12 | | where TSet : IEnumerable<TElement> |
| | | 13 | | { |
| | | 14 | | private readonly Func<IEnumerable<TElement>, TSet> _setConstructor; |
| | | 15 | | |
| | | 16 | | [RequiresUnreferencedCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)] |
| | | 17 | | [RequiresDynamicCode(FSharpCoreReflectionProxy.FSharpCoreUnreferencedCodeMessage)] |
| | 0 | 18 | | public FSharpSetConverter() |
| | 0 | 19 | | { |
| | 0 | 20 | | _setConstructor = FSharpCoreReflectionProxy.Instance.CreateFSharpSetConstructor<TSet, TElement>(); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | protected override void Add(in TElement value, ref ReadStack state) |
| | 0 | 24 | | { |
| | 0 | 25 | | ((List<TElement>)state.Current.ReturnValue!).Add(value); |
| | 0 | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | internal override bool SupportsCreateObjectDelegate => false; |
| | | 29 | | protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOp |
| | 0 | 30 | | { |
| | 0 | 31 | | state.Current.ReturnValue = new List<TElement>(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | internal sealed override bool IsConvertibleCollection => true; |
| | | 35 | | protected override void ConvertCollection(ref ReadStack state, JsonSerializerOptions options) |
| | 0 | 36 | | { |
| | 0 | 37 | | state.Current.ReturnValue = _setConstructor((List<TElement>)state.Current.ReturnValue!); |
| | 0 | 38 | | } |
| | | 39 | | } |
| | | 40 | | } |