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 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)); }
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 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); }
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 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 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); } }
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 static Expression SerializeDeserializeExpressionAsText(Expression expression, ISerializer serializer) { var expressionSerializer = new ExpressionSerializer(serializer); var serialized = expressionSerializer.SerializeText(expression); return expressionSerializer.DeserializeText(serialized); }
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 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); }