1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
3
using System . Collections . Generic ;
4
- using MongoDB . Bson . Serialization ;
5
4
using MongoDB . Extensions . Context . Extensions ;
5
+ #nullable enable
6
6
7
7
namespace MongoDB . Extensions . Context . Internal ;
8
8
9
- public class TypeObjectSerializer : IBsonSerializer < object >
9
+ internal class TypeObjectSerializer : ObjectSerializer
10
10
{
11
- private static readonly ConcurrentDictionary < Type , bool > _allowedTypes = new ( ) ;
12
- private static readonly HashSet < string > _allowedTypesByNamespaces = new ( ) ;
13
- private static readonly HashSet < string > _allowedTypesByDependencies = new ( ) ;
11
+ private static readonly ConcurrentDictionary < Type , bool > _allowedTypes = new ( ) ;
12
+ private static readonly HashSet < string > _allowedTypesByNamespaces = new ( ) ;
13
+ private static readonly HashSet < string > _allowedTypesByDependencies = new ( ) ;
14
14
15
- public Type ValueType => typeof ( object ) ;
15
+ public TypeObjectSerializer ( ) : base ( type => IsTypeAllowed ( type ) )
16
+ {
17
+ }
16
18
17
- public static IReadOnlyDictionary < Type , bool > AllowedTypes => _allowedTypes ;
19
+ public static IReadOnlyDictionary < Type , bool > AllowedTypes
20
+ => _allowedTypes ;
18
21
19
- public static IReadOnlyCollection < string > AllowedTypesByNamespaces => _allowedTypesByNamespaces ;
22
+ public static IReadOnlyCollection < string > AllowedTypesByNamespaces
23
+ => _allowedTypesByNamespaces ;
20
24
21
- public static IReadOnlyCollection < string > AllowedTypesByDependencies => _allowedTypesByDependencies ;
25
+ public static IReadOnlyCollection < string > AllowedTypesByDependencies
26
+ => _allowedTypesByDependencies ;
22
27
23
28
public static bool IsTypeAllowed ( Type type )
24
29
{
25
30
return DefaultAllowedTypes ( type ) ||
26
- _allowedTypes . ContainsKey ( type ) ||
27
- IsInAllowedNamespaces ( type ) ||
28
- IsInAllowedDependencyTypes ( type ) ;
31
+ _allowedTypes . ContainsKey ( type ) ||
32
+ IsInAllowedNamespaces ( type ) ||
33
+ IsInAllowedDependencyTypes ( type ) ;
29
34
}
30
35
31
- public static Func < Type , bool > DefaultAllowedTypes => DefaultFrameworkAllowedTypes . AllowedTypes ;
32
-
33
36
public static void AddAllowedType < T > ( )
34
37
{
35
38
_allowedTypes . TryAdd ( typeof ( T ) , true ) ;
@@ -85,12 +88,13 @@ private static bool IsAllowedNameSpacePart(Type type)
85
88
{
86
89
foreach ( string allowedNamespace in _allowedTypesByNamespaces )
87
90
{
88
- if ( string . IsNullOrEmpty ( type . Namespace ) )
91
+ if ( string . IsNullOrEmpty ( type . Namespace ) )
89
92
{
90
93
return false ;
91
94
}
92
95
93
- if ( type . Namespace . StartsWith ( allowedNamespace , StringComparison . OrdinalIgnoreCase ) )
96
+ if ( type . Namespace . StartsWith ( allowedNamespace ,
97
+ StringComparison . OrdinalIgnoreCase ) )
94
98
{
95
99
return true ;
96
100
}
@@ -101,30 +105,11 @@ private static bool IsAllowedNameSpacePart(Type type)
101
105
102
106
private static bool IsInAllowedDependencyTypes ( Type type )
103
107
{
104
- bool isInDependencyTypes = _allowedTypesByDependencies . Contains ( type . GetRootNamespace ( ) ) ;
108
+ bool isInDependencyTypes = _allowedTypesByDependencies
109
+ . Contains ( type . GetRootNamespace ( ) ) ;
105
110
106
111
_allowedTypes . TryAdd ( type , isInDependencyTypes ) ;
107
112
108
113
return isInDependencyTypes ;
109
114
}
110
-
111
- public void Serialize ( BsonSerializationContext context , BsonSerializationArgs args , object value )
112
- {
113
- var serializer = BsonSerializer . LookupSerializer ( value . GetType ( ) ) ;
114
- serializer . Serialize ( context , args , value ) ;
115
- }
116
-
117
- public object Deserialize ( BsonDeserializationContext context , BsonDeserializationArgs args )
118
- {
119
- var bsonType = context . Reader . GetCurrentBsonType ( ) ;
120
- if ( bsonType == MongoDB . Bson . BsonType . Null )
121
- {
122
- context . Reader . ReadNull ( ) ;
123
- return null ! ;
124
- }
125
-
126
- var type = args . NominalType ;
127
- var serializer = BsonSerializer . LookupSerializer ( type ) ;
128
- return serializer . Deserialize ( context , args ) ;
129
- }
130
115
}
0 commit comments