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]);
            }
        }
Exemple #2
0
        public static BsonValue SerializeValue(this ISerializationExpression field, object value)
        {
            Ensure.IsNotNull(field, nameof(field));

            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("value");
                field.Serializer.Serialize(context, value);
                bsonWriter.WriteEndDocument();
                return(tempDocument[0]);
            }
        }
Exemple #3
0
        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]);
            }
        }
Exemple #4
0
        public static string PrependFieldName(this ISerializationExpression node, string prefix)
        {
            var field = node as IFieldExpression;

            return(CombineFieldNames(prefix, field == null ? null : field.FieldName));
        }
Exemple #5
0
        public static string AppendFieldName(this ISerializationExpression node, string suffix)
        {
            var field = node as IFieldExpression;

            return(CombineFieldNames(field == null ? null : field.FieldName, suffix));
        }
        private static bool TryFindSerializationExpression(MethodCallExpression node, out ISerializationExpression serializationExpression)
        {
            var current = node.Arguments[0];
            serializationExpression = current as ISerializationExpression;
            if (serializationExpression == null &&
                current.NodeType == ExpressionType.Call &&
                ExpressionHelper.IsLinqMethod((MethodCallExpression)current))
            {
                current = ((MethodCallExpression)current).Arguments[0];
                serializationExpression = current as ISerializationExpression;
            }

            return serializationExpression != null;
        }
Exemple #7
0
        private static bool TryFindSerializationExpression(MethodCallExpression node, out ISerializationExpression serializationExpression)
        {
            var current = node.Arguments[0];

            serializationExpression = current as ISerializationExpression;
            if (serializationExpression == null &&
                current.NodeType == ExpressionType.Call &&
                ExpressionHelper.IsLinqMethod((MethodCallExpression)current))
            {
                current = ((MethodCallExpression)current).Arguments[0];
                serializationExpression = current as ISerializationExpression;
            }

            return(serializationExpression != null);
        }