/* Copyright 2010-2014 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
{
///
/// Converts an object to a BSON document byte array.
///
/// The nominal type of the object.
/// The object.
/// A byte array.
public static byte[] ToBson(this TNominalType obj)
{
return ToBson(obj, typeof(TNominalType));
}
///
/// Converts an object to a BSON document byte array.
///
/// The nominal type of the object.
/// The object.
/// The serialization options.
/// A byte array.
public static byte[] ToBson(this TNominalType obj, IBsonSerializationOptions options)
{
return ToBson(obj, typeof(TNominalType), options);
}
///
/// Converts an object to a BSON document byte array.
///
/// The nominal type of the object.
/// The object.
/// The serialization options.
/// The BsonBinaryWriter settings.
/// A byte array.
public static byte[] ToBson(
this TNominalType obj,
IBsonSerializationOptions options,
BsonBinaryWriterSettings settings)
{
return ToBson(obj, typeof(TNominalType), options, settings);
}
///
/// Converts an object to a BSON document byte array.
///
/// The nominal type of the object.
/// The object.
/// The BsonBinaryWriter settings.
/// A byte array.
public static byte[] ToBson(this TNominalType obj, BsonBinaryWriterSettings settings)
{
return ToBson(obj, typeof(TNominalType), settings);
}
///
/// Converts an object to a BSON document byte array.
///
/// The object.
/// The nominal type of the object.
/// A byte array.
public static byte[] ToBson(this object obj, Type nominalType)
{
return ToBson(obj, nominalType, BsonBinaryWriterSettings.Defaults);
}
///
/// Converts an object to a BSON document byte array.
///
/// The object.
/// The nominal type of the object.
/// The serialization options.
/// A byte array.
public static byte[] ToBson(this object obj, Type nominalType, IBsonSerializationOptions options)
{
return ToBson(obj, nominalType, options, BsonBinaryWriterSettings.Defaults);
}
///
/// Converts an object to a BSON document byte array.
///
/// The object.
/// The nominal type of the object.
/// The serialization options.
/// The BsonBinaryWriter settings.
/// A byte array.
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();
}
}
///
/// Converts an object to a BSON document byte array.
///
/// The object.
/// The nominal type of the object.
/// The BsonBinaryWriter settings.
/// A byte array.
public static byte[] ToBson(this object obj, Type nominalType, BsonBinaryWriterSettings settings)
{
return ToBson(obj, nominalType, null, settings);
}
///
/// Converts an object to a BsonDocument.
///
/// The nominal type of the object.
/// The object.
/// A BsonDocument.
public static BsonDocument ToBsonDocument(this TNominalType obj)
{
return ToBsonDocument(obj, typeof(TNominalType));
}
///
/// Converts an object to a BsonDocument.
///
/// The nominal type of the object.
/// The object.
/// The serialization options.
/// A BsonDocument.
public static BsonDocument ToBsonDocument(
this TNominalType obj,
IBsonSerializationOptions options)
{
return ToBsonDocument(obj, typeof(TNominalType), options);
}
///
/// Converts an object to a BsonDocument.
///
/// The object.
/// The nominal type of the object.
/// A BsonDocument.
public static BsonDocument ToBsonDocument(this object obj, Type nominalType)
{
return ToBsonDocument(obj, nominalType, null);
}
///
/// Converts an object to a BsonDocument.
///
/// The object.
/// The nominal type of the object.
/// The serialization options.
/// A BsonDocument.
public static BsonDocument ToBsonDocument(
this object obj,
Type nominalType,
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(writer, nominalType, obj, options);
}
return document;
}
///
/// Converts an object to a JSON string.
///
/// The nominal type of the object.
/// The object.
/// A JSON string.
public static string ToJson(this TNominalType obj)
{
return ToJson(obj, typeof(TNominalType));
}
///
/// Converts an object to a JSON string.
///
/// The nominal type of the object.
/// The object.
/// The serialization options.
/// A JSON string.
public static string ToJson(this TNominalType obj, IBsonSerializationOptions options)
{
return ToJson(obj, typeof(TNominalType), options);
}
///
/// Converts an object to a JSON string.
///
/// The nominal type of the object.
/// The object.
/// The serialization options.
/// The JsonWriter settings.
/// A JSON string.
public static string ToJson(
this TNominalType obj,
IBsonSerializationOptions options,
JsonWriterSettings settings)
{
return ToJson(obj, typeof(TNominalType), options, settings);
}
///
/// Converts an object to a JSON string.
///
/// The nominal type of the object.
/// The object.
/// The JsonWriter settings.
/// A JSON string.
public static string ToJson(this TNominalType obj, JsonWriterSettings settings)
{
return ToJson(obj, typeof(TNominalType), settings);
}
///
/// Converts an object to a JSON string.
///
/// The object.
/// The nominal type of the object.
/// A JSON string.
public static string ToJson(this object obj, Type nominalType)
{
return ToJson(obj, nominalType, JsonWriterSettings.Defaults);
}
///
/// Converts an object to a JSON string.
///
/// The object.
/// The nominal type of the object.
/// The serialization options.
/// A JSON string.
public static string ToJson(this object obj, Type nominalType, IBsonSerializationOptions options)
{
return ToJson(obj, nominalType, options, JsonWriterSettings.Defaults);
}
///
/// Converts an object to a JSON string.
///
/// The object.
/// The nominal type of the object.
/// The serialization options.
/// The JsonWriter settings.
/// A JSON string.
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();
}
}
///
/// Converts an object to a JSON string.
///
/// The object.
/// The nominal type of the object.
/// The JsonWriter settings.
/// A JSON string.
public static string ToJson(this object obj, Type nominalType, JsonWriterSettings settings)
{
return ToJson(obj, nominalType, null, settings);
}
}
}