コード例 #1
0
 public void Export()
 {
     var context = JsonConvert.CreateImportContext();
     var importer = new ExpandoObjectImporter();
     var expando = importer.Import(context, JsonText.CreateReader(@"
     {
         num: 123, flag: true, str: foobar, nil: null,
         obj: { x: 456, y: 789 },
         arr: [ 123, { x: 456, y: 789 } ],
     }"));
     Assert.IsNotNull(expando);
     Assert.IsInstanceOfType(typeof(ExpandoObject), expando);
     dynamic obj = expando;
     Assert.AreEqual(123, ((JsonNumber) obj.num).ToInt32());
     Assert.IsTrue((bool) obj.flag);
     Assert.AreEqual("foobar", (string) obj.str);
     Assert.IsInstanceOfType(typeof(ExpandoObject), (object) obj.obj);
     Assert.AreEqual(456, ((JsonNumber) obj.obj.x).ToInt32());
     Assert.AreEqual(789, ((JsonNumber) obj.obj.y).ToInt32());
     Assert.IsTrue(typeof(IList<object>).IsAssignableFrom(((object)obj.arr).GetType()));
     var arr = (IList<object>) obj.arr;
     Assert.AreEqual(123, ((JsonNumber) arr[0]).ToInt32());
     Assert.IsInstanceOfType(typeof(ExpandoObject), arr[1]);
     Assert.AreEqual(456, ((JsonNumber) (((dynamic) (arr[1])).x)).ToInt32());
     Assert.AreEqual(789, ((JsonNumber) (((dynamic) (arr[1])).y)).ToInt32());
 }
コード例 #2
0
        public void Export()
        {
            var context  = JsonConvert.CreateImportContext();
            var importer = new ExpandoObjectImporter();
            var expando  = importer.Import(context, JsonText.CreateReader(@"
            {
                num: 123, flag: true, str: foobar, nil: null,
                obj: { x: 456, y: 789 },
                arr: [ 123, { x: 456, y: 789 } ],
            }"));

            Assert.IsNotNull(expando);
            Assert.IsInstanceOf <ExpandoObject>(expando);
            dynamic obj = expando;

            Assert.AreEqual(123, ((JsonNumber)obj.num).ToInt32());
            Assert.IsTrue((bool)obj.flag);
            Assert.AreEqual("foobar", (string)obj.str);
            Assert.IsInstanceOf <ExpandoObject>((object)obj.obj);
            Assert.AreEqual(456, ((JsonNumber)obj.obj.x).ToInt32());
            Assert.AreEqual(789, ((JsonNumber)obj.obj.y).ToInt32());
            Assert.IsInstanceOf <IList <object> >((object)obj.arr);
            var arr = (IList <object>)obj.arr;

            Assert.AreEqual(123, ((JsonNumber)arr[0]).ToInt32());
            Assert.IsInstanceOf <ExpandoObject>(arr[1]);
            Assert.AreEqual(456, ((JsonNumber)(((dynamic)(arr[1])).x)).ToInt32());
            Assert.AreEqual(789, ((JsonNumber)(((dynamic)(arr[1])).y)).ToInt32());
        }