public void WriteTo(BsonBuffer buffer) { using (BsonWriter bsonWriter = BsonWriter.Create(buffer)) { BsonSerializer.Serialize(bsonWriter, this); } }
public void WriteTo(Stream stream) { using (BsonWriter bsonWriter = BsonWriter.Create(stream)) { BsonSerializer.Serialize(bsonWriter, this); } }
/// <summary> /// Converts an object to a BsonDocument. /// </summary> /// <typeparam name="T">The nominal type of the object.</typeparam> /// <param name="obj">The object.</param> /// <param name="options">The serialization options.</param> /// <returns>A BsonDocument.</returns> public static BsonDocument ToBsonDocument <T>( this T obj, IBsonSerializationOptions options ) { if (obj == null) { return(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 } // otherwise serialize into a new BsonDocument var document = new BsonDocument(); using (var writer = BsonWriter.Create(document)) { BsonSerializer.Serialize <T>(writer, obj, options); } return(document); }
/// <summary> /// Writes the document to a Stream. /// </summary> /// <param name="stream">The stream.</param> public void WriteTo(Stream stream) { using (BsonWriter bsonWriter = BsonWriter.Create(stream)) { WriteTo(bsonWriter); } }
/// <summary> /// Writes the document to a BsonBuffer. /// </summary> /// <param name="buffer">The buffer.</param> public void WriteTo(BsonBuffer buffer) { using (BsonWriter bsonWriter = BsonWriter.Create(buffer)) { WriteTo(bsonWriter); } }
public void WriteTo(string filename) { using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) { using (BsonWriter bsonWriter = BsonWriter.Create(stream)) { BsonSerializer.Serialize(bsonWriter, this); } } }
/// <summary> /// Converts an object to a JSON string. /// </summary> /// <param name="obj">The object.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="options">The serialization options.</param> /// <param name="settings">The JsonWriter settings.</param> /// <returns>A JSON string.</returns> public static string ToJson(this object obj, Type nominalType, IBsonSerializationOptions options, JsonWriterSettings settings) { using (var stringWriter = new StringWriter()) { using (var bsonWriter = BsonWriter.Create(stringWriter, settings)) { BsonSerializer.Serialize(bsonWriter, nominalType, obj, options); } return(stringWriter.ToString()); } }
/// <summary> /// Converts an object to a BSON document byte array. /// </summary> /// <param name="obj">The object.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="options">The serialization options.</param> /// <param name="settings">The BsonBinaryWriter settings.</param> /// <returns>A byte array.</returns> public static byte[] ToBson(this object obj, Type nominalType, IBsonSerializationOptions options, BsonBinaryWriterSettings settings) { using (var buffer = new BsonBuffer()) { using (var bsonWriter = BsonWriter.Create(buffer, settings)) { BsonSerializer.Serialize(bsonWriter, nominalType, obj, options); } return(buffer.ToByteArray()); } }
/// <summary> /// Converts an object to a BSON document byte array. /// </summary> /// <typeparam name="T">The nominal type of the object.</typeparam> /// <param name="obj">The object.</param> /// <param name="options">The serialization options.</param> /// <param name="settings">The BsonBinaryWriter settings.</param> /// <returns>A byte array.</returns> public static byte[] ToBson <T>( this T obj, IBsonSerializationOptions options, BsonBinaryWriterSettings settings ) { using (var buffer = new BsonBuffer()) { using (var bsonWriter = BsonWriter.Create(buffer, settings)) { BsonSerializer.Serialize <T>(bsonWriter, obj, options); } return(buffer.ToByteArray()); } }
/// <summary> /// Converts an object to a JSON string. /// </summary> /// <typeparam name="T">The nominal type of the object.</typeparam> /// <param name="obj">The object.</param> /// <param name="options">The serialization options.</param> /// <param name="settings">The JsonWriter settings.</param> /// <returns>A JSON string.</returns> public static string ToJson <T>( this T obj, IBsonSerializationOptions options, JsonWriterSettings settings ) { using (var stringWriter = new StringWriter()) { using (var bsonWriter = BsonWriter.Create(stringWriter, settings)) { BsonSerializer.Serialize <T>(bsonWriter, obj, options); } return(stringWriter.ToString()); } }