Skip to content

Commit

Permalink
Trim Picker logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Jan 27, 2024
1 parent 790f8fb commit 097c9ec
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 43 deletions.
21 changes: 1 addition & 20 deletions src/FsCodec.NewtonsoftJson/Pickler.fs
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
namespace FsCodec.NewtonsoftJson

open Newtonsoft.Json
open System

[<AutoOpen>]
module private Prelude =
let memoize (f: 'T -> 'S): 'T -> 'S =
let cache = new System.Collections.Concurrent.ConcurrentDictionary<'T, 'S>()
fun t -> cache.GetOrAdd(t, f)

[<AbstractClass>]
type JsonPickler<'T>() =
inherit JsonConverter()

static let isMatchingType =
let rec isMatching = function
| [] -> false
| t :: _ when t = typeof<'T> -> true
| t :: tl ->
let tail =
[ match t.BaseType with null -> () | bt -> yield bt
yield! t.GetInterfaces()
yield! tl ]
isMatching tail
memoize (fun t -> isMatching [t])

abstract Write: writer: JsonWriter * serializer: JsonSerializer * source: 'T -> unit
abstract Read: reader: JsonReader * serializer: JsonSerializer -> 'T

override _.CanConvert t = isMatchingType t
override _.CanConvert t = t = typeof<'T>
override _.CanRead = true
override _.CanWrite = true

Expand Down
6 changes: 4 additions & 2 deletions src/FsCodec.NewtonsoftJson/UnionConverter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ module private UnionInfo =
|| t.IsArray
|| (t.IsGenericType && let g = t.GetGenericTypeDefinition() in typedefof<Option<_>> = g || g.IsValueType) // Nullable<T>

let typeHasConverterAttribute = memoize (fun (t: Type) -> t.IsDefined(typeof<JsonConverterAttribute>))
let typeIsUnionWithConverterAttribute = memoize (fun (t: Type) -> FsCodec.Union.isUnion t && typeHasConverterAttribute t)
let hasConverterCache = System.Collections.Concurrent.ConcurrentDictionary<Type, bool>()
let typeHasConverterAttribute (t: Type) = hasConverterCache.GetOrAdd(t, fun t -> t.IsDefined(typeof<JsonConverterAttribute>, ``inherit`` = false))
let isUnionCache = System.Collections.Concurrent.ConcurrentDictionary<Type, bool>()
let typeIsUnionWithConverterAttribute t = isUnionCache.GetOrAdd(t, fun t -> FsCodec.Union.isUnion t && typeHasConverterAttribute t)

let propTypeRequiresConstruction (propertyType: Type) =
not (isInlinedIntoUnionItem propertyType)
Expand Down
20 changes: 0 additions & 20 deletions src/FsCodec.SystemTextJson/Pickler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@

open System.Text.Json

[<AutoOpen>]
module private Prelude =
let memoize (f: 'T -> 'S): 'T -> 'S =
let cache = new System.Collections.Concurrent.ConcurrentDictionary<'T, 'S>()
fun t -> cache.GetOrAdd(t, f)

[<AbstractClass>]
type JsonPickler<'T>() =
inherit Serialization.JsonConverter<'T>()

static let isMatchingType =
let rec isMatching = function
| [] -> false
| t :: _ when t = typeof<'T> -> true
| t :: tl ->
let tail =
[ match t.BaseType with null -> () | bt -> yield bt
yield! t.GetInterfaces()
yield! tl ]
isMatching tail
memoize (fun t -> isMatching [t])

abstract Read: reader: byref<Utf8JsonReader> * options: JsonSerializerOptions -> 'T

override _.CanConvert t = isMatchingType t

override x.Read(reader, _typeToConvert, options) =
x.Read(&reader, options)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ open System.Text.Json.Serialization
type UnionOrTypeSafeEnumConverterFactory(typeSafeEnum, union) =
inherit JsonConverterFactory()

static let hasConverterAttribute: System.Type -> bool = memoize _.IsDefined(typeof<JsonConverterAttribute>, true)
static let cache = System.Collections.Concurrent.ConcurrentDictionary<System.Type, bool>()
static let hasConverterAttribute t: bool = cache.GetOrAdd(t, fun (t: System.Type) -> t.IsDefined(typeof<JsonConverterAttribute>, ``inherit`` = false))

override _.CanConvert t =
not (t.IsGenericType && let g = t.GetGenericTypeDefinition() in g = typedefof<option<_>> || g = typedefof<list<_>>)
Expand Down

0 comments on commit 097c9ec

Please sign in to comment.