-
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
217c292
65cddb7
fafcafa
e2f6dda
1ad919f
cb812a2
e35467c
0f2d592
ae07d28
fb36fbb
d8a65b1
b014fda
8acb837
08365aa
f1e2a32
753468e
d7c7841
42528ce
d08f243
63a8af3
e1703dc
0c65ea1
ef75731
29dd24b
3eb7b41
d15e736
4e3c66b
81f73e6
798f53c
40ca99d
4fb39ce
c9c1282
73e5b98
13c322b
a7942fd
77163d7
17031c1
7634e48
bffe921
87bf081
f2d6e4c
6caf45e
08a7537
e793b66
67495b3
74136c0
a59ad0f
0ba1861
5ea195a
823b2ad
acb03b9
c41c30c
01df61f
3bbe170
bcc84b6
d1905f3
18e98e6
0a4bba3
0cd5bbf
9b14687
83607a5
8023b9e
a94acc3
53e3e08
02e2845
2657680
abf6f70
b375316
a3e4d1a
d98c914
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? |
||
{ | ||
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,15 @@ 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); | ||
//QUESTION Is it correct we only need a default domain here? | ||
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 |
||
var array = BsonArraySerializer.Instance.Deserialize(deserializationContext); | ||
|
||
using (var memoryStream = new MemoryStream()) | ||
using (var bsonWriter = new BsonBinaryWriter(memoryStream, BsonBinaryWriterSettings.Defaults)) | ||
{ | ||
var serializationContext = BsonSerializationContext.CreateRoot(bsonWriter); | ||
//QUESTION Is it correct we only need a default domain here? | ||
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 +353,8 @@ 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); | ||
//QUESTION Is it correct we only need a default domain here? | ||
var deserializationContext = BsonDeserializationContext.CreateRoot(this, BsonSerializer.DefaultSerializationDomain); | ||
var document = BsonDocumentSerializer.Instance.Deserialize(deserializationContext); | ||
var bytes = document.ToBson(); | ||
return new ByteArrayBuffer(bytes, isReadOnly: true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
*/ | ||
|
||
using System; | ||
using MongoDB.Bson.Serialization; | ||
|
||
namespace MongoDB.Bson.IO | ||
{ | ||
|
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?