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");
            ExceptionTestHelper.ExpectException<InvalidOperationException>(() => target = JsonValueExtensions.CreateFrom(dyn), expectedMessage);

            dyn.UseErrorSuggestion = true;
            ExceptionTestHelper.ExpectException<TestDynamicObject.TestDynamicObjectException>(() => target = JsonValueExtensions.CreateFrom(dyn));
        }
Example #2
0
        public void DynamicCamelCasePropertyNames()
        {
            dynamic o = new TestDynamicObject();

            o.Text    = "Text!";
            o.Integer = int.MaxValue;

            string json = JsonConvert.SerializeObject(o, Formatting.Indented,
                                                      new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            StringAssert.AreEqual(@"{
  ""explicit"": false,
  ""text"": ""Text!"",
  ""integer"": 2147483647,
  ""int"": 0,
  ""childObject"": null
}", json);
        }
        public void CanUntrackDynamicProperties()
        {
            const string text1 = "hello";
            const string text2 = "world";

            dynamic a = TrackableObjectFactory.CreateFrom
                        (
                new TestDynamicObject
            {
                DeclaredText = text1
            }
                        );

            a.DynamicText = text2;
            a.A           = new A();

            TestDynamicObject untrackedA = ((TestDynamicObject)a).ToUntracked();

            Assert.AreEqual(text1, untrackedA.DeclaredText);
            Assert.AreEqual(text2, ((dynamic)untrackedA).DynamicText);
            Assert.IsNotNull(((dynamic)untrackedA).A);
            Assert.IsFalse(untrackedA.IsTrackable());
            Assert.IsNotNull(((TestDynamicObject)((dynamic)untrackedA)).IsTrackable());
        }
Example #4
0
        public void GetParam_Tuple(object param)
        {
            WriteXmlAndXslFiles();

            object t = null;
            switch ((int)param)
            {
                case 1: t = Tuple.Create(1, "Melitta", 7.5); break;
                case 2: t = new TestDynamicObject(); break;
                case 3: t = new Guid(); break;
                case 4: t = new Dictionary<string, object>(); break;
            }
            _output.WriteLine(t.ToString());

            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load("type.xsl");

            XsltArgumentList xslArg = new XsltArgumentList();
            xslArg.AddParam("param", "", t);
            xslArg.AddExtensionObject("", t);

            try
            {
                xslt.Transform("type.xml", xslArg, new StringWriter());
            }
            catch (Exception e)
            {
                _output.WriteLine(e.Message);
                return;
            }
            Assert.True(false);
        }
Example #5
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;
        }
        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.IsNotNull(target);
            Assert.AreEqual<string>(expected, target.ToString());
        }
        public void CreateFromDynamicWithJsonValueChildrenTest()
        {
            JsonValue target;
            string level3 = "{\"Name\":\"Level3\",\"Null\":null}";
            string level2 = "{\"Name\":\"Level2\",\"JsonObject\":" + AnyInstance.AnyJsonObject + ",\"JsonArray\":" + AnyInstance.AnyJsonArray + ",\"Level3\":" + level3 + "}";
            string level1 = "{\"Name\":\"Level1\",\"JsonPrimitive\":" + AnyInstance.AnyJsonPrimitive + ",\"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.AreEqual<string>(expected, target.ToString());
        }
        public void CreateFromDynamicSimpleTest()
        {
            JsonValue target;

            target = JsonValueExtensions.CreateFrom(AnyInstance.AnyDynamic);
            Assert.IsNotNull(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.AreEqual<string>(expected, target.ToString());

            target = JsonValueExtensions.CreateFrom(new TestDynamicObject());
            Assert.AreEqual<string>("{}", target.ToString());
        }
    public void SerializeDynamicObject()
    {
      TestDynamicObject dynamicObject = new TestDynamicObject();

      dynamic d = dynamicObject;
      d.Int = 1;
      d.Decimal = 99.9d;
      d.ChildObject = new DynamicChildObject();

      Dictionary<string, object> values = new Dictionary<string, object>();

      foreach (string memberName in dynamicObject.GetDynamicMemberNames())
      {
        object value;
        dynamicObject.TryGetMember(memberName, out value);

        values.Add(memberName, value);
      }

      Assert.AreEqual(d.Int, values["Int"]);
      Assert.AreEqual(d.Decimal, values["Decimal"]);
      Assert.AreEqual(d.ChildObject, values["ChildObject"]);

      string json = JsonConvert.SerializeObject(dynamicObject, Formatting.Indented);
      Assert.AreEqual(@"{
  ""Decimal"": 99.9,
  ""Int"": 1,
  ""ChildObject"": {
    ""Text"": null,
    ""Integer"": 0
  }
}", json);

      TestDynamicObject newDynamicObject = JsonConvert.DeserializeObject<TestDynamicObject>(json);
      d = newDynamicObject;

      Assert.AreEqual(99.9, d.Decimal);
      Assert.AreEqual(1, d.Int);
      Assert.AreEqual(dynamicObject.ChildObject.Integer, d.ChildObject.Integer);
      Assert.AreEqual(dynamicObject.ChildObject.Text, d.ChildObject.Text);
    }