本文整理汇总了C#中BsonReader.ReadBsonType方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadBsonType方法的具体用法?C# BsonReader.ReadBsonType怎么用?C# BsonReader.ReadBsonType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadBsonType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
{
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);
}
}
示例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="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);
}
示例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(BsonDocument));
var bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Document:
var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
if (documentSerializationOptions == null)
{
message = string.Format(
"Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
BsonUtils.GetFriendlyTypeName(options.GetType()));
throw new BsonSerializationException(message);
}
bsonReader.ReadStartDocument();
var document = new BsonDocument(documentSerializationOptions.AllowDuplicateNames);
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
document.Add(name, value);
}
bsonReader.ReadEndDocument();
return document;
default:
message = string.Format("Cannot deserialize BsonDocument from BsonType {0}.", bsonType);
throw new FileFormatException(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)
{
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);
}
}
示例5: IsCSharpNullRepresentation
// private methods
private bool IsCSharpNullRepresentation(BsonReader bsonReader)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.Boolean)
{
var name = bsonReader.ReadName();
if (name == "_csharpnull" || name == "$csharpnull")
{
var value = bsonReader.ReadBoolean();
if (value)
{
bsonType = bsonReader.ReadBsonType();
if (bsonType == BsonType.EndOfDocument)
{
bsonReader.ReadEndDocument();
return true;
}
}
}
}
bsonReader.ReturnToBookmark(bookmark);
return false;
}
示例6: 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="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyNominalType(nominalType);
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == Bson.BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
if (actualType != _classMap.ClassType)
{
var message = string.Format("BsonClassMapSerializer.Deserialize for type {0} was called with actualType {1}.",
BsonUtils.GetFriendlyTypeName(_classMap.ClassType), BsonUtils.GetFriendlyTypeName(actualType));
throw new BsonSerializationException(message);
}
if (actualType.IsValueType)
{
var message = string.Format("Value class {0} cannot be deserialized.", actualType.FullName);
throw new BsonSerializationException(message);
}
if (_classMap.IsAnonymous)
{
throw new InvalidOperationException("An anonymous class cannot be deserialized.");
}
if (bsonType != BsonType.Document)
{
var message = string.Format(
"Expected a nested document representing the serialized form of a {0} value, but found a value of type {1} instead.",
actualType.FullName, bsonType);
throw new FileFormatException(message);
}
Dictionary<string, object> values = null;
object obj = null;
ISupportInitialize supportsInitialization = null;
if (_classMap.HasCreatorMaps)
{
// for creator-based deserialization we first gather the values in a dictionary and then call a matching creator
values = new Dictionary<string, object>();
}
else
{
// for mutable classes we deserialize the values directly into the result object
obj = _classMap.CreateInstance();
supportsInitialization = obj as ISupportInitialize;
if (supportsInitialization != null)
{
supportsInitialization.BeginInit();
}
}
var discriminatorConvention = _classMap.GetDiscriminatorConvention();
var allMemberMaps = _classMap.AllMemberMaps;
var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex;
var memberMapBitArray = FastMemberMapHelper.GetBitArray(allMemberMaps.Count);
bsonReader.ReadStartDocument();
var elementTrie = _classMap.ElementTrie;
bool memberMapFound;
int memberMapIndex;
while (bsonReader.ReadBsonType(elementTrie, out memberMapFound, out memberMapIndex) != BsonType.EndOfDocument)
{
var elementName = bsonReader.ReadName();
if (memberMapFound)
{
var memberMap = allMemberMaps[memberMapIndex];
if (memberMapIndex != extraElementsMemberMapIndex)
{
if (obj != null)
{
if (memberMap.IsReadOnly)
{
bsonReader.SkipValue();
}
else
{
var value = DeserializeMemberValue(bsonReader, memberMap);
memberMap.Setter(obj, value);
}
}
else
{
//.........这里部分代码省略.........
示例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(Version));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Document:
bsonReader.ReadStartDocument();
int major = -1, minor = -1, build = -1, revision = -1;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
switch (name)
{
case "Major": major = bsonReader.ReadInt32(); break;
case "Minor": minor = bsonReader.ReadInt32(); break;
case "Build": build = bsonReader.ReadInt32(); break;
case "Revision": revision = bsonReader.ReadInt32(); break;
default:
message = string.Format("Unrecognized element '{0}' while deserializing a Version value.", name);
throw new FileFormatException(message);
}
}
bsonReader.ReadEndDocument();
if (major == -1)
{
message = string.Format("Version missing Major element.");
throw new FileFormatException(message);
}
else if (minor == -1)
{
message = string.Format("Version missing Minor element.");
throw new FileFormatException(message);
}
else if (build == -1)
{
return new Version(major, minor);
}
else if (revision == -1)
{
return new Version(major, minor, build);
}
else
{
return new Version(major, minor, build, revision);
}
case BsonType.String:
return new Version(bsonReader.ReadString());
default:
message = string.Format("Cannot deserialize Version from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例8: 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:
bsonReader.ReadStartArray();
var stack = new Stack();
var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object));
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
var serializer = BsonSerializer.LookupSerializer(elementType);
var element = serializer.Deserialize(bsonReader, typeof(object), elementType, itemSerializationOptions);
stack.Push(element);
}
bsonReader.ReadEndArray();
return stack;
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);
}
}