/* Copyright 2010-2015 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 MongoDB.Bson.IO; namespace MongoDB.Bson.Serialization { /// /// Represents the information needed to serialize a member. /// public class BsonSerializationInfo { // private fields private string _elementName; private IBsonSerializer _serializer; private Type _nominalType; // constructors /// /// Initializes a new instance of the BsonSerializationInfo class. /// /// The element name. /// The serializer. /// The nominal type. public BsonSerializationInfo(string elementName, IBsonSerializer serializer, Type nominalType) { _elementName = elementName; _serializer = serializer; _nominalType = nominalType; } // public properties /// /// Gets or sets the dotted element name. /// public string ElementName { get { return _elementName; } } /// /// Gets or sets the serializer. /// public IBsonSerializer Serializer { get { return _serializer; } } /// /// Gets or sets the nominal type. /// public Type NominalType { get { return _nominalType; } } /// /// Deserializes the value. /// /// The value. /// A deserialized value. 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; } } /// /// Merges the new BsonSerializationInfo by taking its properties and concatenating its ElementName. /// /// The new info. /// A new BsonSerializationInfo. 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); } /// /// Serializes the value. /// /// The value. /// The serialized value. 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]; } } /// /// Serializes the values. /// /// The values. /// The serialized values. 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; } } /// /// Creates a new BsonSerializationInfo object using the elementName provided and copying all other attributes. /// /// Name of the element. /// A new BsonSerializationInfo. public BsonSerializationInfo WithNewName(string elementName) { return new BsonSerializationInfo( elementName, _serializer, _nominalType); } } }