Skip to content

Commit 4d1ccd0

Browse files
committed
Trim Picker logic
1 parent e8458aa commit 4d1ccd0

File tree

4 files changed

+8
-44
lines changed

4 files changed

+8
-44
lines changed

src/FsCodec.NewtonsoftJson/Pickler.fs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
11
namespace FsCodec.NewtonsoftJson
22

33
open Newtonsoft.Json
4-
open System
5-
6-
[<AutoOpen>]
7-
module private Prelude =
8-
let memoize (f: 'T -> 'S): 'T -> 'S =
9-
let cache = new System.Collections.Concurrent.ConcurrentDictionary<'T, 'S>()
10-
fun t -> cache.GetOrAdd(t, f)
114

125
[<AbstractClass>]
136
type JsonPickler<'T>() =
147
inherit JsonConverter()
158

16-
static let isMatchingType =
17-
let rec isMatching = function
18-
| [] -> false
19-
| t :: _ when t = typeof<'T> -> true
20-
| t :: tl ->
21-
let tail =
22-
[ match t.BaseType with null -> () | bt -> yield bt
23-
yield! t.GetInterfaces()
24-
yield! tl ]
25-
isMatching tail
26-
memoize (fun t -> isMatching [t])
27-
289
abstract Write: writer: JsonWriter * serializer: JsonSerializer * source: 'T -> unit
2910
abstract Read: reader: JsonReader * serializer: JsonSerializer -> 'T
3011

31-
override _.CanConvert t = isMatchingType t
12+
override _.CanConvert t = t = typeof<'T>
3213
override _.CanRead = true
3314
override _.CanWrite = true
3415

src/FsCodec.NewtonsoftJson/UnionConverter.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ module private UnionInfo =
1515
|| t.IsArray
1616
|| (t.IsGenericType && let g = t.GetGenericTypeDefinition() in typedefof<Option<_>> = g || g.IsValueType) // Nullable<T>
1717

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

2123
let propTypeRequiresConstruction (propertyType: Type) =
2224
not (isInlinedIntoUnionItem propertyType)

src/FsCodec.SystemTextJson/Pickler.fs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,12 @@
22

33
open System.Text.Json
44

5-
[<AutoOpen>]
6-
module private Prelude =
7-
let memoize (f: 'T -> 'S): 'T -> 'S =
8-
let cache = new System.Collections.Concurrent.ConcurrentDictionary<'T, 'S>()
9-
fun t -> cache.GetOrAdd(t, f)
10-
115
[<AbstractClass>]
126
type JsonPickler<'T>() =
137
inherit Serialization.JsonConverter<'T>()
148

15-
static let isMatchingType =
16-
let rec isMatching = function
17-
| [] -> false
18-
| t :: _ when t = typeof<'T> -> true
19-
| t :: tl ->
20-
let tail =
21-
[ match t.BaseType with null -> () | bt -> yield bt
22-
yield! t.GetInterfaces()
23-
yield! tl ]
24-
isMatching tail
25-
memoize (fun t -> isMatching [t])
26-
279
abstract Read: reader: byref<Utf8JsonReader> * options: JsonSerializerOptions -> 'T
2810

29-
override _.CanConvert t = isMatchingType t
30-
3111
override x.Read(reader, _typeToConvert, options) =
3212
x.Read(&reader, options)
3313

src/FsCodec.SystemTextJson/UnionOrTypeSafeEnumConverterFactory.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ open System.Text.Json.Serialization
55
type UnionOrTypeSafeEnumConverterFactory(typeSafeEnum, union) =
66
inherit JsonConverterFactory()
77

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

1011
override _.CanConvert t =
1112
not (t.IsGenericType && let g = t.GetGenericTypeDefinition() in g = typedefof<option<_>> || g = typedefof<list<_>>)
1213
&& FsCodec.Union.isUnion t
13-
&& not (hasConverterAttribute t)
14+
&& not (typeHasConverterAttribute t)
1415
&& ((typeSafeEnum && union)
1516
|| typeSafeEnum = FsCodec.Union.isNullary t)
1617

0 commit comments

Comments
 (0)