Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSHARP-3985: Support multiple SerializerRegistries #1592

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5a93d57
First test
papafe Jan 8, 2025
bc33071
Revert "First test"
papafe Jan 8, 2025
5e94e62
Added manager
papafe Jan 8, 2025
a317994
Delegating all the methods to the BsonSerializationManager
papafe Jan 8, 2025
0e2f7b9
Added interface
papafe Jan 8, 2025
a051d18
Small fix
papafe Jan 8, 2025
bb60887
Rename
papafe Jan 10, 2025
56a9b7e
Added internal interface
papafe Jan 10, 2025
3f634d5
Improvements
papafe Jan 13, 2025
550eb97
Initial use of domain
papafe Jan 13, 2025
d2e1746
Fix
papafe Jan 13, 2025
8b14426
Added comments
papafe Jan 13, 2025
3dadb07
Test with multiple interfaces
papafe Jan 15, 2025
cf05d41
Improved interfaces
papafe Jan 15, 2025
35f461a
Fix to interface
papafe Jan 15, 2025
7c2beb3
Small renaming
papafe Jan 15, 2025
3ad811e
Added comments
papafe Jan 27, 2025
0ae768c
Finished comments in the Bson lib
papafe Jan 27, 2025
01330fd
Corrected conventions
papafe Jan 27, 2025
7bec37f
Correction
papafe Jan 27, 2025
bb395b0
Corrected convention tests
papafe Jan 27, 2025
d7756c6
Removed multiple interfaces plus more fixes
papafe Jan 27, 2025
70f351b
More improvements
papafe Jan 27, 2025
cc40c72
Small fixes
papafe Jan 27, 2025
04d45fa
Small fixes
papafe Jan 27, 2025
8305dc1
improvements
papafe Jan 28, 2025
402a889
Small fix
papafe Jan 30, 2025
6d2faf9
Various fixes
papafe Jan 30, 2025
5ac0c06
First test passing
papafe Jan 30, 2025
7628f31
Small fixes
papafe Jan 30, 2025
3f5a047
Some saving
papafe Jan 30, 2025
9ccfe7a
Naming correction
papafe Feb 3, 2025
d3973e1
Added test and removed comment
papafe Feb 3, 2025
a587d6d
Improved tests
papafe Feb 3, 2025
856cfb8
Improved deserialization to work as serialization
papafe Feb 3, 2025
2cc6a8d
Trying to fix class map
papafe Feb 4, 2025
5742541
Fixed class
papafe Feb 4, 2025
d6d9ddc
Various corrections
papafe Feb 4, 2025
3b27b43
Moved class map to another file
papafe Feb 4, 2025
98682bb
Removed comments
papafe Feb 4, 2025
dc33112
Passing domain in serializer registry
papafe Feb 4, 2025
a45b027
Small improvements
papafe Feb 5, 2025
3f9c37c
Small corrections
papafe Feb 5, 2025
f0487d3
Renamin to serialization domain
papafe Feb 10, 2025
2ed6fb6
Small naming correction
papafe Feb 10, 2025
0074837
Fixed tests
papafe Feb 10, 2025
9011589
removed comment
papafe Feb 11, 2025
8b15445
Added convention registry
papafe Feb 12, 2025
2157ae8
Some improvements
papafe Feb 12, 2025
ab7c5de
Removed domain from translation context
papafe Feb 18, 2025
e683791
Made something internal
papafe Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improved deserialization to work as serialization
papafe committed Feb 3, 2025
commit 856cfb87eeb6895ca48874510de9858856b48c73
3 changes: 2 additions & 1 deletion src/MongoDB.Bson/IO/BsonBinaryReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -136,7 +136,8 @@ protected override BsonReaderSettings CloneImplementation()
Encoding = _encoding,
FixOldBinarySubTypeOnInput = _fixOldBinarySubTypeOnInput,
FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
MaxDocumentSize = _maxDocumentSize
MaxDocumentSize = _maxDocumentSize,
SerializationDomain = SerializationDomain, //TODO This can be improved
};

return clone;
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/BsonDocumentReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ public static BsonDocumentReaderSettings Defaults
/// <returns>A clone of the settings.</returns>
protected override BsonReaderSettings CloneImplementation()
{
var clone = new BsonDocumentReaderSettings();
var clone = new BsonDocumentReaderSettings { SerializationDomain = SerializationDomain }; //TODO This can be improved
return clone;
}
}
15 changes: 15 additions & 0 deletions src/MongoDB.Bson/IO/BsonReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
*/

using System;
using MongoDB.Bson.Serialization;

namespace MongoDB.Bson.IO
{
@@ -24,6 +25,7 @@ public abstract class BsonReaderSettings
{
// private fields
private bool _isFrozen;
private IBsonSerializationDomain _serializationDomain;

// constructors
/// <summary>
@@ -77,6 +79,19 @@ public BsonReaderSettings FrozenCopy()
}
}

/// <summary>
/// //TODO
/// </summary>
public IBsonSerializationDomain SerializationDomain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find anywhere this property is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in the constructor of the BsonDeserializationContext.

{
get => _serializationDomain;
set
{
if (_isFrozen) { ThrowFrozenException(); }
_serializationDomain = value;
}
}

// protected methods
/// <summary>
/// Creates a clone of the settings.
7 changes: 6 additions & 1 deletion src/MongoDB.Bson/IO/IBsonReader.cs
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ public interface IBsonReader : IDisposable
/// <summary>
/// Pops the settings.
/// </summary>
void PopSettings();
void PopSettings(); //TODO Why do we have push and pop methods? They are not used
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised they are not used.

The matching methods in IBsonWriter are used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure why. Only BsonReader is directly implementing this interface, and the two implementations of PopSettings are never called.


/// <summary>
/// Pushes new settings for the reader.
@@ -246,6 +246,11 @@ public interface IBsonReader : IDisposable
/// <param name="bookmark">The bookmark.</param>
void ReturnToBookmark(BsonReaderBookmark bookmark);

/// <summary>
/// Gets the settings of the reader.
/// </summary>
BsonReaderSettings Settings { get; }

/// <summary>
/// Skips the name (reader must be positioned on a name).
/// </summary>
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/JsonReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ public static JsonReaderSettings Defaults
/// <returns>A clone of the settings.</returns>
protected override BsonReaderSettings CloneImplementation()
{
var clone = new JsonReaderSettings();
var clone = new JsonReaderSettings { SerializationDomain = SerializationDomain }; //TODO This can be improved
return clone;
}
}
18 changes: 3 additions & 15 deletions src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs
Original file line number Diff line number Diff line change
@@ -15,8 +15,6 @@

using System;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;

namespace MongoDB.Bson.Serialization
{
@@ -37,14 +35,13 @@ private BsonDeserializationContext(
IBsonReader reader,
bool allowDuplicateElementNames,
IBsonSerializer dynamicArraySerializer,
IBsonSerializer dynamicDocumentSerializer,
IBsonSerializationDomain domain)
IBsonSerializer dynamicDocumentSerializer)
{
_reader = reader;
_allowDuplicateElementNames = allowDuplicateElementNames;
_dynamicArraySerializer = dynamicArraySerializer;
_dynamicDocumentSerializer = dynamicDocumentSerializer;
_domain = domain;
_domain = reader.Settings.SerializationDomain ?? BsonSerializer.DefaultDomain;
}

// public properties
@@ -221,23 +218,14 @@ public IBsonReader Reader
get { return _reader; }
}

/// <summary>
/// //TODO
/// </summary>
public IBsonSerializationDomain Domain
{
get => _domain;
set => _domain = value;
}

// public methods
/// <summary>
/// Builds the BsonDeserializationContext instance.
/// </summary>
/// <returns>A BsonDeserializationContext.</returns>
internal BsonDeserializationContext Build()
{
return new BsonDeserializationContext(_reader, _allowDuplicateElementNames, _dynamicArraySerializer, _dynamicDocumentSerializer, _domain);
return new BsonDeserializationContext(_reader, _allowDuplicateElementNames, _dynamicArraySerializer, _dynamicDocumentSerializer);
}
}
}
16 changes: 6 additions & 10 deletions src/MongoDB.Bson/Serialization/BsonSerializationDomain.cs
Original file line number Diff line number Diff line change
@@ -18,25 +18,21 @@ namespace MongoDB.Bson.Serialization
internal class BsonSerializationDomain : IBsonSerializationDomainInternal, IDisposable
{
// private fields
private ReaderWriterLockSlim _configLock =
new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private ReaderWriterLockSlim _configLock = new(LockRecursionPolicy.SupportsRecursion);

private Dictionary<Type, IIdGenerator> _idGenerators = new Dictionary<Type, IIdGenerator>();
private Dictionary<Type, IIdGenerator> _idGenerators = new();

private Dictionary<Type, IDiscriminatorConvention> _discriminatorConventions =
new Dictionary<Type, IDiscriminatorConvention>();
private Dictionary<Type, IDiscriminatorConvention> _discriminatorConventions = new();

private Dictionary<BsonValue, HashSet<Type>> _discriminators =
new Dictionary<BsonValue, HashSet<Type>>();
private Dictionary<BsonValue, HashSet<Type>> _discriminators = new();

private HashSet<Type> _discriminatedTypes = new HashSet<Type>();
private HashSet<Type> _discriminatedTypes = new();
private BsonSerializerRegistry _serializerRegistry;

private TypeMappingSerializationProvider _typeMappingSerializationProvider;

// ConcurrentDictionary<Type, object> is being used as a concurrent set of Type. The values will always be null.
private ConcurrentDictionary<Type, object> _typesWithRegisteredKnownTypes =
new ConcurrentDictionary<Type, object>();
private ConcurrentDictionary<Type, object> _typesWithRegisteredKnownTypes = new();

private bool _useNullIdChecker = false;
private bool _useZeroIdChecker = false;
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@ public static List<TDocument> DeserializeBatch<TDocument>(RawBsonArray batch, IB
if (messageEncoderSettings != null)
{
readerSettings.Encoding = messageEncoderSettings.GetOrDefault(MessageEncoderSettingsName.ReadEncoding, Utf8Encodings.Strict);
readerSettings.SerializationDomain =
messageEncoderSettings.GetOrDefault<IBsonSerializationDomain>(MessageEncoderSettingsName.SerializationDomain, null);
};

using (var stream = new ByteBufferStream(batch.Slice, ownsBuffer: false))