/***************************************************/ /**** Interface Methods ****/ /***************************************************/ public Type GetActualType(IBsonReader bsonReader, Type nominalType) { Type actualType = nominalType; // the BsonReader is sitting at the value whose actual type needs to be found BsonType bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement(ElementName)) { var context = BsonDeserializationContext.CreateRoot(bsonReader); var discriminator = BsonValueSerializer.Instance.Deserialize(context); if (discriminator.IsBsonArray) { discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator } if (BsonSerializer.IsTypeDiscriminated(nominalType)) { actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } } bsonReader.ReturnToBookmark(bookmark); } return(actualType); }
// 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(IBsonReader bsonReader, Type nominalType) { // the BsonReader is sitting at the value whose actual type needs to be found var bsonType = bsonReader.GetCurrentBsonType(); 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 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); }
/***************************************************/ /**** Interface Methods ****/ /***************************************************/ public Type GetActualType(IBsonReader bsonReader, Type nominalType) { // the BsonReader is sitting at the value whose actual type needs to be found var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement(ElementName)) { var context = BsonDeserializationContext.CreateRoot(bsonReader); var discriminator = BsonValueSerializer.Instance.Deserialize(context); actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } else { actualType = typeof(CustomObject); } bsonReader.ReturnToBookmark(bookmark); return(actualType); } return(nominalType); }
public Type GetActualType(BsonReader bsonReader, Type nominalType) { BsonReaderBookmark bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); Type actualType = nominalType; if (bsonReader.FindElement("Type")) { BsonValue discriminator = BsonSerializer.Deserialize <BsonValue>(bsonReader); actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return(actualType); }
public Type GetActualType(BsonReader bsonReader, Type nominalType) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement(ElementName)) { var discriminator = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return(actualType); }
/***************************************************/ /**** Interface Methods ****/ /***************************************************/ public Type GetActualType(IBsonReader bsonReader, Type nominalType) { // the BsonReader is sitting at the value whose actual type needs to be found var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement(ElementName)) { var context = BsonDeserializationContext.CreateRoot(bsonReader); var discriminator = BsonValueSerializer.Instance.Deserialize(context); try { actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } catch { BsonDocument doc = new BsonDocument { { "_t", "System.Type" }, { "Name", discriminator.ToString() } }; try { actualType = Convert.FromBson(doc) as Type; } catch { } } } else { actualType = typeof(CustomObject); } bsonReader.ReturnToBookmark(bookmark); return(actualType); } return(nominalType); }
/***************************************************/ /**** Interface Methods ****/ /***************************************************/ public Type GetActualType(IBsonReader bsonReader, Type nominalType) { Type actualType = nominalType; // the BsonReader is sitting at the value whose actual type needs to be found BsonType bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement(ElementName)) { var context = BsonDeserializationContext.CreateRoot(bsonReader); var discriminator = BsonValueSerializer.Instance.Deserialize(context); if (discriminator.IsBsonArray) { discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator } if (BsonSerializer.IsTypeDiscriminated(nominalType)) { actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } else if (Reflection.Compute.UnqualifiedName(nominalType.FullName) != Reflection.Compute.UnqualifiedName(discriminator.ToString()) && Config.AllowUpgradeFromBson && !Config.TypesWithoutUpgrade.Contains(actualType)) { actualType = typeof(IDeprecated); } } bsonReader.ReturnToBookmark(bookmark); if (Config.AllowUpgradeFromBson && actualType.IsDeprecated() && !Config.TypesWithoutUpgrade.Contains(actualType)) { actualType = typeof(IDeprecated); } } return(actualType); }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var doc = BsonDocument.ReadFrom(bsonReader); var id = Convert.ToInt32(doc["_id"]); //return Inject(serializer.Deserialize(bsonReader, nominalType, actualType, options)); //return Inject(serializer.Deserialize(BsonReader.Create(doc), nominalType, actualType, options)); string key = MongoIdentityMap.GetKey <ContentItem>(id); var item = database.IdentityMap.Get(key); var type = actualType ?? BsonSerializer.LookupActualType(nominalType, doc["_t"].AsBsonArray.Last()); if (item == null) { item = BsonClassMap.LookupClassMap(type).CreateInstance(); database.IdentityMap.Set(key, item); } else { if (((ContentItem)item).State != ContentState.None) { return(item); } } try { database.IdentityMap.Current = item; var deserializedItem = (actualType == null) ? serializer.Deserialize(BsonReader.Create(doc), nominalType, options) : serializer.Deserialize(BsonReader.Create(doc), nominalType, actualType, options); return(Inject(deserializedItem)); } finally { database.IdentityMap.Current = null; } }
public void TestLookupActualType() { var actualType = BsonSerializer.LookupActualType(typeof(A), BsonValue.Create("C")); Assert.Equal(typeof(C), actualType); }
// 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); }
// 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(IBsonReader 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.Decimal128: primitiveType = typeof(Decimal128); 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.GetTypeInfo().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(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); }