Skip to content

Commit

Permalink
Merge pull request #7 from Etherna/fix/MCD-10-force-static-serializer…
Browse files Browse the repository at this point in the history
…-registry

fix issue of internal serializator registry selection
  • Loading branch information
tmm360 authored Aug 27, 2024
2 parents a8e13bd + 8426b91 commit 46fef77
Show file tree
Hide file tree
Showing 63 changed files with 333 additions and 187 deletions.
87 changes: 47 additions & 40 deletions src/MongoDB.Bson/BsonDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,6 @@ static BsonDefaults()
}

// public static properties
/// <summary>
/// Gets or sets the dynamic array serializer.
/// </summary>
public static IBsonSerializer DynamicArraySerializer
{
get
{
if (!__dynamicArraySerializerWasSet)
{
__dynamicArraySerializer = BsonSerializer.LookupSerializer<List<object>>();
}
return __dynamicArraySerializer;
}
set
{
__dynamicArraySerializerWasSet = true;
__dynamicArraySerializer = value;
}
}

/// <summary>
/// Gets or sets the dynamic document serializer.
/// </summary>
public static IBsonSerializer DynamicDocumentSerializer
{
get
{
if (!__dynamicDocumentSerializerWasSet)
{
__dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>();
}
return __dynamicDocumentSerializer;
}
set
{
__dynamicDocumentSerializerWasSet = true;
__dynamicDocumentSerializer = value;
}
}

/// <summary>
/// Gets or sets the default representation to be used in serialization of
/// Guids to the database.
Expand Down Expand Up @@ -153,5 +113,52 @@ public static int MaxSerializationDepth
get { return __maxSerializationDepth; }
set { __maxSerializationDepth = value; }
}

//public static methods
/// <summary>
/// Gets the dynamic array serializer.
/// </summary>
public static IBsonSerializer GetDynamicArraySerializer(
bool forceStaticSerializerRegistry = false)
{
if (!__dynamicArraySerializerWasSet)
{
__dynamicArraySerializer = BsonSerializer.LookupSerializer<List<object>>(forceStaticSerializerRegistry);
}

return __dynamicArraySerializer;
}

/// <summary>
/// Gets or sets the dynamic document serializer.
/// </summary>
public static IBsonSerializer GetDynamicDocumentSerializer(
bool forceStaticSerializerRegistry = false)
{
if (!__dynamicDocumentSerializerWasSet)
{
__dynamicDocumentSerializer = BsonSerializer.LookupSerializer<ExpandoObject>(forceStaticSerializerRegistry);
}

return __dynamicDocumentSerializer;
}

/// <summary>
/// Sets the dynamic array serializer.
/// </summary>
public static void SetDynamicArraySerializer(IBsonSerializer value)
{
__dynamicArraySerializerWasSet = true;
__dynamicArraySerializer = value;
}

/// <summary>
/// Gets or sets the dynamic document serializer.
/// </summary>
public static void SetDynamicDocumentSerializer(IBsonSerializer value)
{
__dynamicDocumentSerializerWasSet = true;
__dynamicDocumentSerializer = value;
}
}
}
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, args.ForceStaticSerializerRegistry);
}
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, args.ForceStaticSerializerRegistry);
}
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, args.ForceStaticSerializerRegistry);
}
if (serializer.ValueType != nominalType)
{
Expand Down
12 changes: 10 additions & 2 deletions src/MongoDB.Bson/ObjectModel/BsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -949,8 +949,12 @@ public virtual BsonValue GetValue(string name)
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="defaultValue">The default value returned if the element is not found.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>The value of the element or the default value if the element is not found.</returns>
public virtual BsonValue GetValue(string name, BsonValue defaultValue)
public virtual BsonValue GetValue(
string name,
BsonValue defaultValue,
bool forceStaticSerializerRegistry = false)
{
if (name == null)
{
Expand Down Expand Up @@ -1265,8 +1269,12 @@ public virtual bool TryGetElement(string name, out BsonElement value)
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>True if an element with that name was found.</returns>
public virtual bool TryGetValue(string name, out BsonValue value)
public virtual bool TryGetValue(
string name,
out BsonValue value,
bool forceStaticSerializerRegistry = false)
{
if (name == null)
{
Expand Down
14 changes: 11 additions & 3 deletions src/MongoDB.Bson/ObjectModel/MaterializedOnDemandBsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,17 @@ public override BsonValue GetValue(string name)
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="defaultValue">The default value returned if the element is not found.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>
/// The value of the element or the default value if the element is not found.
/// </returns>
public override BsonValue GetValue(string name, BsonValue defaultValue)
public override BsonValue GetValue(
string name,
BsonValue defaultValue,
bool forceStaticSerializerRegistry = false)
{
EnsureIsMaterialized();
return base.GetValue(name, defaultValue);
return base.GetValue(name, defaultValue, forceStaticSerializerRegistry);
}

/// <summary>
Expand Down Expand Up @@ -726,10 +730,14 @@ public override bool TryGetElement(string name, out BsonElement value)
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>
/// True if an element with that name was found.
/// </returns>
public override bool TryGetValue(string name, out BsonValue value)
public override bool TryGetValue(
string name,
out BsonValue value,
bool forceStaticSerializerRegistry = false)
{
EnsureIsMaterialized();
return base.TryGetValue(name, out value);
Expand Down
27 changes: 21 additions & 6 deletions src/MongoDB.Bson/ObjectModel/RawBsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,14 +668,18 @@ public override BsonValue GetValue(string name)
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="defaultValue">The default value returned if the element is not found.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>
/// The value of the element or the default value if the element is not found.
/// </returns>
public override BsonValue GetValue(string name, BsonValue defaultValue)
public override BsonValue GetValue(
string name,
BsonValue defaultValue,
bool forceStaticSerializerRegistry = false)
{
ThrowIfDisposed();
BsonValue value;
if (TryGetValue(name, out value))
if (TryGetValue(name, out value, forceStaticSerializerRegistry))
{
return value;
}
Expand All @@ -697,14 +701,19 @@ public override void InsertAt(int index, BsonElement element)
/// Materializes the RawBsonDocument into a regular BsonDocument.
/// </summary>
/// <param name="binaryReaderSettings">The binary reader settings.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A BsonDocument.</returns>
public BsonDocument Materialize(BsonBinaryReaderSettings binaryReaderSettings)
public BsonDocument Materialize(
BsonBinaryReaderSettings binaryReaderSettings,
bool forceStaticSerializerRegistry = false)
{
ThrowIfDisposed();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var reader = new BsonBinaryReader(stream, binaryReaderSettings))
{
var context = BsonDeserializationContext.CreateRoot(reader);
var context = BsonDeserializationContext.CreateRoot(
reader,
forceStaticSerializerRegistry: forceStaticSerializerRegistry);
return BsonDocumentSerializer.Instance.Deserialize(context);
}
}
Expand Down Expand Up @@ -853,16 +862,22 @@ public override bool TryGetElement(string name, out BsonElement element)
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>
/// True if an element with that name was found.
/// </returns>
public override bool TryGetValue(string name, out BsonValue value)
public override bool TryGetValue(
string name,
out BsonValue value,
bool forceStaticSerializerRegistry = false)
{
ThrowIfDisposed();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
var context = BsonDeserializationContext.CreateRoot(
bsonReader,
forceStaticSerializerRegistry: forceStaticSerializerRegistry);

bsonReader.ReadStartDocument();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
Expand Down
12 changes: 12 additions & 0 deletions src/MongoDB.Bson/Serialization/BsonDeserializationArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@ namespace Etherna.MongoDB.Bson.Serialization
public struct BsonDeserializationArgs
{
// private fields
private bool _forceStaticSerializerRegistry;
private Type _nominalType;

// constructors
private BsonDeserializationArgs(
bool forceStaticSerializerRegistry,
Type nominalType)
{
_forceStaticSerializerRegistry = forceStaticSerializerRegistry;
_nominalType = nominalType;
}

// public properties
/// <summary>
/// Gets or sets when to force using of static serialization registry
/// </summary>
public bool ForceStaticSerializerRegistry
{
get { return _forceStaticSerializerRegistry; }
set { _forceStaticSerializerRegistry = value; }
}

/// <summary>
/// Gets or sets the nominal type.
/// </summary>
Expand Down
21 changes: 14 additions & 7 deletions src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ public IBsonReader Reader
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="configurator">The configurator.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>
/// A root context.
/// </returns>
public static BsonDeserializationContext CreateRoot(
IBsonReader reader,
Action<Builder> configurator = null)
Action<Builder> configurator = null,
bool forceStaticSerializerRegistry = false)
{
var builder = new Builder(null, reader);
var builder = new Builder(null, reader, forceStaticSerializerRegistry);
if (configurator != null)
{
configurator(builder);
Expand All @@ -113,13 +115,15 @@ public static BsonDeserializationContext CreateRoot(
/// Creates a new context with some values changed.
/// </summary>
/// <param name="configurator">The configurator.</param>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>
/// A new context.
/// </returns>
public BsonDeserializationContext With(
Action<Builder> configurator = null)
Action<Builder> configurator = null,
bool forceStaticSerializerRegistry = false)
{
var builder = new Builder(this, _reader);
var builder = new Builder(this, _reader, forceStaticSerializerRegistry);
if (configurator != null)
{
configurator(builder);
Expand All @@ -140,7 +144,10 @@ public class Builder
private IBsonReader _reader;

// constructors
internal Builder(BsonDeserializationContext other, IBsonReader reader)
internal Builder(
BsonDeserializationContext other,
IBsonReader reader,
bool forceStaticSerializerRegistry)
{
if (reader == null)
{
Expand All @@ -156,8 +163,8 @@ internal Builder(BsonDeserializationContext other, IBsonReader reader)
}
else
{
_dynamicArraySerializer = BsonDefaults.DynamicArraySerializer;
_dynamicDocumentSerializer = BsonDefaults.DynamicDocumentSerializer;
_dynamicArraySerializer = BsonDefaults.GetDynamicArraySerializer(forceStaticSerializerRegistry);
_dynamicDocumentSerializer = BsonDefaults.GetDynamicDocumentSerializer(forceStaticSerializerRegistry);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/MongoDB.Bson/Serialization/BsonSerializationArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Etherna.MongoDB.Bson.Serialization
public struct BsonSerializationArgs
{
// private fields
private bool _forceStaticSerializerRegistry = false;
private Type _nominalType;
private bool _serializeAsNominalType;
private bool _serializeIdFirst;
Expand All @@ -45,6 +46,15 @@ public BsonSerializationArgs(
}

// public properties
/// <summary>
/// Gets or sets when to force using of static serialization registry
/// </summary>
public bool ForceStaticSerializerRegistry
{
get { return _forceStaticSerializerRegistry; }
set { _forceStaticSerializerRegistry = value; }
}

/// <summary>
/// Gets or sets the nominal type.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Bson/Serialization/BsonSerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ internal Builder(BsonSerializationContext other, IBsonWriter writer)
else
{
_isDynamicType = t =>
(BsonDefaults.DynamicArraySerializer != null && t == BsonDefaults.DynamicArraySerializer.ValueType) ||
(BsonDefaults.DynamicDocumentSerializer != null && t == BsonDefaults.DynamicDocumentSerializer.ValueType);
(BsonDefaults.GetDynamicArraySerializer() != null && t == BsonDefaults.GetDynamicArraySerializer().ValueType) ||
(BsonDefaults.GetDynamicDocumentSerializer() != null && t == BsonDefaults.GetDynamicDocumentSerializer().ValueType);
}
}

Expand Down
Loading

0 comments on commit 46fef77

Please sign in to comment.