Пример #1
0
        public void LetExpressionTests()
        {
            var expressions = new List<Expression>();

            Expression<Func<IEnumerable<int>, IEnumerable<int>>> intExpr = c =>
                from x in c
                let test = 8
                where x == test
                select x;
            expressions.Add(intExpr);

            Expression<Func<IEnumerable<string>, IEnumerable<string>>> strExpr = c =>
                from x in c
                let test = "bar"
                where x == test
                select x;
            expressions.Add(strExpr);            

            foreach (var textSerializer in new ITextSerializer[] { new JsonSerializer(), new XmlSerializer() })
            {
                var serializer = new ExpressionSerializer(textSerializer);
                foreach (var expected in expressions)
                {
                    var serialized = serializer.SerializeText(expected);
                    var actual = serializer.DeserializeText(serialized);

                    ExpressionAssert.AreEqual(expected, actual);
                }
            }
        }
Пример #2
0
        public void SerializeLambdaWithEnumTest()
        {
            foreach (var serializer in new ITextSerializer[] { new JsonSerializer(), new XmlSerializer() })
            {
                serializer.AddKnownType(typeof(Gender));

                var expressionSerializer = new ExpressionSerializer(serializer);                
                var fish = new[]
                {
                    new ItemWithEnum {Gender = Gender.Male},
                    new ItemWithEnum {Gender = Gender.Female},
                    new ItemWithEnum(),
                    new ItemWithEnum {Gender = Gender.Female}
                };
                var some = Gender.Female;
                Expression<Func<ItemWithEnum, bool>> expectedExpression = f => f.Gender == some;
                var expected = fish.Where(expectedExpression.Compile()).Count();

                var serialized = expressionSerializer.SerializeText(expectedExpression); // throws SerializationException
                var actualExpression = (Expression<Func<ItemWithEnum, bool>>)expressionSerializer.DeserializeText(serialized);
                var actual = fish.Where(actualExpression.Compile()).Count();

                Assert.AreEqual(expected, actual);
            }
        }
Пример #3
0
        public static HubRequestFilter FromBase64(String filter)
        {
            var expressionSerializer = new Serialization.ExpressionSerializer(new Serialization.JsonSerializer());
            var base64Request        = Encoding.UTF8.GetString(Convert.FromBase64String(filter));
            var expression           = expressionSerializer.DeserializeText(base64Request);

            return(new HubRequestFilter(expression));
        }
Пример #4
0
        public HubRequestFilter(Expression filterExpression)
        {
            FilterExpression = filterExpression;

            var expressionSerializer = new Serialization.ExpressionSerializer(new Serialization.JsonSerializer());
            var json = expressionSerializer.SerializeText(FilterExpression);

            GroupId = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
        }
Пример #5
0
        public void SerializeArrayAsBinary()
        {
            var list = new[] { "one", "two" };
            Expression<Func<Test, bool>> expression = test => list.Contains(test.Code);

            var serializer = new ExpressionSerializer(new BinarySerializer());
            var value = serializer.SerializeBinary(expression);

            Assert.IsNotNull(value);
        }
Пример #6
0
        public void SerializeAsQueryableWithPredicateTest()
        {
            Expression<Func<Order, bool>> predicate = x => x.Id > 0 && x.Id < 5;

            Expression<Func<Document, bool>> pred = x => x.Orders.AsQueryable().Any(predicate);

            var serializer = new ExpressionSerializer(new BinarySerializer());
            var value = serializer.SerializeBinary(pred);

            Assert.IsNotNull(value);
        }
Пример #7
0
        public void SerializeListAsBinary()
        {
            var list = new List<string> { "one", "two" };
            Expression<Func<Test, bool>> expression = test => list.Contains(test.Code);

            var serializer = new ExpressionSerializer(new BinarySerializer())
            {
                AutoAddKnownTypesAsListTypes = true
            };
            var value = serializer.SerializeBinary(expression);

            Assert.IsNotNull(value);
        }
        public void CanSerializeBinaryTest()
        {
            foreach (var textSerializer in CreateTextSerializers())
            {
                var serializer = new ExpressionSerializer(textSerializer);
                Assert.IsFalse(serializer.CanSerializeBinary, "'{0}' was not expected to serialize binary.", textSerializer.GetType());
            }

            foreach (var binSerializer in CreateBinarySerializers())
            {
                var serializer = new ExpressionSerializer(binSerializer);
                Assert.IsTrue(serializer.CanSerializeBinary, "'{0}' was expected to serialize binary.", serializer.GetType());
            }
        }
Пример #9
0
        public void SerializeDeserializeArrayAsJson()
        {
            var list = new[] { "one", "two" };
            Expression<Func<Test, bool>> expression = test => list.Contains(test.Code);

            var serializer = new ExpressionSerializer(new JsonSerializer());
            var value = serializer.SerializeText(expression);

            var actualExpression = (Expression<Func<Test, bool>>)serializer.DeserializeText(value);
            var func = actualExpression.Compile();


            Assert.IsTrue(func(new Test { Code = "one" }), "one failed.");
            Assert.IsTrue(func(new Test { Code = "two" }), "two failed.");
            Assert.IsFalse(func(new Test { Code = "three" }), "three failed.");
        }
Пример #10
0
        public ISubscription Read(JsonReader reader, JsonSerializer serializer)
        {
            var expressionSerializer = new Serialization.ExpressionSerializer(new Serialization.JsonSerializer());

            var dto     = serializer.Deserialize <SubscriptionDto>(reader);
            var subType = typeof(Subscription <>).MakeGenericType(dto.EventType);
            var sub     = (ISubscription)Activator.CreateInstance(subType);

            sub.EventType           = dto.EventType;
            sub.CanHandleExpression = dto.CanHandle;

            var exp = expressionSerializer.DeserializeText(sub.CanHandleExpression);

            sub.CanHandleFunc = sub.ToFunc(exp);

            return(sub);
        }
Пример #11
0
        public void SerializeContainsWithNullablesAndWithNullableKey()
        {
            var enterpriseKeys = new List<long?> { 1, 2, 3, 4 };

            var predicatePart =
                (Expression<Func<GroupEntityWithNullable, bool>>)
                    (groupType =>
                        enterpriseKeys.Contains(groupType.GroupEnterpriseKey));

            var serializeTo = new ExpressionSerializer(new XmlSerializer())
            {
                AutoAddKnownTypesAsListTypes = true
            };
            var predicatePartSerializedToString = serializeTo.SerializeText(predicatePart);

            Assert.IsNotNull(predicatePartSerializedToString);
        }
Пример #12
0
        public void DynamicsTests()
        {
            var expressions = new List<Expression>();

            Expression<Func<Item, dynamic>> objectExp = item => new {item.Name, item.ProductId};
            Expression<Func<string, dynamic>> stringExp = str => new { Text = str };

            expressions.Add(objectExp);
            expressions.Add(stringExp);

            foreach (var textSerializer in new ITextSerializer[] { new JsonSerializer(), new XmlSerializer() })
            {
                var serializer = new ExpressionSerializer(textSerializer);
                foreach (var expected in expressions)
                {
                    var serialized = serializer.SerializeText(expected);
                    var actual = serializer.DeserializeText(serialized);

                    ExpressionAssert.AreEqual(expected, actual);
                }
            }
        }
        public void SerializeDeserializeBinaryTest()
        {
            foreach (var binSerializer in CreateBinarySerializers())
            {
                var serializer = new ExpressionSerializer(binSerializer);
                foreach (var expected in SerializerTestData.TestExpressions)
                {
                    var bytes = serializer.SerializeBinary(expected);

                    this.TestContext.WriteLine("{0} serializes to bytes with length {1}", expected, bytes.Length);

                    var actual = serializer.DeserializeBinary(bytes);

                    if (expected == null)
                    {
                        Assert.IsNull(actual, "Input expression was null, but output is {0} for '{1}'", actual, binSerializer.GetType());
                        continue;
                    }
                    Assert.IsNotNull(actual, "Input expression was {0}, but output is null for '{1}'", expected, binSerializer.GetType());
                    ExpressionAssert.AreEqual(expected, actual);
                }
            }
        }
Пример #14
0
        public void SerializeWithDateTimeLocalTest()
        {
            foreach (var textSerializer in new ITextSerializer[] { new JsonSerializer(), new XmlSerializer() })
            {
                var serializer = new ExpressionSerializer(textSerializer);
                var yarrs = new[]
                {
                    new Yarr {Date = new DateTime(3000,1,1)},
                    new Yarr {Date = new DateTime(2000,1,1)},
                    new Yarr(),
                    new Yarr { Date = DateTime.Now.AddYears(1) }
                };
                var date = DateTime.Now;
                Expression<Func<Yarr, bool>> expectedExpression = f => f.Date > date;
                var expected = yarrs.Where(expectedExpression.Compile()).Count();

                var serialized = serializer.SerializeText(expectedExpression);
                var actualExpression = (Expression<Func<Yarr, bool>>)serializer.DeserializeText(serialized);
                var actual = yarrs.Where(actualExpression.Compile()).Count();

                Assert.AreEqual(expected, actual);
            }
        }
Пример #15
0
        public void SerializeLambdaWithNullableTest()
        {
            foreach (var textSerializer in new ITextSerializer[] { new JsonSerializer(), new XmlSerializer() })
            {
                var serializer = new ExpressionSerializer(textSerializer);
                var fish = new[]
                {
                    new Fish {Count = 0},
                    new Fish {Count = 1},
                    new Fish(),
                    new Fish {Count = 1}
                };
                int? count = 1;
                Expression<Func<Fish, bool>> expectedExpression = f => f.Count == count;
                var expected = fish.Where(expectedExpression.Compile()).Count();

                var serialized = serializer.SerializeText(expectedExpression);
                var actualExpression = (Expression<Func<Fish, bool>>)serializer.DeserializeText(serialized);
                var actual = fish.Where(actualExpression.Compile()).Count();

                Assert.AreEqual(expected, actual);
            }
        }
        public void SerializeDeserializeTextTest()
        {
            foreach (var textSerializer in CreateTextSerializers())
            {
                var serializer = new ExpressionSerializer(textSerializer);
                foreach (var expected in SerializerTestData.TestExpressions)
                {
                    var text = serializer.SerializeText(expected);

                    this.TestContext.WriteLine("{0} serializes to text with length {1}: {2}", expected, text.Length, text);

                    var actual = serializer.DeserializeText(text);

                    if (expected == null)
                    {
                        Assert.IsNull(actual, "Input expression was null, but output is {0} for '{1}'", actual, textSerializer.GetType());
                        continue;
                    }
                    Assert.IsNotNull(actual, "Input expression was {0}, but output is null for '{1}'", expected, textSerializer.GetType());
                    ExpressionAssert.AreEqual(expected, actual);
                }
            }
        }
        public void SerializeFuncExpressionsWithoutParameters()
        {
            var serializer = new ExpressionSerializer(new JsonSerializer());

            Expression<Func<bool>> exp = () => false;

            var result = serializer.SerializeText(exp);
            Assert.IsNotNull(result);
        }
Пример #18
0
        private void TestExpression(Expression<Func<Test, bool>> expression, ReadFieldOn readFieldOn)
        {
            var initialValue = 42;
            var actualValue = -1;

            // Initialize fields
            SetFields(initialValue);

            // Serialize expression
            var settings = new FactorySettings
            {
                AllowPrivateFieldAccess = true
            };
            var serializer = new ExpressionSerializer(new JsonSerializer());
            var value = serializer.SerializeText(expression, settings);

            // Modify fields
            SetFields(actualValue);

            // Deserialize expression
            var actualExpression = (Expression<Func<Test, bool>>)serializer.DeserializeText(value, new ExpressionContext { AllowPrivateFieldAccess = true });
            var func = actualExpression.Compile();

            // Set expected value.
            int expectedValue = readFieldOn == ReadFieldOn.Serialization
                ? initialValue
                : actualValue;

            // Assert
            Assert.IsTrue(func(new Test { IntProperty = expectedValue }));
        }
Пример #19
0
 public void Initialize()
 {
     _jsonExpressionSerializer = new ExpressionSerializer(new JsonSerializer());
 }
        private void SerializeDeserializeGuidValueAsBinary(ISerializer serializer)
        {
            var guidValue = Guid.NewGuid();
            Expression<Func<Guid>> exp = () => guidValue;

            var expressionSerializer = new ExpressionSerializer(serializer);
            var serialized = expressionSerializer.SerializeBinary(exp);

            expressionSerializer.DeserializeBinary(serialized);
        }
Пример #21
0
 public BaseExpressionSerializer()
 {
     _serializer = new Serialize.Linq.Serializers.ExpressionSerializer(new JsonSerializer());
 }
        public void SerializeDeserializeBinaryComplexExpressionWithCompileTest()
        {
            foreach (var binSerializer in CreateBinarySerializers())
            {
                var serializer = new ExpressionSerializer(binSerializer);

                var expected = (Expression<Func<Bar, bool>>)(p => p.LastName == "Miller" && p.FirstName.StartsWith("M"));
                expected.Compile();

                var bytes = serializer.SerializeBinary(expected);
                this.TestContext.WriteLine("{0} serializes to bytes with length {1}", expected, bytes.Length);

                var actual = (Expression<Func<Bar, bool>>)serializer.DeserializeBinary(bytes);
                Assert.IsNotNull(actual, "Input expression was {0}, but output is null for '{1}'", expected, binSerializer.GetType());
                ExpressionAssert.AreEqual(expected, actual);

                actual.Compile();
            }
        }
        public void NullableDecimalTest()
        {
            foreach (var textSerializer in CreateTextSerializers())
            {
                var serializer = new ExpressionSerializer(textSerializer);
                var expected = Expression.Constant(0m, typeof(Decimal?));

                var text = serializer.SerializeText(expected);

                this.TestContext.WriteLine("{0} serializes to text with length {1}: {2}", expected, text.Length, text);

                var actual = serializer.DeserializeText(text);
                Assert.IsNotNull(actual, "Input expression was {0}, but output is null for '{1}'", expected, textSerializer.GetType());
                ExpressionAssert.AreEqual(expected, actual);
            }                        
        }
        private static Expression SerializeDeserializeExpressionAsBinary(Expression expression, ISerializer serializer)
        {
            var expressionSerializer = new ExpressionSerializer(serializer);
            var serialized = expressionSerializer.SerializeBinary(expression);

            return expressionSerializer.DeserializeBinary(serialized);
        }
        public void SerializeNewObjWithoutParameters()
        {
            var serializer = new ExpressionSerializer(new JsonSerializer());

            Expression<Func<List<int>, List<int>>> exp = l => new List<int>();

            var result = serializer.SerializeText(exp);
            Assert.IsNotNull(result);
        }