Beispiel #1
0
        /// <summary>
        /// Tries to get the serialization info for the individual items of the array.
        /// </summary>
        /// <param name="serializationInfo">The serialization information.</param>
        /// <returns>
        ///   <c>true</c> if the serialization info exists; otherwise <c>false</c>.
        /// </returns>
        public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
        {
            var itemSerializer = _lazyItemSerializer.Value;

            serializationInfo = new BsonSerializationInfo(null, itemSerializer, itemSerializer.ValueType);
            return(true);
        }
        public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
        {
            var propertyType = typeof(Sample).GetProperty(memberName).PropertyType;

            serializationInfo = new BsonSerializationInfo(memberName.ToLower(), new ValueSerialize(), typeof(object));
            return(true);
        }
Beispiel #3
0
        public override Expression Bind(ProjectionExpression projection, ProjectionBindingContext context, MethodCallExpression node, IEnumerable <Expression> arguments)
        {
            // OfType is two operations in one. First we create WhereExpression
            // for the test.  Second, we want to issue a conversion projection
            // using the selector parameter of a ProjectionExpression such that
            // the conversion will be propogated to all future expressions.

            var newType = node.Method.GetGenericArguments()[0];

            var parameter = Expression.Parameter(projection.Projector.Type);
            var predicate = Expression.Lambda(
                Expression.TypeIs(parameter, newType),
                parameter);

            var source = BindPredicate(projection, context, projection.Source, predicate);

            var serializer = context.SerializerRegistry.GetSerializer(newType);
            var info       = new BsonSerializationInfo(null, serializer, newType);

            var projector = new SerializationExpression(
                Expression.Convert(projection.Projector, newType),
                info);

            return(new ProjectionExpression(
                       source,
                       projector));
        }
Beispiel #4
0
        public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
        {
            switch (memberName)
            {
            case "type":
                serializationInfo = new BsonSerializationInfo("type", new StringSerializer(), typeof(string));
                return(true);

            case "valueType":
                serializationInfo = new BsonSerializationInfo("valueType", new StringSerializer(), typeof(string));
                return(true);

            case "issuer":
                serializationInfo = new BsonSerializationInfo("issuer", new StringSerializer(), typeof(string));
                return(true);

            case "originalIssuer":
                serializationInfo = new BsonSerializationInfo("originalIssuer", new StringSerializer(), typeof(string));
                return(true);

            default:
                serializationInfo = null;
                return(false);
            }
        }
        /// <summary>
        /// Gets the item serialization info.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="serializationInfo">The serialization info.</param>
        /// <returns>The item BsonSerializationInfo for the expression.</returns>
        public BsonSerializationInfo GetItemSerializationInfo(string methodName, BsonSerializationInfo serializationInfo)
        {
            var arraySerializer = serializationInfo.Serializer as IBsonArraySerializer;

            if (arraySerializer != null)
            {
                var itemSerializationInfo = arraySerializer.GetItemSerializationInfo();
                if (itemSerializationInfo != null)
                {
                    var arrayOptions = serializationInfo.SerializationOptions as ArraySerializationOptions;
                    if (arrayOptions != null)
                    {
                        var itemSerializationOptions = arrayOptions.ItemSerializationOptions;
                        return(new BsonSerializationInfo(
                                   itemSerializationInfo.ElementName,
                                   itemSerializationInfo.Serializer,
                                   itemSerializationInfo.NominalType,
                                   itemSerializationOptions));
                    }

                    return(itemSerializationInfo);
                }
            }

            string message = string.Format("{0} requires that the serializer specified for {1} support items by implementing {2} and returning a non-null result. {3} is the current serializer.",
                                           methodName,
                                           serializationInfo.ElementName,
                                           typeof(IBsonArraySerializer),
                                           serializationInfo.Serializer.GetType());

            throw new NotSupportedException(message);
        }
Beispiel #6
0
        public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
        {
            switch (memberName)
            {
            case "SubjectId":
                serializationInfo = new BsonSerializationInfo("_id", new ObjectIdSerializer(), typeof(ObjectId));
                return(true);

            case "StudyId":
                serializationInfo = new BsonSerializationInfo("STUDYID", new StringSerializer(), typeof(string));
                return(true);

            case "SampleId":
                serializationInfo = new BsonSerializationInfo("BSREFID", new StringSerializer(), typeof(string));
                return(true);

            case "AssayId":
                serializationInfo = new BsonSerializationInfo("BSGRPID", new Int32Serializer(), typeof(int));
                return(true);

            case "DomainCode":
                serializationInfo = new BsonSerializationInfo("DOMAIN", new StringSerializer(), typeof(string));
                return(true);

            default:
                serializationInfo = null;
                return(false);
            }
        }
        public void SetValue(object targetResource, string propertyName, object propertyValue)
        {
            var resourceType = GetResourceType(targetResource);
            var annotation   = (TypedResourceTypeAnnotation)resourceType.CustomState;
            var memberMap    = annotation.ClassMap.GetMemberMap(propertyName);
            var serializer   = memberMap.GetSerializer(memberMap.MemberType) as IBsonArraySerializer;

            if (serializer != null)
            {
                var itemSerializationInfo = serializer.GetItemSerializationInfo();
                var array = itemSerializationInfo.SerializeValues((IEnumerable)propertyValue);
                var memberMapSerializationInfo = new BsonSerializationInfo(memberMap.ElementName,
                                                                           serializer,
                                                                           memberMap.MemberType,
                                                                           memberMap.SerializationOptions);

                propertyValue = memberMapSerializationInfo.DeserializeValue(array);
            }
            memberMap.Setter(targetResource, propertyValue);
            if (_rememberedInstances.Contains(targetResource))
            {
                return;
            }

            var collection = GetCollection(resourceType);

            _rememberedInstances.Add(targetResource);
            _actions.Add(() => collection.Save(resourceType.InstanceType, targetResource));
        }
        protected override Expression VisitUnary(UnaryExpression node)
        {
            var newNode         = base.VisitUnary(node);
            var unaryExpression = newNode as UnaryExpression;

            if (node != newNode &&
                unaryExpression != null &&
                (newNode.NodeType == ExpressionType.Convert || newNode.NodeType == ExpressionType.ConvertChecked))
            {
                var serializationExpression = unaryExpression.Operand as ISerializationExpression;
                if (serializationExpression != null)
                {
                    BsonSerializationInfo serializationInfo;
                    var operandType = unaryExpression.Operand.Type;
                    if (!unaryExpression.Operand.Type.IsEnum &&
                        !operandType.IsNullableEnum() &&
                        !unaryExpression.Type.IsAssignableFrom(unaryExpression.Operand.Type))
                    {
                        // only lookup a new serializer if the cast is "unnecessary"
                        var serializer = _serializerRegistry.GetSerializer(node.Type);
                        serializationInfo = new BsonSerializationInfo(serializationExpression.SerializationInfo.ElementName, serializer, node.Type);
                    }
                    else
                    {
                        serializationInfo = serializationExpression.SerializationInfo;
                    }
                    return(new SerializationExpression(unaryExpression, serializationInfo));
                }
            }

            return(newNode);
        }
Beispiel #9
0
        // private methods
        private Expression BindElementAt(MethodCallExpression node)
        {
            if (!IsLinqMethod(node))
            {
                return(base.VisitMethodCall(node));
            }

            var newNode = base.VisitMethodCall(node);
            var methodCallExpression = newNode as MethodCallExpression;

            if (node != newNode && methodCallExpression != null &&
                (methodCallExpression.Method.DeclaringType == typeof(Enumerable) || methodCallExpression.Method.DeclaringType == typeof(Queryable)))
            {
                var serializationExpression = methodCallExpression.Arguments[0] as IBsonSerializationInfoExpression;
                if (serializationExpression != null)
                {
                    var arraySerializer = serializationExpression.SerializationInfo.Serializer as IBsonArraySerializer;
                    if (arraySerializer != null)
                    {
                        var index = (int)((ConstantExpression)methodCallExpression.Arguments[1]).Value;
                        var itemSerializationInfo = arraySerializer.GetItemSerializationInfo();
                        itemSerializationInfo = new BsonSerializationInfo(
                            index.ToString(),
                            itemSerializationInfo.Serializer,
                            itemSerializationInfo.NominalType);

                        var serializationInfo = serializationExpression.SerializationInfo.Merge(itemSerializationInfo);
                        return(new FieldExpression(methodCallExpression, serializationInfo, serializationExpression.IsProjected));
                    }
                }
            }

            return(newNode);
        }
        public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
        {
            var serializer = BsonSerializer.LookupSerializer(ForeignPropertyMap.PropertyType);

            serializationInfo = new BsonSerializationInfo(null, serializer, ForeignPropertyMap.PropertyType);
            return(true);
        }
Beispiel #11
0
        // constructors
        public GridFSForwardOnlyDownloadStream(
            GridFSBucket <TFileId> bucket,
            IReadBinding binding,
            GridFSFileInfo <TFileId> fileInfo,
            bool checkMD5)
            : base(bucket, binding, fileInfo)
        {
            _checkMD5 = checkMD5;
            if (_checkMD5)
            {
                _md5 = MD5.Create();
            }

            _lastChunkNumber = (int)((fileInfo.Length - 1) / fileInfo.ChunkSizeBytes);
            _lastChunkSize   = (int)(fileInfo.Length % fileInfo.ChunkSizeBytes);

            if (_lastChunkSize == 0)
            {
                _lastChunkSize = fileInfo.ChunkSizeBytes;
            }

            var idSerializer        = bucket.Options.SerializerRegistry.GetSerializer <TFileId>();
            var idSerializationInfo = new BsonSerializationInfo("_id", idSerializer, typeof(TFileId));

            _idAsBsonValue = idSerializationInfo.SerializeValue(fileInfo.Id);
        }
        private SerializationExpression BuildProjector(ProjectionExpression projection, ProjectionBindingContext context, GroupIdExpression id, Expression selector)
        {
            BsonSerializationInfo projectorSerializationInfo;
            var projectorSerializationExpression = projection.Projector as ISerializationExpression;

            if (projectorSerializationExpression != null)
            {
                projectorSerializationInfo = projectorSerializationExpression.SerializationInfo;
            }
            else
            {
                var projectorSerializer = context.SerializerRegistry.GetSerializer(projection.Projector.Type);
                projectorSerializationInfo = new BsonSerializationInfo(
                    null,
                    projectorSerializer,
                    projectorSerializer.ValueType);
            }

            var serializerType = typeof(GroupingDeserializer <,>).MakeGenericType(id.Type, projection.Projector.Type);
            var serializer     = (IBsonSerializer)Activator.CreateInstance(serializerType, id.SerializationInfo, projectorSerializationInfo);
            var info           = new BsonSerializationInfo(null, serializer, serializer.ValueType);
            var projector      = new SerializationExpression(selector, info);

            return(projector);
        }
        // public methods
        /// <summary>
        /// Tries to get the serialization info for a member.
        /// </summary>
        /// <param name="memberName">Name of the member.</param>
        /// <param name="serializationInfo">The serialization information.</param>
        /// <returns>
        ///   <c>true</c> if the serialization info exists; otherwise <c>false</c>.
        /// </returns>
        public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
        {
            string          elementName;
            IBsonSerializer serializer;

            switch (memberName)
            {
            case "DatabaseName":
                elementName = "$db";
                serializer  = new StringSerializer();
                break;

            case "CollectionName":
                elementName = "$ref";
                serializer  = new StringSerializer();
                break;

            case "Id":
                elementName = "$id";
                serializer  = BsonValueSerializer.Instance;
                break;

            default:
                serializationInfo = null;
                return(false);
            }

            serializationInfo = new BsonSerializationInfo(elementName, serializer, serializer.ValueType);
            return(true);
        }
            public override bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
            {
                // Dynamic members are allowed in a TestDocument if
                // they start with Dynamic- or are an ObjectId.
                // Otherwise, we fall back to the base-class' definition.

                ObjectId objectId;

                if (memberName.StartsWith("Dynamic-"))
                {
                    serializationInfo = new BsonSerializationInfo(
                        memberName,
                        BsonValueSerializer.Instance,
                        typeof(BsonValue));
                    return(true);
                }
                else if (ObjectId.TryParse(memberName, out objectId))
                {
                    serializationInfo = new BsonSerializationInfo(
                        memberName,
                        BsonValueSerializer.Instance,
                        typeof(BsonValue));
                    return(true);
                }

                return(base.TryGetMemberSerializationInfo(memberName, out serializationInfo));
            }
 /// <summary>
 /// Registers a serializer with the given expression.
 /// </summary>
 /// <param name="node">The expression.</param>
 /// <param name="serializer">The serializer.</param>
 public void RegisterExpressionSerializer(Expression node, IBsonSerializer serializer)
 {
     _serializationInfoCache[node] = new BsonSerializationInfo(
         null,
         serializer,
         node.Type);
 }
Beispiel #16
0
        protected override Expression VisitUnary(UnaryExpression node)
        {
            var newNode         = base.VisitUnary(node);
            var unaryExpression = newNode as UnaryExpression;

            if (node != newNode &&
                unaryExpression != null &&
                !unaryExpression.Operand.Type.IsEnum && // enums are weird, so we skip them
                (newNode.NodeType == ExpressionType.Convert || newNode.NodeType == ExpressionType.ConvertChecked))
            {
                if (unaryExpression.Operand.Type.IsGenericType && unaryExpression.Operand.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    var underlyingType = Nullable.GetUnderlyingType(node.Operand.Type);
                    if (underlyingType.IsEnum)
                    {
                        // we skip enums because they are weird
                        return(newNode);
                    }
                }

                var serializationExpression = unaryExpression.Operand as ISerializationExpression;
                if (serializationExpression != null)
                {
                    var serializer        = _serializerRegistry.GetSerializer(node.Type);
                    var serializationInfo = new BsonSerializationInfo(serializationExpression.SerializationInfo.ElementName, serializer, node.Type);
                    return(new SerializationExpression(unaryExpression, serializationInfo));
                }
            }

            return(newNode);
        }
        // constructors
        public GridFSForwardOnlyUploadStream(
            GridFSBucket <TFileId> bucket,
            IWriteBinding binding,
            TFileId id,
            string filename,
            BsonDocument metadata,
            IEnumerable <string> aliases,
            string contentType,
            int chunkSizeBytes,
            int batchSize)
        {
            _bucket         = bucket;
            _binding        = binding;
            _id             = id;
            _filename       = filename;
            _metadata       = metadata;                                  // can be null
            _aliases        = aliases == null ? null : aliases.ToList(); // can be null
            _contentType    = contentType;                               // can be null
            _chunkSizeBytes = chunkSizeBytes;
            _batchSize      = batchSize;

            _batch = new List <byte[]>();
            _md5   = IncrementalMD5.Create();

            var idSerializer        = bucket.Options.SerializerRegistry.GetSerializer <TFileId>();
            var idSerializationInfo = new BsonSerializationInfo("_id", idSerializer, typeof(TFileId));

            _idAsBsonValue = idSerializationInfo.SerializeValue(id);
        }
Beispiel #18
0
        // protected methods
        protected override Expression VisitBinary(BinaryExpression node)
        {
            var newNode = base.VisitBinary(node);
            var binary  = newNode as BinaryExpression;

            if (binary != null && binary.NodeType == ExpressionType.ArrayIndex)
            {
                var serializationExpression = binary.Left as IBsonSerializationInfoExpression;
                if (serializationExpression != null)
                {
                    var arraySerializer = serializationExpression.SerializationInfo.Serializer as IBsonArraySerializer;
                    var indexExpression = binary.Right as ConstantExpression;
                    if (arraySerializer != null && indexExpression != null && indexExpression.Type == typeof(int))
                    {
                        var index = (int)indexExpression.Value;
                        var itemSerializationInfo = arraySerializer.GetItemSerializationInfo();
                        itemSerializationInfo = new BsonSerializationInfo(
                            index.ToString(),
                            itemSerializationInfo.Serializer,
                            itemSerializationInfo.NominalType);

                        var serializationInfo = serializationExpression.SerializationInfo.Merge(itemSerializationInfo);
                        newNode = new FieldExpression(binary, serializationInfo, serializationExpression.IsProjected);
                    }
                }
            }

            return(newNode);
        }
        private Expression BuildProjector(Expression selector, ProjectionBindingContext context)
        {
            var selectorNode = selector;

            if (!(selectorNode is ISerializationExpression))
            {
                var serializer = SerializerBuilder.Build(selector, context.SerializerRegistry);
                BsonSerializationInfo info;
                switch (selector.NodeType)
                {
                case ExpressionType.MemberInit:
                case ExpressionType.New:
                    info = new BsonSerializationInfo(null, serializer, serializer.ValueType);
                    break;

                default:
                    // this occurs when a computed field is used. This is a magic string
                    // that shouldn't ever be reference anywhere else...
                    info = new BsonSerializationInfo("__fld0", serializer, serializer.ValueType);
                    break;
                }

                selectorNode = new SerializationExpression(selector, info);
            }

            return(selectorNode);
        }
        /// <inheritdoc/>
        public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
        {
            var valueType = typeof(TValue);

            var contract      = _wrappedSerializer.ContractResolver.ResolveContract(valueType);
            var arrayContract = contract as Newtonsoft.Json.Serialization.JsonArrayContract;

            if (arrayContract == null)
            {
                serializationInfo = null;
                return(false);
            }
            if (arrayContract.Converter != null)
            {
                throw new BsonSerializationException($"The Json.NET contract for type \"{valueType.Name}\" has a Converter and JsonConverters are opaque.");
            }
            if (arrayContract.IsReference ?? false)
            {
                throw new BsonSerializationException($"The Json.NET contract for type \"{valueType.Name}\" is serialized as a reference.");
            }
            if (arrayContract.ItemConverter != null)
            {
                throw new BsonSerializationException($"The Json.NET contract for type \"{valueType.Name}\" has an ItemConverter and JsonConverters are opaque.");
            }

            var itemType           = arrayContract.CollectionItemType;
            var itemSerializerType = typeof(JsonSerializerAdapter <>).MakeGenericType(itemType);
            var itemSerializer     = (IBsonSerializer)Activator.CreateInstance(itemSerializerType, _wrappedSerializer);

            serializationInfo = new BsonSerializationInfo(null, itemSerializer, nominalType: itemType);
            return(true);
        }
Beispiel #21
0
        private Expression BindSelector(ProjectionExpression projection, ProjectionBindingContext context, Expression id, Expression node)
        {
            var lambda = ExtensionExpressionVisitor.GetLambda(node);
            var binder = new AccumulatorBinder(context.GroupMap, context.SerializerRegistry);

            var serializer         = SerializerBuilder.Build(projection.Projector, context.SerializerRegistry);
            var sequenceSerializer = (IBsonSerializer)Activator.CreateInstance(
                typeof(ArraySerializer <>).MakeGenericType(projection.Projector.Type),
                new object[] { serializer });
            var sequenceInfo = new BsonSerializationInfo(
                null,
                sequenceSerializer,
                sequenceSerializer.ValueType);
            var sequenceExpression = new SerializationExpression(
                lambda.Parameters[1],
                sequenceInfo);

            binder.RegisterParameterReplacement(lambda.Parameters[0], id);
            binder.RegisterParameterReplacement(lambda.Parameters[1], sequenceExpression);
            var correlationId = Guid.NewGuid();

            context.GroupMap.Add(sequenceExpression, correlationId);
            var bound = binder.Bind(lambda.Body);

            return(CorrelatedAccumulatorRemover.Remove(bound, correlationId));
        }
Beispiel #22
0
 /// <summary>
 /// Tries to get the serialization info for a member.
 /// </summary>
 /// <param name="memberName">Name of the member.</param>
 /// <param name="serializationInfo">The serialization information.</param>
 /// <returns>
 ///   <c>true</c> if the serialization info exists; otherwise <c>false</c>.
 /// </returns>
 public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
 {
     serializationInfo = new BsonSerializationInfo(
         memberName,
         BsonValueSerializer.Instance,
         typeof(BsonValue));
     return(true);
 }
Beispiel #23
0
 /// <summary>
 /// Tries to get the serialization info for the individual items of the array.
 /// </summary>
 /// <param name="serializationInfo">The serialization information.</param>
 /// <returns>
 ///   <c>true</c> if the serialization info exists; otherwise <c>false</c>.
 /// </returns>
 public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
 {
     serializationInfo = new BsonSerializationInfo(
         null,
         BsonValueSerializer.Instance,
         typeof(BsonValue));
     return(true);
 }
 /// <summary>
 /// Registers a serializer with the given expression.
 /// </summary>
 /// <param name="node">The expression.</param>
 /// <param name="serializer">The serializer.</param>
 public void RegisterExpressionSerializer(Expression node, IBsonSerializer serializer)
 {
     _serializationInfoCache[node] = new BsonSerializationInfo(
         null,
         serializer,
         node.Type,
         serializer.GetDefaultSerializationOptions());
 }
Beispiel #25
0
        private static Expression BindSerializationInfo(SerializationInfoBinder binder, LambdaExpression node, IBsonSerializer parameterSerializer)
        {
            var parameterSerializationInfo = new BsonSerializationInfo(null, parameterSerializer, parameterSerializer.ValueType);
            var parameterExpression        = new DocumentExpression(node.Parameters[0], parameterSerializationInfo, false);

            binder.RegisterParameterReplacement(node.Parameters[0], parameterExpression);
            return(binder.Bind(node.Body));
        }
Beispiel #26
0
        /// <summary>
        /// Tries to get the serialization info for the individual items of the array.
        /// </summary>
        /// <param name="serializationInfo">The serialization information.</param>
        /// <returns>
        /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
        /// </returns>
        public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
        {
            const string ElementName = null;
            var          serializer  = BsonSerializer.LookupSerializer(typeof(ClientAddressDto));
            const Type   NominalType = null;

            serializationInfo = new BsonSerializationInfo(ElementName, serializer, NominalType);
            return(true);
        }
Beispiel #27
0
        public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
        {
            string elementName = null;
            var    serializer  = BsonSerializer.LookupSerializer(typeof(string));
            var    nominalType = typeof(string);

            serializationInfo = new BsonSerializationInfo(elementName, serializer, nominalType);
            return(true);
        }
        private static Expression BindSerializationInfo(SerializationInfoBinder binder, LambdaExpression node, IBsonSerializer parameterSerializer)
        {
            var evaluatedBody = PartialEvaluator.Evaluate(node.Body);
            var parameterSerializationInfo = new BsonSerializationInfo(null, parameterSerializer, parameterSerializer.ValueType);
            var parameterExpression        = new SerializationExpression(node.Parameters[0], parameterSerializationInfo);

            binder.RegisterParameterReplacement(node.Parameters[0], parameterExpression);
            return(binder.Bind(evaluatedBody));
        }
 public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
 {
     if (memberName == "ObjectID" || memberName == "_id")
     {
         serializationInfo = new BsonSerializationInfo("_id", this, typeof(string));
         return(true);
     }
     serializationInfo = null;
     return(false);
 }
Beispiel #30
0
        // constructors
        public GridFSSeekableDownloadStream(
            GridFSBucket <TFileId> bucket,
            IReadBinding binding,
            GridFSFileInfo <TFileId> fileInfo)
            : base(bucket, binding, fileInfo)
        {
            var idSerializer        = bucket.Options.SerializerRegistry.GetSerializer <TFileId>();
            var idSerializationInfo = new BsonSerializationInfo("_id", idSerializer, typeof(TFileId));

            _idAsBsonValue = idSerializationInfo.SerializeValue(fileInfo.Id);
        }