/// <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 static object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (nominalType == typeof(BsonDocument)) { var bsonDocument = new BsonDocument(); return(bsonDocument.Deserialize(bsonReader, nominalType, options)); } // if nominalType is an interface find out the actualType and use it instead if (nominalType.IsInterface) { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); var actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); if (actualType == nominalType) { var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is the interface {0}.", nominalType); throw new FileFormatException(message); } var serializer = LookupSerializer(actualType); return(serializer.Deserialize(bsonReader, actualType, options)); } else { var serializer = LookupSerializer(nominalType); return(serializer.Deserialize(bsonReader, nominalType, options)); } }
public static void JsonInsert(string collectionName, string Json) { try { MongoCollection<BsonDocument> docs = db.GetCollection<BsonDocument>(collectionName); BsonDocument doc = new BsonDocument(); object obj = doc.Deserialize(BsonReader.Create(Json), typeof(BsonDocument), new MongoDB.Bson.Serialization.Options.DocumentSerializationOptions(false)); docs.Insert((BsonDocument)obj); } catch { //todo log throw; } }
public static void JsonInsert(string collectionName, string Json) { try { MongoCollection <BsonDocument> docs = db.GetCollection <BsonDocument>(collectionName); BsonDocument doc = new BsonDocument(); object obj = doc.Deserialize(BsonReader.Create(Json), typeof(BsonDocument), new MongoDB.Bson.Serialization.Options.DocumentSerializationOptions(false)); docs.Insert((BsonDocument)obj); } catch { //todo log throw; } }