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); } }
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); } } }
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)); }
public void SerializeArrayAsJson() { 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); Assert.IsNotNull(value); }
public void SerializeListAsJson() { var list = new List<string> { "one", "two" }; Expression<Func<Test, bool>> expression = test => list.Contains(test.Code); var serializer = new ExpressionSerializer(new JsonSerializer()) { AutoAddKnownTypesAsListTypes = true }; var value = serializer.SerializeText(expression); Assert.IsNotNull(value); }
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."); }
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); }
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 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 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); } }
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); } } }
private static Expression SerializeDeserializeExpressionAsText(Expression expression, ISerializer serializer) { var expressionSerializer = new ExpressionSerializer(serializer); var serialized = expressionSerializer.SerializeText(expression); return expressionSerializer.DeserializeText(serialized); }
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 })); }
private void SerializeDeserializeGuidValueAsText(ISerializer serializer) { var guidValue = Guid.NewGuid(); Expression<Func<Guid>> exp = () => guidValue; var expressionSerializer = new ExpressionSerializer(serializer); var serialized = expressionSerializer.SerializeText(exp); expressionSerializer.DeserializeText(serialized); }
public string SerializeText(Expression expression) { string value = _serializer.SerializeText(expression); return(value); }
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); } }
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); }
public void SerializeFuncExpressionsWithoutParameters() { var serializer = new ExpressionSerializer(new JsonSerializer()); Expression<Func<bool>> exp = () => false; var result = serializer.SerializeText(exp); Assert.IsNotNull(result); }