本文整理汇总了C#中BsonReader.ReadStartDocument方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadStartDocument方法的具体用法?C# BsonReader.ReadStartDocument怎么用?C# BsonReader.ReadStartDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadStartDocument方法的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(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 FileFormatException(message);
}
}
示例3: 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);
}
}
示例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="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);
}
示例5: Deserialize
// public methods
/// <summary>
/// Deserializes an object of type System.Drawing.Size 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(System.Drawing.Size));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Document:
bsonReader.ReadStartDocument();
var width = bsonReader.ReadInt32("Width");
var height = bsonReader.ReadInt32("Height");
bsonReader.ReadEndDocument();
return new System.Drawing.Size(width, height);
default:
var message = string.Format("Cannot deserialize Size 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="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);
}
}
示例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)
{
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);
}
}
示例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)
{
VerifyTypes(nominalType, actualType, typeof(BsonDateTime));
var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Null)
{
bsonReader.ReadNull();
return null;
}
else
{
long? millisecondsSinceEpoch = null;
long? ticks = null;
switch (bsonType)
{
case BsonType.DateTime:
millisecondsSinceEpoch = bsonReader.ReadDateTime();
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime");
bsonReader.ReadName("Ticks");
var ticksValue = BsonValue.ReadFrom(bsonReader);
if (!ticksValue.IsBsonUndefined)
{
ticks = ticksValue.ToInt64();
}
bsonReader.ReadEndDocument();
break;
case BsonType.Int64:
ticks = bsonReader.ReadInt64();
break;
case BsonType.String:
// note: we're not using XmlConvert because of bugs in Mono
DateTime dateTime;
if (dateTimeSerializationOptions.DateOnly)
{
dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
}
else
{
var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", };
dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
}
ticks = dateTime.Ticks;
break;
default:
var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
BsonDateTime bsonDateTime;
if (ticks.HasValue)
{
bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc));
}
else
{
bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value);
}
if (dateTimeSerializationOptions.DateOnly)
{
var dateTime = bsonDateTime.Value;
if (dateTime.TimeOfDay != TimeSpan.Zero)
{
throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero.");
}
bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeSerializationOptions.Kind)); // not ToLocalTime or ToUniversalTime!
}
else
{
if (bsonDateTime.IsValidDateTime)
{
var dateTime = bsonDateTime.Value;
switch (dateTimeSerializationOptions.Kind)
{
case DateTimeKind.Local:
case DateTimeKind.Unspecified:
dateTime = DateTime.SpecifyKind(BsonUtils.ToLocalTime(dateTime), dateTimeSerializationOptions.Kind);
break;
case DateTimeKind.Utc:
dateTime = BsonUtils.ToUniversalTime(dateTime);
break;
}
//.........这里部分代码省略.........
示例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 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);
}
}
示例10: Deserialize
// public methods
/// <summary>
/// Deserializes an Bitmap from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the Bitmap.</param>
/// <param name="actualType">The actual type of the Bitmap.</param>
/// <param name="options">The serialization options.</param>
/// <returns>A Bitmap.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
if (nominalType != typeof(Image) && nominalType != typeof(Bitmap))
{
var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName);
throw new ArgumentException(message, "nominalType");
}
if (actualType != typeof(Bitmap))
{
var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName);
throw new ArgumentException(message, "actualType");
}
var bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
BsonBinarySubType subType;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bsonReader.ReadBinaryData(out bytes, out subType);
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadString("_t");
bsonReader.ReadBinaryData("bitmap", out bytes, out subType);
bsonReader.ReadEndDocument();
break;
default:
var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType);
throw new FileFormatException(message);
}
if (subType != BsonBinarySubType.Binary)
{
var message = string.Format("Binary sub type must be Binary, not {0}.", subType);
throw new FileFormatException(message);
}
var stream = new MemoryStream(bytes);
return new Bitmap(stream);
}
示例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="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
{
//.........这里部分代码省略.........
示例12: 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;
}
示例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(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);
}
}
示例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(DateTime));
var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);
var bsonType = bsonReader.GetCurrentBsonType();
DateTime value;
switch (bsonType)
{
case BsonType.DateTime:
// use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly
value = new BsonDateTime(bsonReader.ReadDateTime()).ToUniversalTime();
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadDateTime("DateTime"); // ignore value (use Ticks instead)
value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc);
bsonReader.ReadEndDocument();
break;
case BsonType.Int64:
value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc);
break;
case BsonType.String:
// note: we're not using XmlConvert because of bugs in Mono
if (dateTimeSerializationOptions.DateOnly)
{
value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc);
}
else
{
var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" };
value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
}
break;
default:
var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
if (dateTimeSerializationOptions.DateOnly)
{
if (value.TimeOfDay != TimeSpan.Zero)
{
throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero.");
}
value = DateTime.SpecifyKind(value, dateTimeSerializationOptions.Kind); // not ToLocalTime or ToUniversalTime!
}
else
{
switch (dateTimeSerializationOptions.Kind)
{
case DateTimeKind.Local:
case DateTimeKind.Unspecified:
value = DateTime.SpecifyKind(BsonUtils.ToLocalTime(value), dateTimeSerializationOptions.Kind);
break;
case DateTimeKind.Utc:
value = BsonUtils.ToUniversalTime(value);
break;
}
}
return value;
}
示例15: GetActualType
// public methods
/// <summary>
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
/// </summary>
/// <param name="bsonReader">The reader.</param>
/// <param name="nominalType">The nominal type.</param>
/// <returns>The actual type.</returns>
public Type GetActualType(BsonReader bsonReader, Type nominalType)
{
// the BsonReader is sitting at the value whose actual type needs to be found
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonReader.State == BsonReaderState.Value)
{
Type primitiveType = null;
switch (bsonType)
{
case BsonType.Boolean: primitiveType = typeof(bool); break;
case BsonType.Binary:
var bookmark = bsonReader.GetBookmark();
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
{
primitiveType = typeof(Guid);
}
bsonReader.ReturnToBookmark(bookmark);
break;
case BsonType.DateTime: primitiveType = typeof(DateTime); break;
case BsonType.Double: primitiveType = typeof(double); break;
case BsonType.Int32: primitiveType = typeof(int); break;
case BsonType.Int64: primitiveType = typeof(long); break;
case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
case BsonType.String: primitiveType = typeof(string); break;
}
// Type.IsAssignableFrom is extremely expensive, always perform a direct type check before calling Type.IsAssignableFrom
if (primitiveType != null && (primitiveType == nominalType || nominalType.IsAssignableFrom(primitiveType)))
{
return primitiveType;
}
}
if (bsonType == BsonType.Document)
{
// ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer)
BsonSerializer.EnsureKnownTypesAreRegistered(nominalType);
// we can skip looking for a discriminator if nominalType has no discriminated sub types
if (BsonSerializer.IsTypeDiscriminated(nominalType))
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
if (bsonReader.FindElement(_elementName))
{
var discriminator = BsonValue.ReadFrom(bsonReader);
if (discriminator.IsBsonArray)
{
discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
}
actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
}
bsonReader.ReturnToBookmark(bookmark);
return actualType;
}
}
return nominalType;
}