1
+ /* Copyright 2010-present MongoDB Inc.
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ using System . Linq ;
17
+ using MongoDB . Bson ;
18
+ using MongoDB . Bson . Serialization ;
19
+ using MongoDB . Bson . Serialization . Attributes ;
20
+ using MongoDB . Bson . Serialization . Serializers ;
21
+ using Xunit ;
22
+
23
+ namespace MongoDB . Driver . Tests
24
+ {
25
+ public class MultipleRegistriesTests
26
+ {
27
+ [ Fact ]
28
+ public void GeneralTest ( )
29
+ {
30
+ // {
31
+ // var client = DriverTestConfiguration.CreateMongoClient(c => c.Domain = BsonSerializer.DefaultDomain);
32
+ // var db = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
33
+ // db.DropCollection(DriverTestConfiguration.CollectionNamespace.CollectionName);
34
+ // var collection = db.GetCollection<Person>(DriverTestConfiguration.CollectionNamespace.CollectionName);
35
+ // var bsonCollection =
36
+ // db.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);
37
+ //
38
+ // var person = new Person { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mario", Age = 24 };
39
+ // collection.InsertOne(person);
40
+ //
41
+ // var retrieved = bsonCollection.FindSync("{}").ToList().Single();
42
+ // var toString = retrieved.ToString();
43
+ //
44
+ // var expectedVal =
45
+ // """{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mario", "Age" : 24 }""";
46
+ // Assert.Equal(expectedVal, toString);
47
+ // }
48
+
49
+ {
50
+ var customDomain = BsonSerializer . CreateDomain ( ) ;
51
+ customDomain . RegisterSerializer ( new CustomStringSerializer ( ) ) ;
52
+
53
+ var client = DriverTestConfiguration . CreateMongoClient ( c => c . Domain = customDomain ) ;
54
+ var db = client . GetDatabase ( DriverTestConfiguration . DatabaseNamespace . DatabaseName ) ;
55
+ db . DropCollection ( DriverTestConfiguration . CollectionNamespace . CollectionName ) ;
56
+ var collection = db . GetCollection < Person > ( DriverTestConfiguration . CollectionNamespace . CollectionName ) ;
57
+ var bsonCollection =
58
+ db . GetCollection < BsonDocument > ( DriverTestConfiguration . CollectionNamespace . CollectionName ) ;
59
+
60
+ var person = new Person { Id = ObjectId . Parse ( "6797b56bf5495bf53aa3078f" ) , Name = "Mario" , Age = 24 } ;
61
+ collection . InsertOne ( person ) ;
62
+
63
+ var retrieved = bsonCollection . FindSync ( "{}" ) . ToList ( ) . Single ( ) ;
64
+ var toString = retrieved . ToString ( ) ;
65
+
66
+ var expectedVal =
67
+ """{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mariotest", "Age" : 24 }""" ;
68
+ Assert . Equal ( expectedVal , toString ) ;
69
+ }
70
+ }
71
+
72
+ public class Person
73
+ {
74
+ [ BsonId ] public ObjectId Id { get ; set ; }
75
+ public string Name { get ; set ; }
76
+ public int Age { get ; set ; }
77
+ }
78
+
79
+ public class CustomStringSerializer : SealedClassSerializerBase < string >
80
+ {
81
+ /// <inheritdoc/>
82
+ public override int GetHashCode ( ) => 0 ;
83
+
84
+ protected override string DeserializeValue ( BsonDeserializationContext context , BsonDeserializationArgs args )
85
+ {
86
+ var bsonReader = context . Reader ;
87
+
88
+ var bsonType = bsonReader . GetCurrentBsonType ( ) ;
89
+ return bsonType switch
90
+ {
91
+ BsonType . String => bsonReader . ReadString ( ) ,
92
+ BsonType . Symbol => bsonReader . ReadSymbol ( ) ,
93
+ _ => throw CreateCannotDeserializeFromBsonTypeException ( bsonType )
94
+ } ;
95
+ }
96
+
97
+ protected override void SerializeValue ( BsonSerializationContext context , BsonSerializationArgs args ,
98
+ string value )
99
+ {
100
+ var bsonWriter = context . Writer ;
101
+ bsonWriter . WriteString ( value + "test" ) ;
102
+ }
103
+ }
104
+ }
105
+ }
0 commit comments