-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BsonSerializationInfo.cs
215 lines (199 loc) · 7.72 KB
/
BsonSerializationInfo.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
/* 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;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson.IO;
namespace MongoDB.Bson.Serialization
{
/// <summary>
/// Represents the information needed to serialize a member.
/// </summary>
public class BsonSerializationInfo
{
#region static
/// <summary>
/// Creates a new instance of the BsonSerializationinfo class with an element path instead of an element name.
/// </summary>
/// <param name="elementPath">The element path.</param>
/// <param name="serializer">The serializer.</param>
/// <param name="nominalType">The nominal type.</param>
/// <returns>A BsonSerializationInfo.</returns>
public static BsonSerializationInfo CreateWithPath(IEnumerable<string> elementPath, IBsonSerializer serializer, Type nominalType)
{
return new BsonSerializationInfo(elementPath.ToList(), serializer, nominalType);
}
#endregion
// private fields
// note: while _elementName could have been modeled with an _elementPath of length 1, treating this is a special case avoids some allocations
private readonly string _elementName;
private readonly IReadOnlyList<string> _elementPath;
private readonly IBsonSerializer _serializer;
private readonly Type _nominalType;
// constructors
/// <summary>
/// Initializes a new instance of the BsonSerializationInfo class.
/// </summary>
/// <param name="elementName">The element name.</param>
/// <param name="serializer">The serializer.</param>
/// <param name="nominalType">The nominal type.</param>
public BsonSerializationInfo(string elementName, IBsonSerializer serializer, Type nominalType)
{
_elementName = elementName;
_serializer = serializer;
_nominalType = nominalType;
}
private BsonSerializationInfo(IReadOnlyList<string> elementPath, IBsonSerializer serializer, Type nominalType)
{
_elementPath = elementPath;
_serializer = serializer;
_nominalType = nominalType;
}
// public properties
/// <summary>
/// Gets the element name.
/// </summary>
public string ElementName
{
get
{
if (_elementPath != null)
{
throw new InvalidOperationException("When ElementPath is not null you must use it instead.");
}
return _elementName;
}
}
/// <summary>
/// Gets element path.
/// </summary>
public IReadOnlyList<string> ElementPath
{
get { return _elementPath; }
}
/// <summary>
/// Gets or sets the serializer.
/// </summary>
public IBsonSerializer Serializer
{
get { return _serializer; }
}
/// <summary>
/// Gets or sets the nominal type.
/// </summary>
public Type NominalType
{
get { return _nominalType; }
}
/// <summary>
/// Deserializes the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A deserialized value.</returns>
public object DeserializeValue(BsonValue value)
{
var tempDocument = new BsonDocument("value", value);
using (var reader = new BsonDocumentReader(tempDocument))
{
var context = BsonDeserializationContext.CreateRoot(reader);
reader.ReadStartDocument();
reader.ReadName("value");
var deserializedValue = _serializer.Deserialize(context);
reader.ReadEndDocument();
return deserializedValue;
}
}
/// <summary>
/// Merges the new BsonSerializationInfo by taking its properties and concatenating its ElementName.
/// </summary>
/// <param name="newSerializationInfo">The new info.</param>
/// <returns>A new BsonSerializationInfo.</returns>
[Obsolete("This method is no longer relevant because field names are now allowed to contain dots.")]
public BsonSerializationInfo Merge(BsonSerializationInfo newSerializationInfo)
{
string elementName = null;
if (ElementName != null && newSerializationInfo.ElementName != null)
{
elementName = ElementName + "." + newSerializationInfo.ElementName;
}
else if (ElementName != null)
{
elementName = ElementName;
}
else if (newSerializationInfo.ElementName != null)
{
elementName = newSerializationInfo.ElementName;
}
return new BsonSerializationInfo(
elementName,
newSerializationInfo._serializer,
newSerializationInfo._nominalType);
}
/// <summary>
/// Serializes the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The serialized value.</returns>
public BsonValue SerializeValue(object value)
{
var tempDocument = new BsonDocument();
using (var bsonWriter = new BsonDocumentWriter(tempDocument))
{
var context = BsonSerializationContext.CreateRoot(bsonWriter);
bsonWriter.WriteStartDocument();
bsonWriter.WriteName("value");
_serializer.Serialize(context, value);
bsonWriter.WriteEndDocument();
return tempDocument[0];
}
}
/// <summary>
/// Serializes the values.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>The serialized values.</returns>
public BsonArray SerializeValues(IEnumerable values)
{
var tempDocument = new BsonDocument();
using (var bsonWriter = new BsonDocumentWriter(tempDocument))
{
var context = BsonSerializationContext.CreateRoot(bsonWriter);
bsonWriter.WriteStartDocument();
bsonWriter.WriteName("values");
bsonWriter.WriteStartArray();
foreach (var value in values)
{
_serializer.Serialize(context, value);
}
bsonWriter.WriteEndArray();
bsonWriter.WriteEndDocument();
return tempDocument[0].AsBsonArray;
}
}
/// <summary>
/// Creates a new BsonSerializationInfo object using the elementName provided and copying all other attributes.
/// </summary>
/// <param name="elementName">Name of the element.</param>
/// <returns>A new BsonSerializationInfo.</returns>
public BsonSerializationInfo WithNewName(string elementName)
{
return new BsonSerializationInfo(
elementName,
_serializer,
_nominalType);
}
}
}