/// <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.CurrentBsonType; 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)); }
/// <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); } }
// 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); } }
// 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); }
// 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); } }
public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { BsonReader bsonReader = (BsonReader)context.Reader; CalendarWrapper item = new CalendarWrapper(); bsonReader.ReadStartDocument(); item.Name = bsonReader.ReadString(ID); var binaryData = bsonReader.ReadBinaryData(CONTENT_STREAM); item.Calendar = (ICalendar) new BinaryFormatter().Deserialize(new MemoryStream(binaryData.Bytes)); bsonReader.ReadEndDocument(); return(item); }
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(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <Guid>(new StringReader(json)).ToJson()); }
static Vec Deserialize(BsonReader bsonReader) { var bs = bsonReader.ReadBinaryData().Bytes; byte[] decompressed; try { decompressed = Lz4.DecompressBytes(bs); } catch (Exception) { decompressed = bs; } RecordCompression(bs.Length, decompressed.Length); return(new DenseVector(LittleEndian.BytesToDoubles(decompressed))); }
/// <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; string message; switch (bsonType) { case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); 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 (bsonReader.GuidRepresentation == GuidRepresentation.Unspecified) { throw new BsonSerializationException("GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified."); } var expectedSubType = (bsonReader.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; if (subType != expectedSubType) { message = string.Format("Expected binary sub type {0}, not {1}, for GuidRepresentation {2}.", expectedSubType, subType, bsonReader.GuidRepresentation); throw new FileFormatException(message); } return(GuidConverter.FromBytes(bytes, bsonReader.GuidRepresentation)); case BsonType.String: return(new Guid(bsonReader.ReadString())); default: message = string.Format("Cannot deserialize Guid from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// 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 FileFormatException(message); } }
public void TestGuid() { var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6"); var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")"; using (_bsonReader = BsonReader.Create(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>(new StringReader(json)).ToJson()); }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { object value = null; var valueType = actualType.GetConceptValueType(); if (valueType == typeof(Guid)) { var binaryData = bsonReader.ReadBinaryData(); value = binaryData.ToGuid(); } 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); }
// 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); } }
// 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 FileFormatException(message); } }
public void TestHexData() { var expectedBytes = new byte[] { 0x01, 0x23 }; var json = "HexData(0, \"123\")"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Binary, bsonReader.ReadBsonType()); byte[] bytes; BsonBinarySubType subType; GuidRepresentation guidRepresentation; bsonReader.ReadBinaryData(out bytes, out subType, out guidRepresentation); Assert.IsTrue(expectedBytes.SequenceEqual(bytes)); Assert.AreEqual(BsonBinarySubType.Binary, subType); Assert.AreEqual(GuidRepresentation.Unspecified, guidRepresentation); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } var expectedJson = "new BinData(0, \"ASM=\")"; Assert.AreEqual(expectedJson, BsonSerializer.Deserialize <byte[]>(new StringReader(json)).ToJson()); }
// 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); } }
// 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); } }
// 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; }
// 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) { // 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)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); 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); }
/// <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.CurrentBsonType; 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.Uuid && bytes.Length == 16) { 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; } if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) { return(primitiveType); } } if (bsonType == BsonType.Document) { 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 = BsonDefaultSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return(actualType); } return(nominalType); }
// 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); } }
// 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); }
// 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; }
// 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; 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); } }
// 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; 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); } }