Skip to content

CSHARP-3985: Support multiple SerializerRegistries #1592

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

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 commits
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
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 I think we could keep this as is
}
return __dynamicArraySerializer;
}
Expand All @@ -62,7 +62,7 @@ public static IBsonSerializer DynamicDocumentSerializer
{
if (!__dynamicDocumentSerializerWasSet)
{
__dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>();
__dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>(); //TODO I think we could keep this as is
}
return __dynamicDocumentSerializer;
}
Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB.Bson/BsonExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static byte[] ToBson(

if (serializer == null)
{
serializer = BsonSerializer.LookupSerializer(nominalType);
serializer = BsonSerializer.LookupSerializer(nominalType); //TODO I think we need another method with the domain as input
}
if (serializer.ValueType != nominalType)
{
Expand Down Expand Up @@ -165,7 +165,7 @@ public static BsonDocument ToBsonDocument(
return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method
}

serializer = BsonSerializer.LookupSerializer(nominalType);
serializer = BsonSerializer.LookupSerializer(nominalType); //TODO I think we need another method with the domain as input
}
if (serializer.ValueType != nominalType)
{
Expand Down Expand Up @@ -236,7 +236,7 @@ public static string ToJson(

if (serializer == null)
{
serializer = BsonSerializer.LookupSerializer(nominalType);
serializer = BsonSerializer.LookupSerializer(nominalType); //TODO I think we need another method with the domain as input
}
if (serializer.ValueType != nominalType)
{
Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB.Bson/ObjectModel/BsonDocumentWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static BsonDocumentWrapper Create<TNominalType>(TNominalType value)
/// <returns>A BsonDocumentWrapper.</returns>
public static BsonDocumentWrapper Create(Type nominalType, object value)
{
var serializer = BsonSerializer.LookupSerializer(nominalType);
var serializer = BsonSerializer.LookupSerializer(nominalType); //TODO I think this could stay. We can potentially add methods with the domain as input.
return new BsonDocumentWrapper(value, serializer);
}

Expand All @@ -121,7 +121,7 @@ public static IEnumerable<BsonDocumentWrapper> CreateMultiple<TNominalType>(IEnu
throw new ArgumentNullException("values");
}

var serializer = BsonSerializer.LookupSerializer(typeof(TNominalType));
var serializer = BsonSerializer.LookupSerializer(typeof(TNominalType)); //TODO I think this could stay. We can potentially add methods with the domain as input.
return values.Select(v => new BsonDocumentWrapper(v, serializer));
}

Expand All @@ -142,7 +142,7 @@ public static IEnumerable<BsonDocumentWrapper> CreateMultiple(Type nominalType,
throw new ArgumentNullException("values");
}

var serializer = BsonSerializer.LookupSerializer(nominalType);
var serializer = BsonSerializer.LookupSerializer(nominalType); //TODO I think this could stay. We can potentially add methods with the domain as input.
return values.Cast<object>().Select(v => new BsonDocumentWrapper(v, serializer));
}

Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB.Bson/Serialization/BsonClassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace MongoDB.Bson.Serialization
public class BsonClassMap
{
// private static fields
private readonly static Dictionary<Type, BsonClassMap> __classMaps = new Dictionary<Type, BsonClassMap>();
private readonly static Dictionary<Type, BsonClassMap> __classMaps = new Dictionary<Type, BsonClassMap>(); //TODO I think the static fields and methods should not be here, but on the domain
private readonly static Queue<Type> __knownTypesQueue = new Queue<Type>();
private static int __freezeNestingLevel = 0;

Expand Down Expand Up @@ -284,7 +284,7 @@ public static Type GetMemberInfoType(MemberInfo memberInfo)
/// <returns>All registered class maps.</returns>
public static IEnumerable<BsonClassMap> GetRegisteredClassMaps()
{
BsonSerializer.ConfigLock.EnterReadLock();
BsonSerializer.ConfigLock.EnterReadLock(); //TODO It would make sense to look at this after the PR by Robert is merged
try
{
return __classMaps.Values.ToList(); // return a copy for thread safety
Expand Down Expand Up @@ -375,7 +375,7 @@ public static BsonClassMap LookupClassMap(Type classType)
/// </summary>
/// <typeparam name="TClass">The class.</typeparam>
/// <returns>The class map.</returns>
public static BsonClassMap<TClass> RegisterClassMap<TClass>()
public static BsonClassMap<TClass> RegisterClassMap<TClass>() //TODO We should move the static methods here to IBSonSerializerDomain
{
return RegisterClassMap<TClass>(cm => { cm.AutoMap(); });
}
Expand Down
26 changes: 24 additions & 2 deletions src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

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

namespace MongoDB.Bson.Serialization
{
Expand All @@ -28,18 +30,22 @@ public class BsonDeserializationContext
private readonly IBsonSerializer _dynamicArraySerializer;
private readonly IBsonSerializer _dynamicDocumentSerializer;
private readonly IBsonReader _reader;
private readonly IBsonSerializationDomain _domain;

// constructors
private BsonDeserializationContext(
IBsonReader reader,
bool allowDuplicateElementNames,
IBsonSerializer dynamicArraySerializer,
IBsonSerializer dynamicDocumentSerializer)
IBsonSerializer dynamicDocumentSerializer,
IBsonSerializationDomain domain)
{
_reader = reader;
_allowDuplicateElementNames = allowDuplicateElementNames;
_dynamicArraySerializer = dynamicArraySerializer;
_dynamicDocumentSerializer = dynamicDocumentSerializer;
_domain = domain;
// _domain = domain ?? BsonSerializer.DefaultDomain; //TODO We could do this here, but let's keep it as the previous line to catch errors.
}

// public properties
Expand All @@ -54,6 +60,11 @@ public bool AllowDuplicateElementNames
get { return _allowDuplicateElementNames; }
}

/// <summary>
/// //TODO
/// </summary>
public IBsonSerializationDomain Domain => _domain;

/// <summary>
/// Gets the dynamic array serializer.
/// </summary>
Expand Down Expand Up @@ -138,6 +149,7 @@ public class Builder
private IBsonSerializer _dynamicArraySerializer;
private IBsonSerializer _dynamicDocumentSerializer;
private IBsonReader _reader;
private IBsonSerializationDomain _domain;

// constructors
internal Builder(BsonDeserializationContext other, IBsonReader reader)
Expand All @@ -153,6 +165,7 @@ internal Builder(BsonDeserializationContext other, IBsonReader reader)
_allowDuplicateElementNames = other.AllowDuplicateElementNames;
_dynamicArraySerializer = other.DynamicArraySerializer;
_dynamicDocumentSerializer = other.DynamicDocumentSerializer;
_domain = other.Domain;
}
else
{
Expand Down Expand Up @@ -209,14 +222,23 @@ 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);
return new BsonDeserializationContext(_reader, _allowDuplicateElementNames, _dynamicArraySerializer, _dynamicDocumentSerializer, _domain);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/Serialization/BsonMemberMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public IBsonSerializer GetSerializer()
// return special serializer for BsonValue members that handles the _csharpnull representation
if (_memberTypeIsBsonValue)
{
var wrappedSerializer = BsonSerializer.LookupSerializer(_memberType);
var wrappedSerializer = BsonSerializer.LookupSerializer(_memberType); //TODO We need another version of this with the domain as input
var isBsonArraySerializer = wrappedSerializer is IBsonArraySerializer;
var isBsonDocumentSerializer = wrappedSerializer is IBsonDocumentSerializer;

Expand Down
24 changes: 22 additions & 2 deletions src/MongoDB.Bson/Serialization/BsonSerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,27 @@ public class BsonSerializationContext
// private fields
private readonly Func<Type, bool> _isDynamicType;
private readonly IBsonWriter _writer;
private readonly IBsonSerializationDomain _domain;

// constructors
private BsonSerializationContext(
IBsonWriter writer,
Func<Type, bool> isDynamicType)
Func<Type, bool> isDynamicType,
IBsonSerializationDomain domain)
{
_writer = writer;
_isDynamicType = isDynamicType;
_domain = domain;
// _domain = domain ?? BsonSerializer.DefaultDomain; //TODO We could do this here, but let's keep it as the previous line to catch errors.
}

// public properties

/// <summary>
/// //TODO
/// </summary>
public IBsonSerializationDomain Domain => _domain;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably standardize on Domain or SerializationDomain.

SerializationDomain is more accurate, but longer...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, at the moment it's a little bit here and there. I think serialization domain is a better name, even if longer.


/// <summary>
/// Gets a function that, when executed, will indicate whether the type
/// is a dynamic type.
Expand Down Expand Up @@ -105,6 +115,7 @@ public class Builder
// private fields
private Func<Type, bool> _isDynamicType;
private IBsonWriter _writer;
private IBsonSerializationDomain _domain;

// constructors
internal Builder(BsonSerializationContext other, IBsonWriter writer)
Expand All @@ -127,6 +138,15 @@ internal Builder(BsonSerializationContext other, IBsonWriter writer)
}
}

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

// properties
/// <summary>
/// Gets or sets the function used to determine if a type is a dynamic type.
Expand Down Expand Up @@ -155,7 +175,7 @@ public IBsonWriter Writer
/// <returns>A BsonSerializationContext.</returns>
internal BsonSerializationContext Build()
{
return new BsonSerializationContext(_writer, _isDynamicType);
return new BsonSerializationContext(_writer, _isDynamicType, _domain);
}
}
}
Expand Down
Loading