public void DeserializeComplex_AbstractAndInterfaceClass() { var obj = new ClassWithAbstractAndInterface { A = new ImplementAbstractAndInterface { MyProp = 10 }, B = new ImplementAbstractAndInterface { MyProp = 20 }, }; var expressionStr = obj.AsExpression().DefaultSerializer.Serialize(); var factory = new ComplexEntityFactory(obj.GetType()); factory.AddMapType <Interface, ImplementAbstractAndInterface>(); factory.AddMapType <AbstractClass, ImplementAbstractAndInterface>(); var deserializer = new ComplexEntityExpressionDeserializer(); var deserialized = deserializer.Deserialize <ClassWithAbstractAndInterface>(expressionStr, factory); Assert.Equal(10, deserialized.A.MyProp); Assert.Equal(20, deserialized.B.MyProp); }
public void FactoryTyped_CreateInstance_A_plus_B_plus_Pharentesis() { var root = (new Entity("CircularEntity.0") + new Entity("Name: Root2") + (new Entity("children.1") + (new Entity("[0].2") + new Entity("Name: Name1") + new Entity("Name: Name1 (last)")) + (new Entity("[1].3") + new Entity("Name: Name2")) + (new Entity("[2].4") + new Entity("Name: Name3")))); var build = new ComplexEntityFactory <CircularEntity>(root).Build(); var value = build.Value; Assert.Equal("CircularEntity", root.Name); Assert.Equal("Root2", value.Name); Assert.Equal(3, value.Children.Count()); Assert.Equal("Name1 (last)", value.Children.ElementAt(0).Name); Assert.Equal("Name2", value.Children.ElementAt(1).Name); Assert.Equal("Name3", value.Children.ElementAt(2).Name); Assert.Same(root["children"].Value, value.Children); Assert.Same(root["children"]["[0]"].Value, value.Children.ElementAt(0)); Assert.Same(root["children"]["[1]"].Value, value.Children.ElementAt(1)); Assert.Same(root["children"]["[2]"].Value, value.Children.ElementAt(2)); Assert.Equal("Root2", root["Name"].Value); }
public void DeserializeComplex_AbstractAndInterfaceClass_NoFactory() { var obj = new ClassWithAbstractAndInterface { A = new ImplementAbstractAndInterface { MyProp = 10 }, B = new ImplementAbstractAndInterface { MyProp = 20 }, }; var factory = new ComplexEntityFactory(obj.GetType()); factory.IgnoreErrors = true; var expressionStr = obj.AsExpression().DefaultSerializer.Serialize(); var deserializer = new ComplexEntityExpressionDeserializer(); var deserialized = deserializer.Deserialize <ClassWithAbstractAndInterface>(expressionStr, factory); Assert.Null(deserialized.A); Assert.Null(deserialized.B); Assert.Equal(2, factory.Errors.Count); Assert.Equal($"An instance of type '{typeof(Interface).FullName}' contains value, but not created. Make sure it is an interface or an abstract class, if so, set up a corresponding concrete class in the '{nameof(ComplexEntityFactory)}.{nameof(ComplexEntityFactory.MapTypes)}' configuration.", factory.Errors[0]); Assert.Equal($"An instance of type '{typeof(AbstractClass).FullName}' contains value, but not created. Make sure it is an interface or an abstract class, if so, set up a corresponding concrete class in the '{nameof(ComplexEntityFactory)}.{nameof(ComplexEntityFactory.MapTypes)}' configuration.", factory.Errors[1]); }
/// <summary> /// Deserializes a string expression with custom complex entity factory /// </summary> /// <param name="expression">Expressin as string</param> /// <param name="factory">This factory is util to customize entity creation</param> /// <returns>Return a complety entity</returns> public async Task <object> DeserializeAsync(string expression, ComplexEntityFactory factory) { var roslyn = new RoslynExpressionDeserializer <Entity>(); var runner = roslyn.GetDelegateExpression(expression, factory.GetType()); var root = await runner(factory); factory.Root = root; return(factory.Build().Value); }
public void EntityFactory5() { var root = new Entity(0) + new Entity("[0]", "10") + new Entity("[1]: 11"); var factory = new ComplexEntityFactory <int[]>(root); // Build entity and get typed value var entity = factory.Build().Value; System.Console.WriteLine(entity[0]); System.Console.WriteLine(entity[1]); }
public void EntityFactory4() { var root = new Entity(0) + new Entity("System.Int32._intValue: 1000"); var factory = new ComplexEntityFactory <MyClass>(root); // Build entity factory.Build(); var entity = factory.Value; System.Console.WriteLine(typeof(MyClass).GetField("_intValue", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(entity)); }
public void EntityFactory2() { var root = new Entity(0) + new Entity("Child", 0); var factory = new ComplexEntityFactory <MyClass>(root); // Build entity factory.Build(); var entity = factory.Value; System.Console.WriteLine(entity == entity.Child); }
public void EntityFactory() { var root = new Entity(0) + new Entity("Name: Entity name ;)"); var factory = new ComplexEntityFactory <CircularEntity>(root); // Build entity factory.Build(); var entity = factory.Value; System.Console.WriteLine(entity.Name); }
public void FactoryTyped_CreateSimpleClass() { var root = GetEntity("CircularEntity.0") + GetEntity("Name: A") + GetEntity("Count: 0") + (GetEntity("Children.3") + GetEntity("Capacity: 0") + GetEntity("Count: 0")); var build = new ComplexEntityFactory <CircularEntity>(root).Build(); Assert.Equal("A", build.Value.Name); Assert.Empty(build.Value.Children); }
public void FactoryTyped_CreateArray() { var root = GetEntity("Class1[].55467396") + (GetEntity("[0].56235422") + GetEntity("Prop1: 1")) + (GetEntity("[1].31423778") + GetEntity("Prop1: 2")) + (GetEntity("[2].29279655") + GetEntity("Prop1: 3")) + GetEntity("[3].56235422"); // Repeat 56235422 var value = new ComplexEntityFactory <Class1[]>(root).Build().Value; Assert.Equal(1, value[0].Prop1); Assert.Equal(2, value[1].Prop1); Assert.Equal(3, value[2].Prop1); Assert.Same(value[0], value[3]); }
public void EntityFactory6() { var root = new Entity(0) + (new Entity("A", 1) + new Entity("MyProp", "10")) + (new Entity("B", 2) + new Entity("MyProp", "20")); var factory = new ComplexEntityFactory <ClassWithAbstractAndInterface>(root); factory.AddMapType <Interface, ImplementAbstractAndInterface>(); factory.AddMapType <AbstractClass, ImplementAbstractAndInterface>(); // Build entity and get typed value var entity = factory.Build().Value; System.Console.WriteLine(entity.A.MyProp); System.Console.WriteLine(entity.A.GetType().Name); System.Console.WriteLine(entity.B.MyProp); System.Console.WriteLine(entity.B.GetType().Name); }
/// <summary> /// Deserializes a string expression with custom complex entity factory /// </summary> /// <param name="expression">Expressin as string</param> /// <param name="factory">This factory is util to customize entity creation</param> /// <returns>Return a complety entity</returns> public object Deserialize(string expression, ComplexEntityFactory factory) { return(DeserializeAsync(expression, factory).Result); }
/// <summary> /// Deserializes a string expression with custom complex entity factory /// </summary> /// <typeparam name="T">Type of real entity</typeparam> /// <param name="expression">Expressin as string</param> /// <param name="factory">This factory is util to customize entity creation</param> /// <returns>Return a typed complety entity</returns> public async Task <T> DeserializeAsync <T>(string expression, ComplexEntityFactory factory) { var root = await DeserializeAsync(expression, factory); return((T)root); }
/// <summary> /// Deserializes a string expression with custom complex entity factory /// </summary> /// <typeparam name="T">Type of real entity</typeparam> /// <param name="expression">Expressin as string</param> /// <param name="factory">This factory is util to customize entity creation</param> /// <returns>Return a typed complety entity</returns> public T Deserialize <T>(string expression, ComplexEntityFactory factory) { return(DeserializeAsync <T>(expression, factory).Result); }