public static BsonArray SerializeValues(this ISerializationExpression field, Type itemType, IEnumerable values) { Ensure.IsNotNull(field, nameof(field)); Ensure.IsNotNull(itemType, nameof(itemType)); Ensure.IsNotNull(values, nameof(values)); var itemSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.SerializerRegistry, itemType); var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("values"); bsonWriter.WriteStartArray(); foreach (var value in values) { itemSerializer.Serialize(context, value); } bsonWriter.WriteEndArray(); bsonWriter.WriteEndDocument(); return((BsonArray)tempDocument[0]); } }
public override BsonDocument Render( IBsonSerializer documentSerializer, IBsonSerializerRegistry serializerRegistry) { StringFieldDefinitionHelper.Resolve( _path, documentSerializer, out string?resolvedFieldName, out IBsonSerializer? resolvedFieldSerializer); resolvedFieldSerializer ??= documentSerializer; if (_value is BsonDocument bsonDocument) { return(new BsonDocument(resolvedFieldName, bsonDocument)); } if (_value is BsonValue bsonValue) { return(new BsonDocument(resolvedFieldName, bsonValue)); } if (_value is MongoDbFilterDefinition mongoDbOperation) { if (_path is "") { return(mongoDbOperation.Render(resolvedFieldSerializer, serializerRegistry)); } return(new BsonDocument( resolvedFieldName, mongoDbOperation.Render(resolvedFieldSerializer, serializerRegistry))); } var document = new BsonDocument(); using var bsonWriter = new BsonDocumentWriter(document); var context = BsonSerializationContext.CreateRoot(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName(resolvedFieldName); if (_value is IList values) { bsonWriter.WriteStartArray(); foreach (var value in values) { resolvedFieldSerializer.Serialize(context, value); } bsonWriter.WriteEndArray(); } else { resolvedFieldSerializer.Serialize(context, _value); } bsonWriter.WriteEndDocument(); return(document); }
public static BsonArray SerializeValues(IBsonSerializer itemSerializer, IEnumerable values) { var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { writer.WriteStartDocument(); writer.WriteName("_v"); writer.WriteStartArray(); var context = BsonSerializationContext.CreateRoot(writer); foreach (var value in values) { itemSerializer.Serialize(context, value); } writer.WriteEndArray(); writer.WriteEndDocument(); } return(document["_v"].AsBsonArray); }
public static BsonArray SerializeValues(this ISerializationExpression field, IEnumerable values) { var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("values"); bsonWriter.WriteStartArray(); foreach (var value in values) { field.Serializer.Serialize(context, ConvertIfNecessary(field.Serializer.ValueType, value)); } bsonWriter.WriteEndArray(); bsonWriter.WriteEndDocument(); return((BsonArray)tempDocument[0]); } }
/// <summary> /// Serializes the values. /// </summary> /// <param name="values">The values.</param> /// <returns>The serialized values.</returns> public BsonArray SerializeValues(IEnumerable values) { var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) { var context = BsonSerializationContext.CreateRoot <BsonDocument>(bsonWriter); bsonWriter.WriteStartDocument(); bsonWriter.WriteName("values"); bsonWriter.WriteStartArray(); foreach (var value in values) { context.SerializeWithChildContext(_serializer, value); } bsonWriter.WriteEndArray(); bsonWriter.WriteEndDocument(); return(tempDocument[0].AsBsonArray); } }