public void CreateFromDynamicWithJsonValueChildrenTest()
        {
            JsonValue target;
            string    level3   = "{\"Name\":\"Level3\",\"Null\":null}";
            string    level2   = "{\"Name\":\"Level2\",\"JsonObject\":" + AnyInstance.AnyJsonObject.ToString() + ",\"JsonArray\":" + AnyInstance.AnyJsonArray.ToString() + ",\"Level3\":" + level3 + "}";
            string    level1   = "{\"Name\":\"Level1\",\"JsonPrimitive\":" + AnyInstance.AnyJsonPrimitive.ToString() + ",\"Level2\":" + level2 + "}";
            string    expected = "{\"Name\":\"Root\",\"Level1\":" + level1 + "}";

            dynamic dyn = new TestDynamicObject();

            dyn.Name                      = "Root";
            dyn.Level1                    = new TestDynamicObject();
            dyn.Level1.Name               = "Level1";
            dyn.Level1.JsonPrimitive      = AnyInstance.AnyJsonPrimitive;
            dyn.Level1.Level2             = new TestDynamicObject();
            dyn.Level1.Level2.Name        = "Level2";
            dyn.Level1.Level2.JsonObject  = AnyInstance.AnyJsonObject;
            dyn.Level1.Level2.JsonArray   = AnyInstance.AnyJsonArray;
            dyn.Level1.Level2.Level3      = new TestDynamicObject();
            dyn.Level1.Level2.Level3.Name = "Level3";
            dyn.Level1.Level2.Level3.Null = null;

            target = JsonValueExtensions.CreateFrom(dyn);
            Assert.Equal <string>(expected, target.ToString());
        }
        public void CreateFromDeepNestedDynamic()
        {
            int    count    = 5000;
            string expected = "";

            dynamic dyn = new TestDynamicObject();
            dynamic cur = dyn;

            for (int i = 0; i < count; i++)
            {
                expected         += "{\"" + i + "\":";
                cur[i.ToString()] = new TestDynamicObject();
                cur = cur[i.ToString()];
            }

            expected += "{}";

            for (int i = 0; i < count; i++)
            {
                expected += "}";
            }

            JsonValue jv = JsonValueExtensions.CreateFrom(dyn);

            Assert.Equal(expected, jv.ToString());
        }
        public void CreateFromDynamicComplextTest()
        {
            JsonValue target;
            Person    person = AnyInstance.AnyPerson;
            dynamic   dyn    = TestDynamicObject.CreatePersonAsDynamic(person);

            dyn.TestProperty = AnyInstance.AnyString;

            target = JsonValueExtensions.CreateFrom(dyn);
            Assert.NotNull(target);
            Assert.Equal <string>(AnyInstance.AnyString, dyn.TestProperty);
            Person jvPerson = target.ReadAsType <Person>();

            Assert.Equal(person.ToString(), jvPerson.ToString());

            Person p1 = Person.CreateSample();
            Person p2 = Person.CreateSample();

            p2.Name         += "__2";
            p2.Age          += 10;
            p2.Address.City += "__2";

            Person[] friends = new Person[] { p1, p2 };
            target = JsonValueExtensions.CreateFrom(friends);
            Person[] personArr = target.ReadAsType <Person[]>();
            Assert.Equal <int>(friends.Length, personArr.Length);
            Assert.Equal <string>(friends[0].ToString(), personArr[0].ToString());
            Assert.Equal <string>(friends[1].ToString(), personArr[1].ToString());
        }
        public static dynamic CreatePersonAsDynamic(Person person)
        {
            dynamic dynObj = new TestDynamicObject();

            dynObj.Name           = person.Name;
            dynObj.Age            = person.Age;
            dynObj.Address        = new Address();
            dynObj.Address.City   = person.Address.City;
            dynObj.Address.Street = person.Address.Street;
            dynObj.Address.State  = person.Address.State;
            dynObj.Friends        = person.Friends;

            return(dynObj);
        }
        public void CreateFromNestedDynamicTest()
        {
            JsonValue target;
            string    expected = "{\"Name\":\"Root\",\"Level1\":{\"Name\":\"Level1\",\"Level2\":{\"Name\":\"Level2\"}}}";
            dynamic   dyn      = new TestDynamicObject();

            dyn.Name               = "Root";
            dyn.Level1             = new TestDynamicObject();
            dyn.Level1.Name        = "Level1";
            dyn.Level1.Level2      = new TestDynamicObject();
            dyn.Level1.Level2.Name = "Level2";

            target = JsonValueExtensions.CreateFrom(dyn);
            Assert.NotNull(target);
            Assert.Equal <string>(expected, target.ToString());
        }
        public void CreateFromDynamicBinderFallbackTest()
        {
            JsonValue target;
            Person    person = AnyInstance.AnyPerson;
            dynamic   dyn    = new TestDynamicObject();

            dyn.Name = AnyInstance.AnyString;

            dyn.UseFallbackMethod = true;
            string expectedMessage = String.Format(DynamicPropertyNotDefined, dyn.GetType().FullName, "Name");

            ExceptionHelper.Throws <InvalidOperationException>(() => target = JsonValueExtensions.CreateFrom(dyn), expectedMessage);

            dyn.UseErrorSuggestion = true;
            ExceptionHelper.Throws <TestDynamicObject.TestDynamicObjectException>(() => target = JsonValueExtensions.CreateFrom(dyn));
        }
        public void CreateFromDynamicSimpleTest()
        {
            JsonValue target;

            target = JsonValueExtensions.CreateFrom(AnyInstance.AnyDynamic);
            Assert.NotNull(target);

            string expected = "{\"Name\":\"Bill Gates\",\"Age\":21,\"Grades\":[\"F\",\"B-\",\"C\"]}";
            dynamic obj = new TestDynamicObject();
            obj.Name = "Bill Gates";
            obj.Age = 21;
            obj.Grades = new[] { "F", "B-", "C" };

            target = JsonValueExtensions.CreateFrom(obj);
            Assert.Equal<string>(expected, target.ToString());

            target = JsonValueExtensions.CreateFrom(new TestDynamicObject());
            Assert.Equal<string>("{}", target.ToString());
        }
        public void CreateFromDynamicSimpleTest()
        {
            JsonValue target;

            target = JsonValueExtensions.CreateFrom(AnyInstance.AnyDynamic);
            Assert.NotNull(target);

            string  expected = "{\"Name\":\"Bill Gates\",\"Age\":21,\"Grades\":[\"F\",\"B-\",\"C\"]}";
            dynamic obj      = new TestDynamicObject();

            obj.Name   = "Bill Gates";
            obj.Age    = 21;
            obj.Grades = new[] { "F", "B-", "C" };

            target = JsonValueExtensions.CreateFrom(obj);
            Assert.Equal <string>(expected, target.ToString());

            target = JsonValueExtensions.CreateFrom(new TestDynamicObject());
            Assert.Equal <string>("{}", target.ToString());
        }
        public void CreateFromDynamicWithJsonValueChildrenTest()
        {
            JsonValue target;
            string level3 = "{\"Name\":\"Level3\",\"Null\":null}";
            string level2 = "{\"Name\":\"Level2\",\"JsonObject\":" + AnyInstance.AnyJsonObject.ToString() + ",\"JsonArray\":" + AnyInstance.AnyJsonArray.ToString() + ",\"Level3\":" + level3 + "}";
            string level1 = "{\"Name\":\"Level1\",\"JsonPrimitive\":" + AnyInstance.AnyJsonPrimitive.ToString() + ",\"Level2\":" + level2 + "}";
            string expected = "{\"Name\":\"Root\",\"Level1\":" + level1 + "}";

            dynamic dyn = new TestDynamicObject();
            dyn.Name = "Root";
            dyn.Level1 = new TestDynamicObject();
            dyn.Level1.Name = "Level1";
            dyn.Level1.JsonPrimitive = AnyInstance.AnyJsonPrimitive;
            dyn.Level1.Level2 = new TestDynamicObject();
            dyn.Level1.Level2.Name = "Level2";
            dyn.Level1.Level2.JsonObject = AnyInstance.AnyJsonObject;
            dyn.Level1.Level2.JsonArray = AnyInstance.AnyJsonArray;
            dyn.Level1.Level2.Level3 = new TestDynamicObject();
            dyn.Level1.Level2.Level3.Name = "Level3";
            dyn.Level1.Level2.Level3.Null = null;

            target = JsonValueExtensions.CreateFrom(dyn);
            Assert.Equal<string>(expected, target.ToString());
        }
        public void CreateFromNestedDynamicTest()
        {
            JsonValue target;
            string expected = "{\"Name\":\"Root\",\"Level1\":{\"Name\":\"Level1\",\"Level2\":{\"Name\":\"Level2\"}}}";
            dynamic dyn = new TestDynamicObject();
            dyn.Name = "Root";
            dyn.Level1 = new TestDynamicObject();
            dyn.Level1.Name = "Level1";
            dyn.Level1.Level2 = new TestDynamicObject();
            dyn.Level1.Level2.Name = "Level2";

            target = JsonValueExtensions.CreateFrom(dyn);
            Assert.NotNull(target);
            Assert.Equal<string>(expected, target.ToString());
        }
        public void CreateFromDynamicBinderFallbackTest()
        {
            JsonValue target;
            Person person = AnyInstance.AnyPerson;
            dynamic dyn = new TestDynamicObject();
            dyn.Name = AnyInstance.AnyString;

            dyn.UseFallbackMethod = true;
            string expectedMessage = String.Format(DynamicPropertyNotDefined, dyn.GetType().FullName, "Name");
            ExceptionHelper.Throws<InvalidOperationException>(() => target = JsonValueExtensions.CreateFrom(dyn), expectedMessage);

            dyn.UseErrorSuggestion = true;
            ExceptionHelper.Throws<TestDynamicObject.TestDynamicObjectException>(() => target = JsonValueExtensions.CreateFrom(dyn));
        }
Example #12
0
        public static dynamic CreatePersonAsDynamic(Person person)
        {
            dynamic dynObj = new TestDynamicObject();

            dynObj.Name = person.Name;
            dynObj.Age = person.Age;
            dynObj.Address = new Address();
            dynObj.Address.City = person.Address.City;
            dynObj.Address.Street = person.Address.Street;
            dynObj.Address.State = person.Address.State;
            dynObj.Friends = person.Friends;

            return dynObj;
        }