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 44 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
44 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
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
4 changes: 2 additions & 2 deletions src/MongoDB.Bson/BsonDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static IBsonSerializer DynamicArraySerializer
{
if (!__dynamicArraySerializerWasSet)
{
__dynamicArraySerializer = BsonSerializer.LookupSerializer<List<object>>();
__dynamicArraySerializer = BsonSerializer.LookupSerializer<List<object>>(); //TODO ??
}
return __dynamicArraySerializer;
}
Expand All @@ -62,7 +62,7 @@ public static IBsonSerializer DynamicDocumentSerializer
{
if (!__dynamicDocumentSerializerWasSet)
{
__dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>();
__dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>(); //TODO ??
}
return __dynamicDocumentSerializer;
}
Expand Down
75 changes: 72 additions & 3 deletions src/MongoDB.Bson/BsonExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ public static byte[] ToBson(
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs),
int estimatedBsonSize = 0) => ToBson(obj, nominalType, BsonSerializer.DefaultDomain, writerSettings,
serializer, configurator, args, estimatedBsonSize);

/// <summary>
/// //TODO
/// </summary>
/// <param name="obj"></param>
/// <param name="nominalType"></param>
/// <param name="domain"></param>
/// <param name="writerSettings"></param>
/// <param name="serializer"></param>
/// <param name="configurator"></param>
/// <param name="args"></param>
/// <param name="estimatedBsonSize"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public static byte[] ToBson(
this object obj,
Type nominalType,
IBsonSerializationDomain domain,
BsonBinaryWriterSettings writerSettings = null,
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs),
int estimatedBsonSize = 0)
{
if (estimatedBsonSize < 0)
Expand All @@ -84,7 +109,7 @@ public static byte[] ToBson(

if (serializer == null)
{
serializer = BsonSerializer.LookupSerializer(nominalType);
serializer = domain.LookupSerializer(nominalType);
}
if (serializer.ValueType != nominalType)
{
Expand Down Expand Up @@ -138,6 +163,27 @@ public static BsonDocument ToBsonDocument(
Type nominalType,
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs)) => ToBsonDocument(obj, nominalType,
BsonSerializer.DefaultDomain, serializer, configurator, args);

/// <summary>
/// //TODO
/// </summary>
/// <param name="obj"></param>
/// <param name="nominalType"></param>
/// <param name="domain"></param>
/// <param name="serializer"></param>
/// <param name="configurator"></param>
/// <param name="args"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static BsonDocument ToBsonDocument(
this object obj,
Type nominalType,
IBsonSerializationDomain domain,
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs))
{
if (nominalType == null)
Expand Down Expand Up @@ -165,7 +211,7 @@ public static BsonDocument ToBsonDocument(
return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method
}

serializer = BsonSerializer.LookupSerializer(nominalType);
serializer = domain.LookupSerializer(nominalType); //TODO ??
}
if (serializer.ValueType != nominalType)
{
Expand Down Expand Up @@ -227,6 +273,29 @@ public static string ToJson(
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs))
=> ToJson(obj, nominalType, BsonSerializer.DefaultDomain, writerSettings, serializer, configurator, args);

/// <summary>
/// //TODO
/// </summary>
/// <param name="obj"></param>
/// <param name="nominalType"></param>
/// <param name="domain"></param>
/// <param name="writerSettings"></param>
/// <param name="serializer"></param>
/// <param name="configurator"></param>
/// <param name="args"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static string ToJson(
this object obj,
Type nominalType,
IBsonSerializationDomain domain,
JsonWriterSettings writerSettings = null,
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs))
{
if (nominalType == null)
{
Expand All @@ -236,7 +305,7 @@ public static string ToJson(

if (serializer == null)
{
serializer = BsonSerializer.LookupSerializer(nominalType);
serializer = domain.LookupSerializer(nominalType);
}
if (serializer.ValueType != nominalType)
{
Expand Down
3 changes: 2 additions & 1 deletion src/MongoDB.Bson/IO/BsonBinaryReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/MongoDB.Bson/IO/BsonBinaryWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ protected override BsonWriterSettings CloneImplementation()
Encoding = _encoding,
FixOldBinarySubTypeOnOutput = _fixOldBinarySubTypeOnOutput,
MaxDocumentSize = _maxDocumentSize,
MaxSerializationDepth = MaxSerializationDepth
MaxSerializationDepth = MaxSerializationDepth,
SerializationDomain = SerializationDomain,
};
return clone;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/BsonDocumentReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/MongoDB.Bson/IO/BsonReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using MongoDB.Bson.Serialization;

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

// constructors
/// <summary>
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions src/MongoDB.Bson/IO/BsonWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using MongoDB.Bson.Serialization;

namespace MongoDB.Bson.IO
{
Expand All @@ -25,6 +26,7 @@ public abstract class BsonWriterSettings
// private fields
private bool _isFrozen;
private int _maxSerializationDepth = BsonDefaults.MaxSerializationDepth;
private IBsonSerializationDomain _serializationDomain;

// constructors
/// <summary>
Expand Down Expand Up @@ -92,6 +94,19 @@ public BsonWriterSettings 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 BsonSerializationContext

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

// protected methods
/// <summary>
/// Creates a clone of the settings.
Expand Down
7 changes: 6 additions & 1 deletion src/MongoDB.Bson/IO/IBsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.


/// <summary>
/// Pushes new settings for the reader.
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/JsonReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
58 changes: 50 additions & 8 deletions src/MongoDB.Bson/ObjectModel/BsonDocumentWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,19 @@ public object Wrapped
/// <typeparam name="TNominalType">The nominal type of the wrapped object.</typeparam>
/// <param name="value">The wrapped object.</param>
/// <returns>A BsonDocumentWrapper.</returns>
public static BsonDocumentWrapper Create<TNominalType>(TNominalType value)
public static BsonDocumentWrapper Create<TNominalType>(TNominalType value) =>
Create(value, BsonSerializer.DefaultDomain);

/// <summary>
/// //TODO
/// </summary>
/// <param name="value"></param>
/// <param name="domain"></param>
/// <typeparam name="TNominalType"></typeparam>
/// <returns></returns>
public static BsonDocumentWrapper Create<TNominalType>(TNominalType value, IBsonSerializationDomain domain)
{
return Create(typeof(TNominalType), value);
return Create(typeof(TNominalType), value, domain);
}

/// <summary>
Expand All @@ -102,9 +112,19 @@ public static BsonDocumentWrapper Create<TNominalType>(TNominalType value)
/// <param name="nominalType">The nominal type of the wrapped object.</param>
/// <param name="value">The wrapped object.</param>
/// <returns>A BsonDocumentWrapper.</returns>
public static BsonDocumentWrapper Create(Type nominalType, object value)
public static BsonDocumentWrapper Create(Type nominalType, object value) =>
Create(nominalType, value, BsonSerializer.DefaultDomain);

/// <summary>
/// //TODO
/// </summary>
/// <param name="nominalType"></param>
/// <param name="value"></param>
/// <param name="domain"></param>
/// <returns></returns>
public static BsonDocumentWrapper Create(Type nominalType, object value, IBsonSerializationDomain domain)
{
var serializer = BsonSerializer.LookupSerializer(nominalType);
var serializer = domain.LookupSerializer(nominalType); //TODO ??
return new BsonDocumentWrapper(value, serializer);
}

Expand All @@ -114,14 +134,25 @@ public static BsonDocumentWrapper Create(Type nominalType, object value)
/// <typeparam name="TNominalType">The nominal type of the wrapped objects.</typeparam>
/// <param name="values">A list of wrapped objects.</param>
/// <returns>A list of BsonDocumentWrappers.</returns>
public static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IEnumerable<TNominalType> values)
public static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IEnumerable<TNominalType> values) =>
CreateMultiple(values, BsonSerializer.DefaultDomain);

/// <summary>
/// //TODO
/// </summary>
/// <param name="values"></param>
/// <param name="domain"></param>
/// <typeparam name="TNominalType"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IEnumerable<TNominalType> values, IBsonSerializationDomain domain)
{
if (values == null)
{
throw new ArgumentNullException("values");
}

var serializer = BsonSerializer.LookupSerializer(typeof(TNominalType));
var serializer = domain.LookupSerializer(typeof(TNominalType));
return values.Select(v => new BsonDocumentWrapper(v, serializer));
}

Expand All @@ -131,7 +162,18 @@ public static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IEnu
/// <param name="nominalType">The nominal type of the wrapped object.</param>
/// <param name="values">A list of wrapped objects.</param>
/// <returns>A list of BsonDocumentWrappers.</returns>
public static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType, IEnumerable values)
public static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType, IEnumerable values) =>
CreateMultiple(nominalType, values, BsonSerializer.DefaultDomain);

/// <summary>
/// //TODO
/// </summary>
/// <param name="nominalType"></param>
/// <param name="values"></param>
/// <param name="domain"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType, IEnumerable values, IBsonSerializationDomain domain)
{
if (nominalType == null)
{
Expand All @@ -142,7 +184,7 @@ public static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType,
throw new ArgumentNullException("values");
}

var serializer = BsonSerializer.LookupSerializer(nominalType);
var serializer = domain.LookupSerializer(nominalType);
return values.Cast<object>().Select(v => new BsonDocumentWrapper(v, serializer));
}

Expand Down
Loading