-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from all commits
0472c3d
0595cb8
d83811d
ef988c3
879f6c5
43ea9c2
08247aa
a8ba088
59c3a47
27671ae
f8915ab
7f45484
acd8a64
681f461
d23c75d
ee3592e
01e7d59
643408a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using MongoDB.Bson.Serialization; | ||
|
||
namespace MongoDB.Bson | ||
{ | ||
internal class BsonDefaultsDomain : IBsonDefaults | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for this class to be immutable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before, we can decide to make this immutable when this is created with builders. |
||
{ | ||
private IBsonSerializationDomain _serializationDomain; | ||
private bool _dynamicArraySerializerWasSet; | ||
private IBsonSerializer _dynamicArraySerializer; | ||
private bool _dynamicDocumentSerializerWasSet; | ||
private IBsonSerializer _dynamicDocumentSerializer; | ||
|
||
public BsonDefaultsDomain(IBsonSerializationDomain serializationDomain) | ||
{ | ||
_serializationDomain = serializationDomain; | ||
} | ||
|
||
public IBsonSerializer DynamicArraySerializer | ||
{ | ||
get | ||
{ | ||
if (!_dynamicArraySerializerWasSet) | ||
{ | ||
_dynamicArraySerializer = _serializationDomain.LookupSerializer<List<object>>(); | ||
} | ||
return _dynamicArraySerializer; | ||
} | ||
set | ||
{ | ||
_dynamicArraySerializerWasSet = true; | ||
_dynamicArraySerializer = value; | ||
} | ||
} | ||
|
||
public IBsonSerializer DynamicDocumentSerializer | ||
{ | ||
get | ||
{ | ||
if (!_dynamicDocumentSerializerWasSet) | ||
{ | ||
_dynamicDocumentSerializer = _serializationDomain.LookupSerializer<ExpandoObject>(); | ||
} | ||
return _dynamicDocumentSerializer; | ||
} | ||
set | ||
{ | ||
_dynamicDocumentSerializerWasSet = true; | ||
_dynamicDocumentSerializer = value; | ||
} | ||
} | ||
|
||
public int MaxDocumentSize { get; set; } = int.MaxValue; | ||
|
||
public int MaxSerializationDepth { get; set; } = 100; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using MongoDB.Bson.Serialization; | ||
|
||
namespace MongoDB.Bson | ||
{ | ||
internal interface IBsonDefaults | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
IBsonSerializer DynamicArraySerializer { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
IBsonSerializer DynamicDocumentSerializer { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
int MaxDocumentSize { get; set; } | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
int MaxSerializationDepth { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,13 +321,13 @@ public virtual IByteBuffer ReadRawBsonArray() | |
// overridden in BsonBinaryReader to read the raw bytes from the stream | ||
// for all other streams, deserialize the array and reserialize it using a BsonBinaryWriter to get the raw bytes | ||
|
||
var deserializationContext = BsonDeserializationContext.CreateRoot(this); | ||
var deserializationContext = BsonDeserializationContext.CreateRoot(this, BsonSerializer.DefaultSerializationDomain); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need a domain for RawBsonArray/RawBsonDocument? Another idea to address all "default domain" questions: We can consider having some simplified version of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I was not sure if we're dealing only with If so this is the perfect place to address the fact that in my opinion we need to have another kind of domain around. |
||
var array = BsonArraySerializer.Instance.Deserialize(deserializationContext); | ||
|
||
using (var memoryStream = new MemoryStream()) | ||
using (var bsonWriter = new BsonBinaryWriter(memoryStream, BsonBinaryWriterSettings.Defaults)) | ||
{ | ||
var serializationContext = BsonSerializationContext.CreateRoot(bsonWriter); | ||
var serializationContext = BsonSerializationContext.CreateRoot(bsonWriter, BsonSerializer.DefaultSerializationDomain); | ||
bsonWriter.WriteStartDocument(); | ||
var startPosition = memoryStream.Position + 3; // just past BsonType, "x" and null byte | ||
bsonWriter.WriteName("x"); | ||
|
@@ -351,7 +351,7 @@ public virtual IByteBuffer ReadRawBsonDocument() | |
// overridden in BsonBinaryReader to read the raw bytes from the stream | ||
// for all other streams, deserialize the document and use ToBson to get the raw bytes | ||
|
||
var deserializationContext = BsonDeserializationContext.CreateRoot(this); | ||
var deserializationContext = BsonDeserializationContext.CreateRoot(this, BsonSerializer.DefaultSerializationDomain); | ||
var document = BsonDocumentSerializer.Instance.Deserialize(deserializationContext); | ||
var bytes = document.ToBson(); | ||
return new ByteArrayBuffer(bytes, isReadOnly: true); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for this class to be immutable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could when we have builders and we'll have only setters.