本文整理汇总了C#中MongoDB.Bson.IO.BsonReader.ReadBinaryData方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadBinaryData方法的具体用法?C# BsonReader.ReadBinaryData怎么用?C# BsonReader.ReadBinaryData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoDB.Bson.IO.BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadBinaryData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
IBsonSerializationOptions options
) {
BsonType bsonType = bsonReader.CurrentBsonType;
BitArray bitArray;
byte[] bytes;
BsonBinarySubType subType;
string message;
switch (bsonType) {
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
message = string.Format("Invalid Binary sub type: {0}", subType);
throw new FileFormatException(message);
}
return new BitArray(bytes);
case BsonType.Document:
bsonReader.ReadStartDocument();
var length = bsonReader.ReadInt32("Length");
bsonReader.ReadBinaryData("Bytes", out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
message = string.Format("Invalid Binary sub type: {0}", subType);
throw new FileFormatException(message);
}
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:
message = string.Format("Cannot deserialize Byte[] from BsonType: {0}", bsonType);
throw new FileFormatException(message);
}
}
示例2: 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;
}
示例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(BsonBinaryData));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
byte[] bytes;
BsonBinarySubType subType;
GuidRepresentation guidRepresentation;
bsonReader.ReadBinaryData(out bytes, out subType, out guidRepresentation);
return new BsonBinaryData(bytes, subType, guidRepresentation);
default:
var message = string.Format("Cannot deserialize BsonBinaryData 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)
{
VerifyTypes(nominalType, actualType, typeof(byte));
var bsonType = bsonReader.GetCurrentBsonType();
byte value;
var lostData = false;
switch (bsonType)
{
case BsonType.Binary:
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (bytes.Length != 1)
{
throw new FileFormatException("Binary data for Byte must be exactly one byte long.");
}
value = bytes[0];
break;
case BsonType.Int32:
var int32Value = bsonReader.ReadInt32();
value = (byte)int32Value;
lostData = (int)value != int32Value;
break;
case BsonType.Int64:
var int64Value = bsonReader.ReadInt64();
value = (byte)int64Value;
lostData = (int)value != int64Value;
break;
case BsonType.String:
var s = bsonReader.ReadString();
if (s.Length == 1)
{
s = "0" + s;
}
value = byte.Parse(s, NumberStyles.HexNumber);
break;
default:
var message = string.Format("Cannot deserialize Byte from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
if (lostData)
{
var message = string.Format("Data loss occurred when trying to convert from {0} to Byte.", bsonType);
throw new FileFormatException(message);
}
return value;
}
示例5: Deserialize
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
public override object Deserialize(
BsonReader bsonReader,
Type nominalType
) {
BsonType bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else if (bsonType == BsonType.Binary) {
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
var message = string.Format("Invalid Binary sub type: {0}", subType);
throw new FileFormatException(message);
}
return new BitArray(bytes);
} else if (bsonType == BsonType.Document) {
bsonReader.ReadStartDocument();
var length = bsonReader.ReadInt32("Length");
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData("Bytes", out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
var message = string.Format("Invalid Binary sub type: {0}", subType);
throw new FileFormatException(message);
}
bsonReader.ReadEndDocument();
var bitArray = new BitArray(bytes);
bitArray.Length = length;
return bitArray;
} else {
var message = string.Format("Cannot deserialize Byte[] 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(BsonBinaryData));
var bsonType = bsonReader.GetCurrentBsonType();
switch (bsonType)
{
case BsonType.Binary:
return bsonReader.ReadBinaryData();
default:
var message = string.Format("Cannot deserialize BsonBinaryData from BsonType {0}.", bsonType);
throw new Exception(message);
}
}
示例7: 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:
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
{
message = string.Format("Invalid Binary sub type {0}.", subType);
throw new FileFormatException(message);
}
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);
}
}
示例8: Deserialize
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
public override object Deserialize(
BsonReader bsonReader,
Type nominalType
) {
BsonType bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else if (bsonType == BsonType.Binary) {
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
var message = string.Format("Invalid Binary sub type: {0}", subType);
throw new FileFormatException(message);
}
return bytes;
} else {
var 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)
{
VerifyTypes(nominalType, actualType, typeof(Guid));
var bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Binary:
var binaryData = bsonReader.ReadBinaryData();
var bytes = binaryData.Bytes;
var subType = binaryData.SubType;
var guidRepresentation = binaryData.GuidRepresentation;
if (bytes.Length != 16)
{
message = string.Format("Expected length to be 16, not {0}.", bytes.Length);
throw new FileFormatException(message);
}
if (subType != BsonBinarySubType.UuidStandard && subType != BsonBinarySubType.UuidLegacy)
{
message = string.Format("Expected binary sub type to be UuidStandard or UuidLegacy, not {0}.", subType);
throw new FileFormatException(message);
}
if (guidRepresentation == GuidRepresentation.Unspecified)
{
throw new BsonSerializationException("GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified.");
}
return GuidConverter.FromBytes(bytes, guidRepresentation);
case BsonType.String:
return new Guid(bsonReader.ReadString());
default:
message = string.Format("Cannot deserialize Guid from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例10: TestGuid
public void TestGuid()
{
var guid = new Guid("B5F21E0C2A0D42d6AD03D827008D8AB6");
string json = "{ \"$binary\" : \"DB7ytQ0q1kKtA9gnAI2Ktg==\", \"$type\" : \"03\" }";
using (bsonReader = BsonReader.Create(json)) {
Assert.AreEqual(BsonType.Binary, bsonReader.ReadBsonType());
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
Assert.IsTrue(bytes.SequenceEqual(guid.ToByteArray()));
Assert.AreEqual(BsonBinarySubType.Uuid, subType);
Assert.AreEqual(BsonReadState.Done, bsonReader.ReadState);
}
Assert.AreEqual(json, BsonSerializer.Deserialize<Guid>(new StringReader(json)).ToJson());
}
示例11: 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);
}
示例12: TestGuid
public void TestGuid()
{
var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6");
var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")";
using (_bsonReader = new JsonReader(json))
{
Assert.AreEqual(BsonType.Binary, _bsonReader.ReadBsonType());
var binaryData = _bsonReader.ReadBinaryData();
Assert.IsTrue(binaryData.Bytes.SequenceEqual(guid.ToByteArray()));
Assert.AreEqual(BsonBinarySubType.UuidLegacy, binaryData.SubType);
Assert.AreEqual(GuidRepresentation.CSharpLegacy, binaryData.GuidRepresentation);
Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
}
var expected = "CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")";
Assert.AreEqual(expected, BsonSerializer.Deserialize<Guid>(json).ToJson());
}
示例13: Deserialize
public override object Deserialize(
BsonReader bsonReader,
Type nominalType
)
{
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (bytes.Length != 16) {
throw new FileFormatException("BinaryData length is not 16");
}
if (subType != BsonBinarySubType.Uuid) {
throw new FileFormatException("BinaryData sub type is not Uuid");
}
return new Guid(bytes);
}
示例14: 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
) {
var bsonType = bsonReader.CurrentBsonType;
switch (bsonType) {
case BsonType.Binary:
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (bytes.Length != 16) {
throw new FileFormatException("BinaryData length is not 16");
}
if (subType != BsonBinarySubType.Uuid) {
throw new FileFormatException("BinaryData sub type is not Uuid");
}
return new Guid(bytes);
case BsonType.String:
return new Guid(bsonReader.ReadString());
default:
var message = string.Format("Cannot deserialize Guid from BsonType: {0}", bsonType);
throw new FileFormatException(message);
}
}
示例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();
var binaryData = bsonReader.ReadBinaryData();
var subType = binaryData.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)
{
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;
if (bsonReader.FindElement(_elementName))
{
var context = BsonDeserializationContext.CreateRoot<BsonValue>(bsonReader);
var discriminator = BsonValueSerializer.Instance.Deserialize(context);
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;
}