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


C# JsonDeserializer.FromString方法代码示例

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


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

示例1: TestAfterDeserialization

        public void TestAfterDeserialization()
        {
            var js = new JsonSerializer();
            var v0 = new SampleAfter { X = "m" };
            var result0 = js.ToString(v0);
            Assert.AreEqual("{\n\t\"X\":\"m\"\n}", result0);

            var jd = new JsonDeserializer();
            var w0 = new SampleAfter();
            jd.FromString(w0, result0);
            Assert.AreEqual("m1", w0.X);

            var w1 = new SampleAfter2();
            jd.FromString(w1, result0);
            Assert.AreEqual("m231", w1.X);

            var w2 = (SampleAfter2)SampleAfter2_JsonDeserializer.Instance.FromString(result0);
            Assert.AreEqual("m231", w2.X);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:19,代码来源:TestJson.cs

示例2: TestJsonToBinarySimple

        public void TestJsonToBinarySimple()
        {
            var bs = new BinarySerializer();
            var bd = new BinaryDeserializer();
            var js = new JsonSerializer();
            js.JsonOptions.FieldSeparator = "";
            js.JsonOptions.Indent = "";
            js.JsonOptions.SaveRootClass = true;
            var jd = new JsonDeserializer();

            var data1 = "{\"class\":\"YuzuTest.Sample1, YuzuTest\",\"X\":98,\"Y\":\"\"}";
            var result1 = js.ToString(bd.FromBytes(bs.ToBytes(jd.FromString(data1))));
            Assert.AreEqual(data1, result1);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:14,代码来源:TestInterop.cs

示例3: TestAbstract

        public void TestAbstract()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.JsonOptions.FieldSeparator = "";
            js.JsonOptions.SaveRootClass = true;
            var jd = new JsonDeserializer();

            SampleAbstract v1 = new SampleConcrete { XX = 81 };
            var result1 = js.ToString(v1);
            Assert.AreEqual("{\"class\":\"YuzuTest.SampleConcrete, YuzuTest\",\"XX\":81}", result1);

            var w1 = jd.FromString<SampleAbstract>(result1);
            Assert.AreEqual((v1 as SampleConcrete).XX, (w1 as SampleConcrete).XX);
            var w1g = (SampleConcrete)SampleAbstract_JsonDeserializer.Instance.FromString(result1);
            Assert.AreEqual((v1 as SampleConcrete).XX, w1g.XX);

            var v2 = new List<SampleAbstract>();
            v2.Add(new SampleConcrete { XX = 51 });

            var w2 = jd.FromString<List<SampleAbstract>>(js.ToString(v2));
            Assert.AreEqual(v2.Count, w2.Count);
            Assert.AreEqual((v2[0] as SampleConcrete).XX, (w2[0] as SampleConcrete).XX);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:24,代码来源:TestJson.cs

示例4: TestBinaryToJsonUnknown

        public void TestBinaryToJsonUnknown()
        {
            var bs = new BinarySerializer();
            var bd = new BinaryDeserializer();
            var js = new JsonSerializer();
            js.JsonOptions.SaveRootClass = true;
            var jd = new JsonDeserializer();

            var n = "NewType".ToCharArray().Select(ch => (byte)ch).ToList();
            var data1 =
                new byte[] { 0x20, 0x01, 0x00, (byte)n.Count }.Concat(n).
                Concat(new byte[] { 0x01, 0x00, 0x01, (byte)'X', (byte)RoughType.String, 0x01, 0x00,
                    0x03, (byte)'a', (byte)'b', (byte)'c', 0, 0
                }).ToArray();
            var result1 = bs.ToBytes(jd.FromString(js.ToString(bd.FromBytes(data1))));
            CollectionAssert.AreEqual(data1, result1);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:17,代码来源:TestInterop.cs

示例5: TestJsonLongArrayStr

        public void TestJsonLongArrayStr()
        {
            var list1 = new SampleArray { A = new string[100000] };
            for (int i = 0; i < list1.A.Length; ++i)
                list1.A[i] = i.ToString();

            var js = new JsonSerializer();
            js.JsonOptions.ArrayLengthPrefix = true;
            var result1 = js.ToString(list1);
            Assert.IsTrue(result1 != "");

            var list2 = new SampleArray();
            var jd = new JsonDeserializer();
            jd.JsonOptions.ArrayLengthPrefix = true;
            jd.FromString(list2, result1);
            Assert.AreEqual(list1.A.Length, list2.A.Length);

            var jdg = new SampleArray_JsonDeserializer();
            var list3 = (SampleArray)jdg.FromString(result1);
            Assert.AreEqual(list1.A.Length, list3.A.Length);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:21,代码来源:Speed.cs

示例6: TestDictionary

        public void TestDictionary()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.Options.TagMode = TagMode.Names;
            var jd = new JsonDeserializer();
            jd.Options.TagMode = TagMode.Names;

            var v0 = new SampleDict {
                Value = 3, Children = new Dictionary<string, SampleDict> {
                { "a", new SampleDict { Value = 5, Children = new Dictionary<string, SampleDict>() } },
                { "b", new SampleDict { Value = 7 } },
            }
            };
            var result0 = js.ToString(v0);
            Assert.AreEqual(
                "{\n\"Value\":3,\n\"Children\":{\n" +
                "\"a\":{\n\"Value\":5,\n\"Children\":{}\n},\n" +
                "\"b\":{\n\"Value\":7,\n\"Children\":null\n}\n" +
                "}\n}", result0);

            var w0 = new SampleDict();
            jd.FromString(w0, result0);
            Assert.AreEqual(v0.Value, w0.Value);
            Assert.AreEqual(v0.Children.Count, w0.Children.Count);
            Assert.AreEqual(v0.Children["a"].Value, w0.Children["a"].Value);

            var w1 = (SampleDict)SampleDict_JsonDeserializer.Instance.FromString(result0);
            Assert.AreEqual(v0.Value, w1.Value);
            Assert.AreEqual(v0.Children.Count, w1.Children.Count);
            Assert.AreEqual(v0.Children["a"].Value, w1.Children["a"].Value);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:32,代码来源:TestJson.cs

示例7: TestDelegate

        public void TestDelegate()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.JsonOptions.FieldSeparator = "";

            var v1 = new SampleSelfDelegate { x = 77 };
            v1.OnSomething = v1.Handler1;
            var result = js.ToString(v1);
            Assert.AreEqual("{\"OnSomething\":\"Handler1\",\"x\":77}", result);

            var w1 = new SampleSelfDelegate();
            var jd = new JsonDeserializer();
            jd.FromString(w1, result);
            Assert.AreEqual(v1.x, w1.x);
            w1.OnSomething(10);
            Assert.AreEqual(87, w1.x);

            jd.FromString(w1, result.Replace("Handler1", "Handler2"));
            w1.OnSomething(10);
            Assert.AreEqual(770, w1.x);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:22,代码来源:TestJson.cs

示例8: TestTopLevelList

        public void TestTopLevelList()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            var jd = new JsonDeserializer();

            var v0 = new List<string> { "a", "b", "c" };
            var result0 = js.ToString(v0);
            Assert.AreEqual("[\n\"a\",\n\"b\",\n\"c\"\n]", result0);

            var w0 = new List<string>();
            jd.FromString(w0, result0);
            CollectionAssert.AreEqual(v0, w0);
            jd.FromString(w0, "[]");
            CollectionAssert.AreEqual(v0, w0);
            jd.FromString(w0, result0);
            CollectionAssert.AreEqual(new List<string> { "a", "b", "c", "a", "b", "c" }, w0);

            var v1 = new List<List<int>> { new List<int> { 1, 2 }, new List<int> { 3 } };
            var result1 = js.ToString(v1);
            Assert.AreEqual("[\n[\n1,\n2\n],\n[\n3\n]\n]", result1);
            List<List<int>> w1 = new List<List<int>>();
            YuzuGen.System.Collections.Generic.List_List_Int32_JsonDeserializer.Instance.FromString(w1, result1);
            Assert.AreEqual(v1.Count, w1.Count);
            CollectionAssert.AreEqual(v1[0], w1[0]);
            CollectionAssert.AreEqual(v1[1], w1[1]);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:27,代码来源:TestJson.cs

示例9: TestDecimal

        public void TestDecimal()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";

            var v = new SampleDecimal { N = -12.34m };

            var jd = new JsonDeserializer();

            var result1 = js.ToString(v);
            Assert.AreEqual("{\n\"N\":-12.34\n}", result1);
            var w1 = new SampleDecimal();
            jd.FromString(w1, result1);
            Assert.AreEqual(v.N, w1.N);

            jd.JsonOptions.DecimalAsString = js.JsonOptions.DecimalAsString = true;
            var result2 = js.ToString(v);
            Assert.AreEqual("{\n\"N\":\"-12.34\"\n}", result2);
            var w2 = new SampleDecimal();
            jd.FromString(w2, result2);
            Assert.AreEqual(v.N, w2.N);

            var w3 = (SampleDecimal)SampleDecimal_JsonDeserializer.Instance.FromString(result1);
            Assert.AreEqual(v.N, w3.N);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:25,代码来源:TestJson.cs

示例10: TestAssemblies

        public void TestAssemblies()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.JsonOptions.FieldSeparator = "";
            var jd = new JsonDeserializer();

            var v1 = new List<SampleAssemblyBase> {
                new SampleAssemblyDerivedQ { Q = 10 },
                new SampleAssemblyDerivedR { R = "R1" } };
            var result1 = js.ToString(v1);
            Assert.AreEqual(
                "[{\"class\":\"YuzuTestAssembly.SampleAssemblyDerivedQ, AssemblyTest\",\"Q\":10}," +
                "{\"class\":\"YuzuTest.SampleAssemblyDerivedR, YuzuTest\",\"R\":\"R1\"}]",
                result1);

            var w1 = new List<SampleAssemblyBase>();
            jd.FromString(w1, result1);
            Assert.AreEqual((v1[0] as SampleAssemblyDerivedQ).Q, (w1[0] as SampleAssemblyDerivedQ).Q);
            Assert.AreEqual((v1[1] as SampleAssemblyDerivedR).R, (w1[1] as SampleAssemblyDerivedR).R);

            var w1g = (List<SampleAssemblyBase>)
                YuzuGen.System.Collections.Generic.List_SampleAssemblyBase_JsonDeserializer.Instance.FromString(result1);
            Assert.AreEqual((v1[0] as SampleAssemblyDerivedQ).Q, (w1g[0] as SampleAssemblyDerivedQ).Q);
            Assert.AreEqual((v1[1] as SampleAssemblyDerivedR).R, (w1g[1] as SampleAssemblyDerivedR).R);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:26,代码来源:TestJson.cs

示例11: TestLong

        public void TestLong()
        {
            var js = new JsonSerializer();
            var v1 = new SampleLong { S = -1L << 33, U = 1UL << 33 };

            js.JsonOptions.Indent = "";
            var result = js.ToString(v1);
            Assert.AreEqual("{\n\"S\":-8589934592,\n\"U\":8589934592\n}", result);

            var v2 = new SampleLong();
            var jd = new JsonDeserializer();
            jd.FromString(v2, result);
            Assert.AreEqual(v1.S, v2.S);
            Assert.AreEqual(v1.U, v2.U);

            js.JsonOptions.Int64AsString = true;
            var result1 = js.ToString(v1);
            Assert.AreEqual("{\n\"S\":\"-8589934592\",\n\"U\":\"8589934592\"\n}", result1);
            var jd1 = new JsonDeserializer();
            jd1.JsonOptions.Int64AsString = true;
            jd1.FromString(v2, result1);
            Assert.AreEqual(v1.S, v2.S);
            Assert.AreEqual(v1.U, v2.U);

            js.JsonOptions.Int64AsString = false;
            v1.S = long.MinValue;
            v1.U = ulong.MaxValue;
            jd.FromString(v2, js.ToString(v1));
            Assert.AreEqual(v1.S, v2.S);
            Assert.AreEqual(v1.U, v2.U);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:31,代码来源:TestJson.cs

示例12: TestErrors

        public void TestErrors()
        {
            var js = new JsonSerializer();
            XAssert.Throws<YuzuException>(() => js.ToString(new Empty()), "Empty");
            XAssert.Throws<YuzuException>(() => js.ToString(new SampleCollectionWithField<int>()), "collection");
            XAssert.Throws<YuzuException>(() => js.ToString(new SampleInterfacedFieldDup()), "X");
            XAssert.Throws<YuzuException>(() => js.ToString(new BadMerge1()), "merge");
            XAssert.Throws<YuzuException>(() => js.ToString(new BadMerge2()), "merge");

            var jd = new JsonDeserializer();
            var w = new Sample1();
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{}"), "X");
            XAssert.Throws<YuzuException>(() => jd.FromString("{null:1}"), "'n'");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\" }"), ":");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "nn"), "'u'");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\":1, \"Y\": \"\\z\" }"), "z");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\":1, \"Y\": \"\\uQ\" }"), "Q");
            XAssert.Throws<YuzuException>(() => jd.FromString(new SampleBool(), "{ \"B\": 1 }"), "1");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ ,}"), ",");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"Y\": \"q\" }"), "'X'");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "[]"), "'Sample1'");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"class\": \"Q\" }"), "'Q'");
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"class\": \"YuzuTest.Sample2, YuzuTest\" }"), ".Sample2");
            XAssert.Throws<YuzuException>(() => jd.FromString(
                new Sample2Allow(), "{ \"class\": \"YuzuTest.Sample1, YuzuTest\" }"), ".Sample1");
            XAssert.Throws<YuzuException>(() => jd.FromString(new SamplePoint(), "[ \"QQ\" ]"), "");
            XAssert.Throws<YuzuException>(() => jd.FromString(new object(), "{}"), "object");
            XAssert.Throws<EndOfStreamException>(() => jd.FromString(""), "");
            XAssert.Throws<EndOfStreamException>(() => jd.FromString(w, "{ \"X\": 1"));
            XAssert.Throws<YuzuException>(() => jd.FromString(
                "{\"class\":\"YuzuTest.SampleInterfaceField, YuzuTest\",\"I\":{}}"), "class");
            XAssert.Throws<YuzuException>(() => jd.FromString(
                "{\"class\":\"YuzuTest.SampleInterfaceField, YuzuTest\",\"I\":{\"class\":\"YuzuTest.Sample1, YuzuTest\"}}"),
                "ISample");

            jd.Options.ReportErrorPosition = true;
            XAssert.Throws<YuzuException>(() => jd.FromString(w, "      z"), "7");
            jd.Options.ReportErrorPosition = false;
            try {
                jd.FromString(w, "      z");
            }
            catch (YuzuException e) {
                Assert.IsFalse(e.Message.Contains("7"));
            }
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:45,代码来源:TestJson.cs

示例13: TestFloat

        public void TestFloat()
        {
            var js = new JsonSerializer();
            js.Options.TagMode = TagMode.Names;

            var v = new SampleFloat { F = 1e-20f, D = -3.1415e100d };
            js.JsonOptions.Indent = "";

            var result1 = js.ToString(v);
            Assert.AreEqual("{\n\"F\":1E-20,\n\"D\":-3.1415E100\n}", result1);

            var w = new SampleFloat();
            var jd = new JsonDeserializer();
            jd.Options.TagMode = TagMode.Names;
            jd.FromString(w, result1);
            Assert.AreEqual(v.F, w.F);
            Assert.AreEqual(v.D, w.D);

            Assert.AreEqual("NaN", js.ToString(Double.NaN));
            Assert.AreEqual("Infinity", js.ToString(Double.PositiveInfinity));
            Assert.AreEqual("-Infinity", js.ToString(Double.NegativeInfinity));
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:22,代码来源:TestJson.cs

示例14: TestBool

        public void TestBool()
        {
            var js = new JsonSerializer();

            var v = new SampleBool { B = true };
            js.JsonOptions.Indent = "";

            var result1 = js.ToString(v);
            Assert.AreEqual("{\n\"B\":true\n}", result1);

            var jd = new JsonDeserializer();
            var w = new SampleBool();
            jd.FromString(w, result1);
            Assert.AreEqual(true, w.B);

            w = (SampleBool)SampleBool_JsonDeserializer.Instance.FromString(result1);
            Assert.AreEqual(true, w.B);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:18,代码来源:TestJson.cs

示例15: TestClassList

        public void TestClassList()
        {
            var js = new JsonSerializer();
            js.Options.TagMode = TagMode.Names;
            js.JsonOptions.SaveRootClass = true;
            var jd = new JsonDeserializer();
            jd.Options.TagMode = TagMode.Names;

            var v = new SampleClassList {
                E = new List<SampleBase> {
                    new SampleDerivedA(),
                    new SampleDerivedB { FB = 9 },
                    new SampleDerivedB { FB = 8 },
                }
            };

            var result = js.ToString(v);
            var w = (SampleClassList)jd.FromString(result);

            Assert.AreEqual(3, w.E.Count);
            Assert.IsInstanceOfType(w.E[0], typeof(SampleDerivedA));
            Assert.IsInstanceOfType(w.E[1], typeof(SampleDerivedB));
            Assert.AreEqual(9, ((SampleDerivedB)w.E[1]).FB);
            Assert.IsInstanceOfType(w.E[2], typeof(SampleDerivedB));
            Assert.AreEqual(8, ((SampleDerivedB)w.E[2]).FB);

            w = (SampleClassList)SampleClassList_JsonDeserializer.Instance.FromString(result);
            Assert.AreEqual(3, w.E.Count);
            Assert.IsInstanceOfType(w.E[0], typeof(SampleDerivedA));
            Assert.IsInstanceOfType(w.E[1], typeof(SampleDerivedB));
            Assert.AreEqual(9, ((SampleDerivedB)w.E[1]).FB);
            Assert.IsInstanceOfType(w.E[2], typeof(SampleDerivedB));
            Assert.AreEqual(8, ((SampleDerivedB)w.E[2]).FB);
        }
开发者ID:klenin,项目名称:Yuzu,代码行数:34,代码来源:TestJson.cs


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