当前位置: 首页>>代码示例>>C#>>正文


C# Object.ToJson方法代码示例

本文整理汇总了C#中System.Object.ToJson方法的典型用法代码示例。如果您正苦于以下问题:C# Object.ToJson方法的具体用法?C# Object.ToJson怎么用?C# Object.ToJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Object的用法示例。


在下文中一共展示了Object.ToJson方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Test1

        public void Test1()
        {
            typeof(Foo).Config().DefaultEngine().OptOutNonPublic.NotSlots(mi => mi.Name == "Calc");
            typeof(IBar).GetProperty("Qux").Config().AfterDeserialize((Qux qux) => { qux.Value *= 10; return qux; });
            typeof(IBar).GetProperty("Qux").Config().BeforeSerialize((Qux qux) => { qux.Value /= 10; return qux; });
            Properties.Rule(pi => pi.DeclaringType == typeof(Foo)).AfterDeserialize((pi, o) => Log.WriteLine("AfterDeserialize Foo::{0}", pi.Name).Ignore());
            typeof(Foo).GetProperty("Ok").Config().Engine((pi, j) => { Log.WriteLine("Deserializing Foo::Ok"); return new DefaultEngine().Deserialize(pi.PropertyType, j); },
                (pi, o) => { Log.WriteLine("Serializing Foo::Ok"); return new DefaultEngine().Serialize(pi.PropertyType, o); });
            typeof(Foo).GetProperty("Ok").Config().AddValidator(_ => Log.WriteLine("Validating Foo::Ok"));
            typeof(Foo).Config().AddValidator(_ => Log.WriteLine("Validating Foo"));
            typeof(Foo).Config().AfterDeserialize(o => Log.WriteLine("AfterDeserialize Foo").Ignore());

            var s_json = InputText();
            var json = Json.Parse(s_json);
            Assert.AreEqual(new Json(false), json[0].ok);
            Assert.AreEqual(false, (bool)json[0].ok);
            Assert.AreEqual(new Json("e/*i,[{]}',[{]}\\,[{]}\",[{]}n//*/"), json[0].bars.ein[0].baz);
            Assert.AreEqual(2, (int)json[0].bars.ein[1].qux);
            Assert.AreEqual(new Json(new {}), json[0].foos[1].bars);
            Assert.AreEqual(new Json(null), json[0].bars.drei);
            Assert.AreEqual(null, (IList<IBar>)json[0].bars.drei);
            // todo. find out why this doesn't work
//            Assert.IsTrue(Seq.Equal(new []{0, 1}, json.Keys));
            Assert.IsTrue(Seq.Equal(new []{0, 1}, ((IEnumerable<Object>)json.Keys).Cast<int>()));
            Assert.IsTrue(Seq.Equal(new []{"ok", "bars", "foos"}, json[0].Keys));

            var anon = new Object[]
            {
                new
                {
                    ok = false, 
                    bars = new
                    {
                        ein = new []
                        {
                            new { qux = 1, baz = "e/*i,[{]}',[{]}\\,[{]}\",[{]}n//*/" },
                            new { qux = 2, baz = "" }
                        },
                        zwei = new []
                        {
                            new { qux = 1 },
                        },
                        drei = (Object)null
                    },
                    foos = new Object[]
                    {
                        new { ok = true },
                        new { ok = false, bars = new { } },
                    }
                },
                new
                {
                    ok = true
                }
            };
            Assert.AreEqual(anon.ToJson(), json);
            Assert.AreEqual(anon.ToJson(), ((Json)json).ToJson());
            Assert.AreEqual(anon.ToJson().ToCompactString(), json.ToCompactString());
            Assert.AreEqual(anon.ToJson().ToPrettyString(), json.ToPrettyString());

            var foos = (List<Foo>)json;
            Assert.AreEqual(2, foos.Count);
            var foo0 = foos[0];
                Assert.AreEqual(false, foo0.Ok);
                var foo0_bars = foo0.Bars;
                    var bar0 = foo0_bars.AsEnumerable().Nth(0);
                        Assert.AreEqual("ein", bar0.Key);
                        var bar0_value = bar0.Value;
                        Assert.AreEqual(2, bar0_value.Count);
                        var bar0_value0 = bar0_value[0];
                            Assert.AreEqual(new Qux(10), bar0_value0.Qux); // property adapter!
                            Assert.AreEqual("e/*i,[{]}',[{]}\\,[{]}\",[{]}n//*/", bar0_value0.Baz);
                        var bar0_value1 = bar0_value[1];
                            Assert.AreEqual(new Qux(20), bar0_value1.Qux); // property adapter!
                            Assert.AreEqual("", bar0_value1.Baz);
                    var bar1 = foo0_bars.AsEnumerable().Nth(1);
                        Assert.AreEqual("zwei", bar1.Key);
                        var bar1_value = bar1.Value;
                        Assert.AreEqual(1, bar1_value.Count);
                        var bar1_value0 = bar1_value[0];
                            Assert.AreEqual(new Qux(10), bar1_value0.Qux); // property adapter!
                            Assert.AreEqual("null", bar1_value0.Baz); // type adapter!
                    var bar2 = foo0_bars.AsEnumerable().Nth(2);
                        Assert.AreEqual("drei", bar2.Key);
                        Assert.AreEqual(null, bar2.Value);
                var foo0_foos = foo0.Foos;
                Assert.AreEqual(2, foo0_foos.Count);
                var foo0_foo0 = foo0_foos[0];
                    Assert.AreEqual(true, foo0_foo0.Ok);
                    Assert.AreEqual(null, foo0_foo0.Bars);
                    Assert.AreEqual(null, foo0_foo0.Foos);
                var foo0_foo1 = foo0_foos[1];
                    Assert.AreEqual(false, foo0_foo1.Ok);
                    Assert.AreEqual(0, foo0_foo1.Bars.Count);
                    Assert.AreEqual(null, foo0_foo1.Foos);
            var foo1 = foos[1];
                Assert.AreEqual(true, foo1.Ok);
                Assert.AreEqual(null, foo1.Bars);
                Assert.AreEqual(null, foo1.Foos);

//.........这里部分代码省略.........
开发者ID:xeno-by,项目名称:xenogears,代码行数:101,代码来源:JsonTests.cs


注:本文中的System.Object.ToJson方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。