本文整理汇总了C#中BsonReader.ReadNull方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadNull方法的具体用法?C# BsonReader.ReadNull怎么用?C# BsonReader.ReadNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadNull方法的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(CultureInfo));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Document:
bsonReader.ReadStartDocument();
var name = bsonReader.ReadString("Name");
var useUserOverride = bsonReader.ReadBoolean("UseUserOverride");
bsonReader.ReadEndDocument();
return new CultureInfo(name, useUserOverride);
case BsonType.String:
return new CultureInfo(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize CultureInfo 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,
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);
}
}
示例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)
{
var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options);
var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions;
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Array:
var instance = CreateInstance(actualType);
var itemDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object));
Type lastItemType = null;
IBsonSerializer lastItemSerializer = null;
bsonReader.ReadStartArray();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var itemType = itemDiscriminatorConvention.GetActualType(bsonReader, typeof(object));
IBsonSerializer itemSerializer;
if (itemType == lastItemType)
{
itemSerializer = lastItemSerializer;
}
else
{
itemSerializer = BsonSerializer.LookupSerializer(itemType);
lastItemType = itemType;
lastItemSerializer = itemSerializer;
}
var item = itemSerializer.Deserialize(bsonReader, typeof(object), itemType, itemSerializationOptions);
AddItem(instance, item);
}
bsonReader.ReadEndArray();
return FinalizeResult(instance, actualType);
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadString("_t"); // skip over discriminator
bsonReader.ReadName("_v");
var value = Deserialize(bsonReader, actualType, actualType, options);
bsonReader.ReadEndDocument();
return value;
default:
var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType);
throw new FileFormatException(message);
}
}
示例4: Deserialize
// public methods
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
/// <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(BitArray));
BsonType bsonType = bsonReader.GetCurrentBsonType();
BitArray bitArray;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
return new BitArray(bsonReader.ReadBytes());
case BsonType.Document:
bsonReader.ReadStartDocument();
var length = bsonReader.ReadInt32("Length");
var bytes = bsonReader.ReadBytes("Bytes");
bsonReader.ReadEndDocument();
bitArray = new BitArray(bytes);
bitArray.Length = length;
return bitArray;
case BsonType.String:
var s = bsonReader.ReadString();
bitArray = new BitArray(s.Length);
for (int i = 0; i < s.Length; i++)
{
var c = s[i];
switch (c)
{
case '0':
break;
case '1':
bitArray[i] = true;
break;
default:
throw new FileFormatException("String value is not a valid BitArray.");
}
}
return bitArray;
default:
var message = string.Format("Cannot deserialize Byte[] 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(BsonNull));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return BsonNull.Value;
default:
var message = string.Format("Cannot deserialize BsonNull from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例6: 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="options">The serialization options.</param>
/// <returns>An object.</returns>
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
if (nominalType != typeof(object))
{
var message = string.Format("ObjectSerializer can only be used with nominal type System.Object, not type {0}.", nominalType.FullName);
throw new InvalidOperationException(message);
}
var bsonType = bsonReader.GetCurrentBsonType();
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 = BsonSerializer.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, options);
}
示例7: 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(LazyBsonDocument));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Document:
var slice = bsonReader.ReadRawBsonDocument();
return new LazyBsonDocument(slice);
default:
var message = string.Format("Cannot deserialize LazyBsonDocument from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例8: Deserialize
// public methods
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
/// <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(byte[]));
BsonType bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bytes = bsonReader.ReadBytes();
return bytes;
case BsonType.String:
var s = bsonReader.ReadString();
if ((s.Length % 2) != 0)
{
s = "0" + s; // prepend a zero to make length even
}
bytes = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
{
var hex = s.Substring(i, 2);
var b = byte.Parse(hex, NumberStyles.HexNumber);
bytes[i / 2] = b;
}
return bytes;
default:
message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例9: 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)
{
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader))
{
// if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null
return null;
}
// handle BSON null for backward compatibility with existing data (new data would have _csharpnull)
if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull)))
{
bsonReader.ReadNull();
return null;
}
var serializer = BsonSerializer.LookupSerializer(actualType);
return serializer.Deserialize(bsonReader, nominalType, actualType, options);
}
示例10: 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(IPEndPoint));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.String:
var stringValue = bsonReader.ReadString();
var match = Regex.Match(stringValue, @"^(?<address>(.+|\[.*\]))\:(?<port>\d+)$");
if (match.Success)
{
IPAddress address;
if (IPAddress.TryParse(match.Groups["address"].Value, out address))
{
int port;
if (int.TryParse(match.Groups["port"].Value, out port))
{
return new IPEndPoint(address, port);
}
}
}
message = string.Format("Invalid IPEndPoint value '{0}'.", stringValue);
throw new FileFormatException(message);
default:
message = string.Format("Cannot deserialize IPEndPoint from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例11: 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="options">The serialization options.</param>
/// <returns>An object.</returns>
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
VerifyNominalType(nominalType);
if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
var discriminatorConvention = _classMap.GetDiscriminatorConvention();
var actualType = discriminatorConvention.GetActualType(bsonReader, nominalType);
if (actualType != nominalType)
{
var serializer = BsonSerializer.LookupSerializer(actualType);
if (serializer != this)
{
return serializer.Deserialize(bsonReader, nominalType, actualType, options);
}
}
return Deserialize(bsonReader, nominalType, actualType, options);
}
}
示例12: 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)
{
var dictionarySerializationOptions = EnsureSerializationOptions(options);
var dictionaryRepresentation = dictionarySerializationOptions.Representation;
var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions;
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else if (bsonType == BsonType.Document)
{
if (nominalType == typeof(object))
{
bsonReader.ReadStartDocument();
bsonReader.ReadString("_t"); // skip over discriminator
bsonReader.ReadName("_v");
var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType
bsonReader.ReadEndDocument();
return value;
}
var dictionary = CreateInstance(actualType);
var valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object));
bsonReader.ReadStartDocument();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var key = bsonReader.ReadName();
var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(object));
var valueSerializer = BsonSerializer.LookupSerializer(valueType);
var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, keyValuePairSerializationOptions.ValueSerializationOptions);
dictionary.Add(key, value);
}
bsonReader.ReadEndDocument();
return dictionary;
}
else if (bsonType == BsonType.Array)
{
var dictionary = CreateInstance(actualType);
bsonReader.ReadStartArray();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var keyValuePair = (KeyValuePair<object, object>)_keyValuePairSerializer.Deserialize(
bsonReader,
typeof(KeyValuePair<object, object>),
keyValuePairSerializationOptions);
dictionary.Add(keyValuePair.Key, keyValuePair.Value);
}
bsonReader.ReadEndArray();
return dictionary;
}
else
{
var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType);
throw new FileFormatException(message);
}
}
示例13: 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(BsonRegularExpression));
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
string regexPattern, regexOptions;
bsonReader.ReadRegularExpression(out regexPattern, out regexOptions);
return new BsonRegularExpression(regexPattern, regexOptions);
}
}
示例14: DeserializeMemberValue
private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
{
try
{
var nominalType = memberMap.MemberType;
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null && nominalType.IsInterface)
{
bsonReader.ReadNull();
return null;
}
Type actualType;
if (bsonType == BsonType.Null)
{
actualType = nominalType;
}
else
{
var discriminatorConvention = memberMap.GetDiscriminatorConvention();
actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found
}
var serializer = memberMap.GetSerializer(actualType);
return serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions);
}
catch (Exception ex)
{
var message = string.Format(
"An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message
memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message);
throw new FileFormatException(message, ex);
}
}
示例15: 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(string));
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
switch (bsonType)
{
case BsonType.ObjectId:
if (representationSerializationOptions.Representation == BsonType.ObjectId)
{
int timestamp, machine, increment;
short pid;
bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment);
var objectId = new ObjectId(timestamp, machine, pid, increment);
return objectId.ToString();
}
else
{
goto default;
}
case BsonType.String:
return bsonReader.ReadString();
case BsonType.Symbol:
return bsonReader.ReadSymbol();
default:
var message = string.Format("Cannot deserialize string from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
}