本文整理汇总了C#中IBsonSerializationOptions类的典型用法代码示例。如果您正苦于以下问题:C# IBsonSerializationOptions类的具体用法?C# IBsonSerializationOptions怎么用?C# IBsonSerializationOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IBsonSerializationOptions类属于命名空间,在下文中一共展示了IBsonSerializationOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(double));
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Double:
return bsonReader.ReadDouble();
case BsonType.Int32:
return representationSerializationOptions.ToDouble(bsonReader.ReadInt32());
case BsonType.Int64:
return representationSerializationOptions.ToDouble(bsonReader.ReadInt64());
case BsonType.String:
return XmlConvert.ToDouble(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例2: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, // ignored
IBsonSerializationOptions options)
{
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Array: return (BsonValue)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), options);
case BsonType.Binary: return (BsonValue)BsonBinaryDataSerializer.Instance.Deserialize(bsonReader, typeof(BsonBinaryData), options);
case BsonType.Boolean: return (BsonValue)BsonBooleanSerializer.Instance.Deserialize(bsonReader, typeof(BsonBoolean), options);
case BsonType.DateTime: return (BsonValue)BsonDateTimeSerializer.Instance.Deserialize(bsonReader, typeof(BsonDateTime), options);
case BsonType.Document: return (BsonValue)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), options);
case BsonType.Double: return (BsonValue)BsonDoubleSerializer.Instance.Deserialize(bsonReader, typeof(BsonDouble), options);
case BsonType.Int32: return (BsonValue)BsonInt32Serializer.Instance.Deserialize(bsonReader, typeof(BsonInt32), options);
case BsonType.Int64: return (BsonValue)BsonInt64Serializer.Instance.Deserialize(bsonReader, typeof(BsonInt64), options);
case BsonType.JavaScript: return (BsonValue)BsonJavaScriptSerializer.Instance.Deserialize(bsonReader, typeof(BsonJavaScript), options);
case BsonType.JavaScriptWithScope: return (BsonValue)BsonJavaScriptWithScopeSerializer.Instance.Deserialize(bsonReader, typeof(BsonJavaScriptWithScope), options);
case BsonType.MaxKey: return (BsonValue)BsonMaxKeySerializer.Instance.Deserialize(bsonReader, typeof(BsonMaxKey), options);
case BsonType.MinKey: return (BsonValue)BsonMinKeySerializer.Instance.Deserialize(bsonReader, typeof(BsonMinKey), options);
case BsonType.Null: return (BsonValue)BsonNullSerializer.Instance.Deserialize(bsonReader, typeof(BsonNull), options);
case BsonType.ObjectId: return (BsonValue)BsonObjectIdSerializer.Instance.Deserialize(bsonReader, typeof(BsonObjectId), options);
case BsonType.RegularExpression: return (BsonValue)BsonRegularExpressionSerializer.Instance.Deserialize(bsonReader, typeof(BsonRegularExpression), options);
case BsonType.String: return (BsonValue)BsonStringSerializer.Instance.Deserialize(bsonReader, typeof(BsonString), options);
case BsonType.Symbol: return (BsonValue)BsonSymbolSerializer.Instance.Deserialize(bsonReader, typeof(BsonSymbol), options);
case BsonType.Timestamp: return (BsonValue)BsonTimestampSerializer.Instance.Deserialize(bsonReader, typeof(BsonTimestamp), options);
case BsonType.Undefined: return (BsonValue)BsonUndefinedSerializer.Instance.Deserialize(bsonReader, typeof(BsonUndefined), options);
default:
var message = string.Format("Invalid BsonType {0}.", bsonType);
throw new BsonInternalException(message);
}
}
示例3: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(DateTimeOffset));
BsonType bsonType = bsonReader.GetCurrentBsonType();
long ticks;
TimeSpan offset;
switch (bsonType)
{
case BsonType.Array:
bsonReader.ReadStartArray();
ticks = bsonReader.ReadInt64();
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
bsonReader.ReadEndArray();
return new DateTimeOffset(ticks, offset);
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadDateTime("DateTime"); // ignore value
ticks = bsonReader.ReadInt64("Ticks");
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset"));
bsonReader.ReadEndDocument();
return new DateTimeOffset(ticks, offset);
case BsonType.String:
return XmlConvert.ToDateTimeOffset(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType);
throw new Exception(message);
}
}
示例4: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(decimal));
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Array:
var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
var bits = new int[4];
bits[0] = array[0].AsInt32;
bits[1] = array[1].AsInt32;
bits[2] = array[2].AsInt32;
bits[3] = array[3].AsInt32;
return new decimal(bits);
case BsonType.Double:
return representationSerializationOptions.ToDecimal(bsonReader.ReadDouble());
case BsonType.Int32:
return representationSerializationOptions.ToDecimal(bsonReader.ReadInt32());
case BsonType.Int64:
return representationSerializationOptions.ToDecimal(bsonReader.ReadInt64());
case BsonType.String:
return XmlConvert.ToDecimal(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例5: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(bool));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Boolean:
return bsonReader.ReadBoolean();
case BsonType.Double:
return bsonReader.ReadDouble() != 0.0;
case BsonType.Int32:
return bsonReader.ReadInt32() != 0;
case BsonType.Int64:
return bsonReader.ReadInt64() != 0;
case BsonType.Null:
bsonReader.ReadNull();
return false;
case BsonType.String:
return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower());
default:
var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例6: Serialize
/// <summary>
/// Serializes an object to a BsonWriter.
/// </summary>
/// <param name="bsonWriter">The BsonWriter.</param>
/// <param name="nominalType">The nominal type.</param>
/// <param name="value">The object.</param>
/// <param name="options">The serialization options.</param>
public override void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
IBsonSerializationOptions options)
{
if (value == null)
{
bsonWriter.WriteNull();
}
else
{
var lazyBsonArray = (LazyBsonArray)value;
var slice = lazyBsonArray.Slice;
if (slice == null)
{
BsonArraySerializer.Instance.Serialize(bsonWriter, typeof(BsonArray), lazyBsonArray, options);
}
else
{
using (var clonedSlice = slice.GetSlice(0, slice.Length))
{
bsonWriter.WriteRawBsonArray(clonedSlice);
}
}
}
}
示例7: Deserialize
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
object value = null;
var valueType = actualType.GetConceptValueType();
if (valueType == typeof(Guid)) {
var guidBytes = new byte[16];
BsonBinarySubType subType;
bsonReader.ReadBinaryData (out guidBytes, out subType);
value = new Guid (guidBytes);
} else if (valueType == typeof(double))
value = bsonReader.ReadDouble ();
else if (valueType == typeof(float))
value = (float)bsonReader.ReadDouble ();
else if (valueType == typeof(Int32))
value = bsonReader.ReadInt32 ();
else if (valueType == typeof(Int64))
value = bsonReader.ReadInt64 ();
else if (valueType == typeof(bool))
value = bsonReader.ReadBoolean ();
else if (valueType == typeof(string))
value = bsonReader.ReadString ();
else if (valueType == typeof(decimal))
value = decimal.Parse (bsonReader.ReadString ());
var concept = ConceptFactory.CreateConceptInstance(actualType, value);
return concept;
}
示例8: Deserialize
public object Deserialize(global::MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
if (nominalType != typeof(TriggerKey) || actualType != typeof(TriggerKey))
{
var message = string.Format("Can't deserialize a {0} from {1}.", nominalType.FullName, this.GetType().Name);
throw new BsonSerializationException(message);
}
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Document)
{
TriggerKey item;
bsonReader.ReadStartDocument();
item = new TriggerKey(
bsonReader.ReadString("Name"),
bsonReader.ReadString("Group"));
bsonReader.ReadEndDocument();
return item;
}
else if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType);
throw new BsonSerializationException(message);
}
}
示例9: Serialize
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
{
if (value == null)
{
bsonWriter.WriteNull();
return;
}
var metaObject = ((IDynamicMetaObjectProvider)value).GetMetaObject(Expression.Constant(value));
var memberNames = metaObject.GetDynamicMemberNames().ToList();
if (memberNames.Count == 0)
{
bsonWriter.WriteNull();
return;
}
bsonWriter.WriteStartDocument();
foreach (var memberName in memberNames)
{
bsonWriter.WriteName(memberName);
var memberValue = BinderHelper.GetMemberValue(value, memberName);
if (memberValue == null)
bsonWriter.WriteNull();
else
{
var memberType = memberValue.GetType();
var serializer = BsonSerializer.LookupSerializer(memberType);
serializer.Serialize(bsonWriter, memberType, memberValue, options);
}
}
bsonWriter.WriteEndDocument();
}
示例10: _Deserialize
public OXmlSimpleFieldElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
{
OXmlSimpleFieldElement element = new OXmlSimpleFieldElement();
while (true)
{
BsonType bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
break;
string name = bsonReader.ReadName();
switch (name.ToLower())
{
case "type":
if (bsonType != BsonType.String)
throw new PBException($"wrong type value {bsonType}");
string type = bsonReader.ReadString();
if (type.ToLower() != "simplefield")
throw new PBException($"invalid Type {type} when deserialize OXmlSimpleFieldElement");
break;
case "instruction":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.String)
throw new PBException($"wrong Instruction value {bsonType}");
element.Instruction = bsonReader.ReadString();
break;
default:
throw new PBException($"unknow SimpleField value \"{name}\"");
}
}
return element;
}
示例11: Deserialize
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
) {
if (nominalType != typeof(object)) {
var message = string.Format("ObjectSerializer called for nominal type {0}.", nominalType.FullName);
throw new InvalidOperationException(message);
}
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else if (bsonType == BsonType.Document) {
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
if (bsonReader.ReadBsonType() == BsonType.EndOfDocument) {
bsonReader.ReadEndDocument();
return new object();
} else {
bsonReader.ReturnToBookmark(bookmark);
}
}
var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
var actualType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
if (actualType == typeof(object)) {
var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is {0}.", bsonType);
throw new FileFormatException(message);
}
var serializer = BsonSerializer.LookupSerializer(actualType);
return serializer.Deserialize(bsonReader, nominalType, actualType, null);
}
示例12: BsonSerializationInfo
// 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>
/// <param name="serializationOptions">The serialization options.</param>
public BsonSerializationInfo(string elementName, IBsonSerializer serializer, Type nominalType, IBsonSerializationOptions serializationOptions)
{
_elementName = elementName;
_serializer = serializer;
_nominalType = nominalType;
_serializationOptions = serializationOptions;
}
示例13: _Deserialize
public OXmlParagraphElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
{
OXmlParagraphElement paragraph = new OXmlParagraphElement();
while (true)
{
BsonType bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
break;
//if (bsonType != BsonType.String)
// throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType);
//var value = bsonReader.ReadString();
string name = bsonReader.ReadName();
switch (name.ToLower())
{
case "type":
if (bsonType != BsonType.String)
throw new PBException($"wrong type value {bsonType}");
string type = bsonReader.ReadString();
if (type.ToLower() != "paragraph")
throw new PBException($"invalid Type {type} when deserialize OXmlParagraphElement");
break;
case "style":
if (bsonType == BsonType.Null)
break;
if (bsonType != BsonType.String)
throw new PBException($"wrong style value {bsonType}");
paragraph.Style = bsonReader.ReadString();
break;
default:
throw new PBException($"unknow Paragraph value \"{name}\"");
}
}
return paragraph;
}
示例14: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(IPAddress));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.String:
var stringValue = bsonReader.ReadString();
IPAddress address;
if (IPAddress.TryParse(stringValue, out address))
{
return address;
}
message = string.Format("Invalid IPAddress value '{0}'.", stringValue);
throw new FileFormatException(message);
default:
message = string.Format("Cannot deserialize IPAddress from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例15: InvalidOperationException
object IBsonSerializable.Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
) {
throw new InvalidOperationException();
}