/* 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.IO; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; namespace MongoDB.Bson { /// /// A static class containing BSON extension methods. /// public static class BsonExtensionMethods { /// /// Serializes an object to a BSON byte array. /// /// The nominal type of the object. /// The object. /// The serializer. /// The writer settings. /// The serialization context configurator. /// The serialization args. /// A BSON byte array. public static byte[] ToBson( this TNominalType obj, IBsonSerializer serializer = null, BsonBinaryWriterSettings writerSettings = null, Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs) ) { args.SetOrValidateNominalType(typeof(TNominalType), ""); return ToBson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args); } /// /// Serializes an object to a BSON byte array. /// /// The object. /// The nominal type of the object.. /// The writer settings. /// The serializer. /// The serialization context configurator. /// The serialization args. /// A BSON byte array. /// nominalType /// serializer public static byte[] ToBson( this object obj, Type nominalType, BsonBinaryWriterSettings writerSettings = null, IBsonSerializer serializer = null, Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs)) { if (nominalType == null) { throw new ArgumentNullException("nominalType"); } args.SetOrValidateNominalType(nominalType, "nominalType"); if (serializer == null) { serializer = BsonSerializer.LookupSerializer(nominalType); } if (serializer.ValueType != nominalType) { var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName); throw new ArgumentException(message, "serializer"); } using (var memoryStream = new MemoryStream()) { using (var bsonWriter = new BsonBinaryWriter(memoryStream, writerSettings ?? BsonBinaryWriterSettings.Defaults)) { var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); serializer.Serialize(context, args, obj); } return memoryStream.ToArray(); } } /// /// Serializes an object to a BsonDocument. /// /// The nominal type of the object. /// The object. /// The serializer. /// The serialization context configurator. /// The serialization args. /// A BsonDocument. public static BsonDocument ToBsonDocument( this TNominalType obj, IBsonSerializer serializer = null, Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs)) { args.SetOrValidateNominalType(typeof(TNominalType), ""); return ToBsonDocument(obj, typeof(TNominalType), serializer, configurator, args); } /// /// Serializes an object to a BsonDocument. /// /// The object. /// The nominal type of the object. /// The serializer. /// The serialization context configurator. /// The serialization args. /// A BsonDocument. /// nominalType /// serializer public static BsonDocument ToBsonDocument( this object obj, Type nominalType, IBsonSerializer serializer = null, Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs)) { if (nominalType == null) { throw new ArgumentNullException("nominalType"); } args.SetOrValidateNominalType(nominalType, "nominalType"); if (obj == null) { return null; } if (serializer == null) { var bsonDocument = obj as BsonDocument; if (bsonDocument != null) { return bsonDocument; // it's already a BsonDocument } var convertibleToBsonDocument = obj as IConvertibleToBsonDocument; if (convertibleToBsonDocument != null) { return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method } serializer = BsonSerializer.LookupSerializer(nominalType); } if (serializer.ValueType != nominalType) { var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName); throw new ArgumentException(message, "serializer"); } // otherwise serialize into a new BsonDocument var document = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(document)) { var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); serializer.Serialize(context, args, obj); } return document; } /// /// Serializes an object to a JSON string. /// /// The nominal type of the object. /// The object. /// The JsonWriter settings. /// The serializer. /// The serializastion context configurator. /// The serialization args. /// /// A JSON string. /// public static string ToJson( this TNominalType obj, JsonWriterSettings writerSettings = null, IBsonSerializer serializer = null, Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs)) { args.SetOrValidateNominalType(typeof(TNominalType), ""); return ToJson(obj, typeof(TNominalType), writerSettings, serializer, configurator, args); } /// /// Serializes an object to a JSON string. /// /// The object. /// The nominal type of the objectt. /// The JsonWriter settings. /// The serializer. /// The serialization context configurator. /// The serialization args. /// /// A JSON string. /// /// nominalType /// serializer public static string ToJson( this object obj, Type nominalType, JsonWriterSettings writerSettings = null, IBsonSerializer serializer = null, Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs)) { if (nominalType == null) { throw new ArgumentNullException("nominalType"); } args.SetOrValidateNominalType(nominalType, "nominalType"); if (serializer == null) { serializer = BsonSerializer.LookupSerializer(nominalType); } if (serializer.ValueType != nominalType) { var message = string.Format("Serializer type {0} value type does not match document types {1}.", serializer.GetType().FullName, nominalType.FullName); throw new ArgumentException(message, "serializer"); } using (var stringWriter = new StringWriter()) { using (var bsonWriter = new JsonWriter(stringWriter, writerSettings ?? JsonWriterSettings.Defaults)) { var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); serializer.Serialize(context, args, obj); } return stringWriter.ToString(); } } } }