Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Jan 24, 2024
1 parent 39cd706 commit 277a107
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/FsCodec.SystemTextJson/UnionOrTypeSafeEnumConverterFactory.fs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
namespace FsCodec.SystemTextJson

open System
open System.Linq.Expressions
open System.Text.Json.Serialization

type internal ConverterActivator = delegate of unit -> JsonConverter

type UnionOrTypeSafeEnumConverterFactory(typeSafeEnum, union) =
inherit JsonConverterFactory()

static let hasConverterAttribute = memoize (fun (t: Type) -> t.IsDefined(typeof<JsonConverterAttribute>, true))
static let hasConverterAttribute = memoize (fun (t: System.Type) -> t.IsDefined(typeof<JsonConverterAttribute>, true))

override _.CanConvert(t: Type) =
override _.CanConvert t =
not (t.IsGenericType && let gtd = t.GetGenericTypeDefinition() in gtd = typedefof<option<_>> || gtd = typedefof<list<_>>)
&& FsCodec.Union.isUnion t
&& not (hasConverterAttribute t)
Expand All @@ -20,9 +16,4 @@ type UnionOrTypeSafeEnumConverterFactory(typeSafeEnum, union) =

override _.CreateConverter(t, _options) =
let openConverterType = if FsCodec.Union.isNullary t then typedefof<TypeSafeEnumConverter<_>> else typedefof<UnionConverter<_>>
let constructor = openConverterType.MakeGenericType(t).GetConstructors() |> Array.head
let newExpression = Expression.New(constructor)
let lambda = Expression.Lambda(typeof<ConverterActivator>, newExpression)

let activator = lambda.Compile() :?> ConverterActivator
activator.Invoke()
openConverterType.MakeGenericType(t).GetConstructors().[0].Invoke[||] :?> _

0 comments on commit 277a107

Please sign in to comment.