コード例 #1
0
        public void Serialize_Hashtable()
        {
            var data = new Hashtable()
            {
                ["x"] = 1
            };

            //! used to fail
            var result = _mapper.Serialize(data).AsDocument;

            Assert.Single(result);
            Assert.Equal(1, result["x"].AsInt32);
        }
コード例 #2
0
ファイル: QueryVisitor.cs プロジェクト: S3NoSql/S3NoSql
        private BsonValue VisitValue(Expression expr, Expression left)
        {
            // check if left side is an enum and convert to string before return
            Func <Type, object, BsonValue> convert = (type, value) =>
            {
                var enumType = (left as UnaryExpression) == null ? null : (left as UnaryExpression).Operand.Type;

                if (enumType != null && enumType.GetTypeInfo().IsEnum)
                {
                    var str = Enum.GetName(enumType, value);
                    return(_mapper.Serialize(typeof(string), str, 0));
                }

                return(_mapper.Serialize(type, value, 0));
            };

            // its a constant; Eg: "fixed string"
            if (expr is ConstantExpression)
            {
                var value = (expr as ConstantExpression);

                return(convert(value.Type, value.Value));
            }
            else if (expr is MemberExpression && _parameters.Count > 0)
            {
                var mExpr  = (MemberExpression)expr;
                var mValue = this.VisitValue(mExpr.Expression, left);
                var value  = mValue.AsDocument[mExpr.Member.Name];

                return(convert(typeof(object), value));
            }
            else if (expr is ParameterExpression)
            {
                BsonValue result;
                if (_parameters.TryGetValue((ParameterExpression)expr, out result))
                {
                    return(result);
                }
            }

            // execute expression
            var objectMember = Expression.Convert(expr, typeof(object));
            var getterLambda = Expression.Lambda <Func <object> >(objectMember);
            var getter       = getterLambda.Compile();

            return(convert(typeof(object), getter()));
        }
コード例 #3
0
        public void Enum_Array_Test()
        {
            var mapper = new BsonMapper();

            mapper.EnumAsInteger = false;

            var array = new CustomerType[] { CustomerType.Potential, CustomerType.Loyal };

            var serialized1   = mapper.Serialize(array);
            var deserialized1 = mapper.Deserialize <CustomerType[]>(serialized1);

            deserialized1.Should().Equal(array);

            mapper.EnumAsInteger = true;

            var serialized2   = mapper.Serialize(array);
            var deserialized2 = mapper.Deserialize <CustomerType[]>(serialized1);

            deserialized2.Should().Equal(array);
        }
コード例 #4
0
ファイル: Market.cs プロジェクト: frabul/SharpTrader
        public void RegisterCustomSerializers(BsonMapper mapper)
        {
            //this implementation is only for testing as the simulator doesn't save it's state
            BsonMapper defaultMapper = new BsonMapper();

            BsonValue OrderToBson(Order order)
            {
                return(defaultMapper.Serialize(typeof(IOrder), order));
            }

            Order BsonToOrder(BsonValue value)
            {
                var order = DeserializedOrders.FirstOrDefault(o => o.Id == value["_id"].AsString);

                if (order == null)
                {
                    DeserializedOrders.Add(order = defaultMapper.Deserialize <Order>(value));
                }
                return(order);
            }

            BsonValue SerializeTrade(Trade trade)
            {
                return(defaultMapper.Serialize(typeof(ITrade), trade));
            }

            Trade DeserializeTrade(BsonValue value)
            {
                var trade = DeserializedTrades.FirstOrDefault(o => o.Id == value["_id"].AsString);

                if (trade == null)
                {
                    DeserializedTrades.Add(trade = defaultMapper.Deserialize <Trade>(value));
                }
                return(trade);
            }

            mapper.RegisterType <Order>(OrderToBson, BsonToOrder);
            mapper.RegisterType <Trade>(SerializeTrade, DeserializeTrade);
        }
コード例 #5
0
ファイル: CustomMapping_Tests.cs プロジェクト: zamis/LiteDB
        public void Custom_Id_In_Interface()
        {
            var mapper = new BsonMapper();

            var obj = new ConcreteClass {
                CustomId = "myid", Name = "myname"
            };
            var doc = mapper.Serialize(obj) as BsonDocument;

            doc["_id"].Should().NotBeNull();
            doc["_id"].Should().Be("myid");
            doc["CustomName"].Should().NotBe(BsonValue.Null);
            doc["CustomName"].Should().Be("myname");
            doc["Name"].Should().Be(BsonValue.Null);
            doc.Keys.ExpectCount(2);
        }
コード例 #6
0
ファイル: LinqEval_Tests.cs プロジェクト: mercan01/LiteDB
        private void Eval <T, K>(T entity, Expression <Func <T, K> > expr, params K[] expect)
        {
            var expression = _mapper.GetExpression(expr);
            var doc        = _mapper.ToDocument <T>(entity);

            var results = expression.Execute(doc).ToArray();
            var index   = 0;

            Assert.AreEqual(expect.Length, results.Length);

            foreach (var result in results)
            {
                var exp = _mapper.Serialize(typeof(K), expect[index++]);

                Assert.AreEqual(exp, result);
            }
        }