#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); } }
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; }
// 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; }
#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); } }
// 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); } }
// 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); } }
#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); } }
// 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 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()); }
// 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 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()); }
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); }
/// <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> /// 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; }
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 void TestGuid() { var guid = new Guid("B5F21E0C2A0D42d6AD03D827008D8AB6"); var json = "BinData(3, \"DB7ytQ0q1kKtA9gnAI2Ktg==\")"; // test plain BinData without "new" 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.UuidLegacy, subType); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } var expected = "new " + json; // ToJson output will have "new" prepended Assert.AreEqual(expected, BsonSerializer.Deserialize<Guid>(new StringReader(json)).ToJson()); }
// BsonReader is sitting at the value whose actual type needs to be found public Type GetActualType( BsonReader bsonReader, Type nominalType ) { 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; }
/// <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); } }
object IBsonSerializable.Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { if (bsonReader.CurrentBsonType == Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartDocument(); string message; BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "$ref": collectionName = bsonReader.ReadString(); break; case "$id": switch (bsonType) { case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); if (bytes.Length == 16 && subType == BsonBinarySubType.Uuid) { id = new Guid(bytes); } else { throw new FileFormatException("Binary data is not valid for Guid"); } break; case BsonType.Int32: id = bsonReader.ReadInt32(); break; case BsonType.Int64: id = bsonReader.ReadInt64(); break; case BsonType.ObjectId: int timestamp; int machine; short pid; int increment; bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); id = new ObjectId(timestamp, machine, pid, increment); break; case BsonType.String: id = bsonReader.ReadString(); break; default: message = string.Format("Unsupported BsonType for $id element of a DBRef: {0}", bsonType); throw new MongoException(message); } break; case "$db": databaseName = bsonReader.ReadString(); break; default: message = string.Format("Invalid element for DBRef: {0}", name); throw new FileFormatException(message); } } bsonReader.ReadEndDocument(); return this; } }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); return new BsonBinaryData(bytes, subType); } }
/// <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 guidSerializationOptions = options as GuidSerializationOptions; if (guidSerializationOptions == null) { guidSerializationOptions = GuidSerializationOptions.Defaults; } 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"); } if (guidSerializationOptions.ByteOrder == GuidByteOrder.BigEndian && BitConverter.IsLittleEndian) { // we don't need to Clone bytes this time because we know we own this byte array Array.Reverse(bytes, 0, 4); Array.Reverse(bytes, 4, 2); Array.Reverse(bytes, 6, 2); } 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 override object Deserialize( BsonReader bsonReader, Type nominalType ) { BsonType bsonType = bsonReader.CurrentBsonType; if (bsonType == 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); } else if (bsonType == BsonType.String) { return new Guid(bsonReader.ReadString()); } else { var message = string.Format("Cannot deserialize Guid 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()); byte[] bytes; BsonBinarySubType subType; GuidRepresentation guidRepresentation; bsonReader.ReadBinaryData(out bytes, out subType, out guidRepresentation); Assert.IsTrue(bytes.SequenceEqual(guid.ToByteArray())); Assert.AreEqual(BsonBinarySubType.UuidLegacy, subType); Assert.AreEqual(GuidRepresentation.CSharpLegacy, 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 override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); return new BsonBinaryData(bytes, subType); 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.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.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; } if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) { return primitiveType; } } if (bsonType == BsonType.Document) { // ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer) BsonDefaultSerializer.EnsureKnownTypesAreRegistered(nominalType); // we can skip looking for a discriminator if nominalType has no discriminated sub types if (BsonDefaultSerializer.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 = BsonDefaultSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return actualType; } } return nominalType; }
/// <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.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); if (subType == BsonBinarySubType.UuidStandard) { return new BsonBinaryData(bytes, subType, GuidRepresentation.Standard); } else if (subType == BsonBinarySubType.UuidLegacy) { return new BsonBinaryData(bytes, subType, bsonReader.Settings.GuidRepresentation); } else { return new BsonBinaryData(bytes, subType); } default: var message = string.Format("Cannot deserialize BsonBinaryData from BsonType {0}.", bsonType); throw new FileFormatException(message); } }