private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap) { var extraElements = extraElementsMemberMap.Getter(obj); if (extraElements != null) { if (extraElementsMemberMap.MemberType == typeof(BsonDocument)) { var bsonDocument = (BsonDocument)extraElements; foreach (var element in bsonDocument) { bsonWriter.WriteName(element.Name); BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null); } } else { var dictionary = (IDictionary <string, object>)extraElements; foreach (var key in dictionary.Keys) { bsonWriter.WriteName(key); var value = dictionary[key]; if (value == null) { bsonWriter.WriteNull(); } else { var bsonValue = BsonTypeMapper.MapToBsonValue(dictionary[key]); BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), bsonValue, null); } } } } }
private void DeserializeExtraElement( BsonReader bsonReader, object obj, string elementName, BsonMemberMap extraElementsMemberMap) { if (extraElementsMemberMap.MemberType == typeof(BsonDocument)) { var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj); if (extraElements == null) { extraElements = new BsonDocument(); extraElementsMemberMap.Setter(obj, extraElements); } var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); extraElements[elementName] = bsonValue; } else { var extraElements = (IDictionary <string, object>)extraElementsMemberMap.Getter(obj); if (extraElements == null) { if (extraElementsMemberMap.MemberType == typeof(IDictionary <string, object>)) { extraElements = new Dictionary <string, object>(); } else { extraElements = (IDictionary <string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType); } extraElementsMemberMap.Setter(obj, extraElements); } var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue); } }
private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap) { var value = memberMap.Getter(obj); if (!memberMap.ShouldSerialize(obj, value)) { return; // don't serialize member } bsonWriter.WriteName(memberMap.ElementName); var nominalType = memberMap.MemberType; if (value == null && nominalType.IsInterface) { bsonWriter.WriteNull(); } else { var actualType = (value == null) ? nominalType : value.GetType(); var serializer = memberMap.GetSerializer(actualType); serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions); } }
private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap) { var extraElements = extraElementsMemberMap.Getter(obj); if (extraElements != null) { if (extraElementsMemberMap.MemberType == typeof(BsonDocument)) { var bsonDocument = (BsonDocument)extraElements; foreach (var element in bsonDocument) { bsonWriter.WriteName(element.Name); BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null); } } else { var dictionary = (IDictionary<string, object>)extraElements; foreach (var key in dictionary.Keys) { bsonWriter.WriteName(key); var value = dictionary[key]; if (value == null) { bsonWriter.WriteNull(); } else { var bsonValue = BsonTypeMapper.MapToBsonValue(dictionary[key]); BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), bsonValue, null); } } } } }
private void DeserializeExtraElement( BsonReader bsonReader, object obj, string elementName, BsonMemberMap extraElementsMemberMap) { if (extraElementsMemberMap.MemberType == typeof(BsonDocument)) { var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj); if (extraElements == null) { extraElements = new BsonDocument(); extraElementsMemberMap.Setter(obj, extraElements); } var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); extraElements[elementName] = bsonValue; } else { var extraElements = (IDictionary<string, object>)extraElementsMemberMap.Getter(obj); if (extraElements == null) { if (extraElementsMemberMap.MemberType == typeof(IDictionary<string, object>)) { extraElements = new Dictionary<string, object>(); } else { extraElements = (IDictionary<string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType); } extraElementsMemberMap.Setter(obj, extraElements); } var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue); } }