-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BsonValueCSharpNullSerializer.cs
221 lines (204 loc) · 9.18 KB
/
BsonValueCSharpNullSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* 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 MongoDB.Bson.IO;
namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
/// Represents a serializer for a BsonValue that can round trip C# null.
/// </summary>
/// <typeparam name="TBsonValue">The type of the BsonValue.</typeparam>
public class BsonValueCSharpNullSerializer<TBsonValue> : SerializerBase<TBsonValue> where TBsonValue : BsonValue
{
// private fields
private readonly IBsonSerializer<TBsonValue> _wrappedSerializer;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BsonValueCSharpNullSerializer{TBsonValue}"/> class.
/// </summary>
/// <param name="wrappedSerializer">The wrapped serializer.</param>
public BsonValueCSharpNullSerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
{
_wrappedSerializer = wrappedSerializer;
}
// public methods
/// <summary>
/// Deserializes a value.
/// </summary>
/// <param name="context">The deserialization context.</param>
/// <param name="args">The deserialization args.</param>
/// <returns>A deserialized value.</returns>
public override TBsonValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonReader = context.Reader;
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader))
{
// if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null
return null;
}
// handle BSON null for backward compatibility with existing data (new data would have _csharpnull)
if (bsonType == BsonType.Null && (args.NominalType != typeof(BsonValue) && args.NominalType != typeof(BsonNull)))
{
bsonReader.ReadNull();
return null;
}
return _wrappedSerializer.Deserialize(context);
}
/// <summary>
/// Serializes a value.
/// </summary>
/// <param name="context">The serialization context.</param>
/// <param name="args">The serialization args.</param>
/// <param name="value">The object.</param>
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value)
{
if (value == null)
{
var bsonWriter = context.Writer;
bsonWriter.WriteStartDocument();
bsonWriter.WriteBoolean("_csharpnull", true);
bsonWriter.WriteEndDocument();
}
else
{
_wrappedSerializer.Serialize(context, value);
}
}
// private methods
private bool IsCSharpNullRepresentation(IBsonReader bsonReader)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.Boolean)
{
var name = bsonReader.ReadName();
if (name == "_csharpnull" || name == "$csharpnull")
{
var value = bsonReader.ReadBoolean();
if (value)
{
bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
{
bsonReader.ReadEndDocument();
return true;
}
}
}
}
bsonReader.ReturnToBookmark(bookmark);
return false;
}
}
/// <summary>
/// Represents a serializer for a BsonValue that can round trip C# null and implements IBsonArraySerializer and IBsonDocumentSerializer.
/// </summary>
/// <typeparam name="TBsonValue">The type of the bson value.</typeparam>
public class BsonValueCSharpNullArrayAndDocumentSerializer<TBsonValue> : BsonValueCSharpNullSerializer<TBsonValue>, IBsonArraySerializer, IBsonDocumentSerializer
where TBsonValue : BsonValue
{
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BsonValueCSharpNullArrayAndDocumentSerializer{TBsonValue}"/> class.
/// </summary>
/// <param name="wrappedSerializer">The wrapped serializer.</param>
public BsonValueCSharpNullArrayAndDocumentSerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
: base(wrappedSerializer)
{
}
/// <summary>
/// Tries to get the serialization info for the individual items of the array.
/// </summary>
/// <param name="serializationInfo">The serialization information.</param>
/// <returns>
/// The serialization info for the items.
/// </returns>
public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
{
return BsonValueSerializer.Instance.TryGetItemSerializationInfo(out serializationInfo);
}
/// <summary>
/// Tries to get the serialization info for a member.
/// </summary>
/// <param name="memberName">Name of the member.</param>
/// <param name="serializationInfo">The serialization information.</param>
/// <returns>
/// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
/// </returns>
public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
{
return BsonValueSerializer.Instance.TryGetMemberSerializationInfo(memberName, out serializationInfo);
}
}
/// <summary>
/// Represents a serializer for a BsonValue that can round trip C# null and implements IBsonArraySerializer.
/// </summary>
/// <typeparam name="TBsonValue">The type of the bson value.</typeparam>
public class BsonValueCSharpNullArraySerializer<TBsonValue> : BsonValueCSharpNullSerializer<TBsonValue>, IBsonArraySerializer
where TBsonValue : BsonValue
{
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BsonValueCSharpNullArraySerializer{TBsonValue}"/> class.
/// </summary>
/// <param name="wrappedSerializer">The wrapped serializer.</param>
public BsonValueCSharpNullArraySerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
: base(wrappedSerializer)
{
}
/// <summary>
/// Tries to get the serialization info for the individual items of the array.
/// </summary>
/// <param name="serializationInfo">The serialization information.</param>
/// <returns>
/// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
/// </returns>
public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
{
return BsonValueSerializer.Instance.TryGetItemSerializationInfo(out serializationInfo);
}
}
/// <summary>
/// Represents a serializer for a BsonValue that can round trip C# null and implements IBsonDocumentSerializer.
/// </summary>
/// <typeparam name="TBsonValue">The type of the bson value.</typeparam>
public class BsonValueCSharpNullDocumentSerializer<TBsonValue> : BsonValueCSharpNullSerializer<TBsonValue>, IBsonDocumentSerializer
where TBsonValue : BsonValue
{
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BsonValueCSharpNullDocumentSerializer{TBsonValue}"/> class.
/// </summary>
/// <param name="wrappedSerializer">The wrapped serializer.</param>
public BsonValueCSharpNullDocumentSerializer(IBsonSerializer<TBsonValue> wrappedSerializer)
: base(wrappedSerializer)
{
}
/// <summary>
/// Tries to get the serialization info for a member.
/// </summary>
/// <param name="memberName">Name of the member.</param>
/// <param name="serializationInfo">The serialization information.</param>
/// <returns>
/// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
/// </returns>
public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
{
return BsonValueSerializer.Instance.TryGetMemberSerializationInfo(memberName, out serializationInfo);
}
}
}