示例#1
0
文件: TestJson.cs 项目: klenin/Yuzu
        public void TestUnknownStorage()
        {
            var js = new JsonSerializer();
            js.Options.AllowUnknownFields = true;
            js.JsonOptions.Indent = "";
            js.JsonOptions.FieldSeparator = "";

            var jd = new JsonDeserializer();
            jd.Options.AllowUnknownFields = true;

            var w = new SampleUnknown();
            jd.FromString(w, "{}");
            Assert.AreEqual(0, w.X);

            jd.FromString(w, "{\"A\":10}");
            Assert.AreEqual(1, w.Storage.Fields.Count);
            Assert.AreEqual("A", w.Storage.Fields[0].Name);
            Assert.AreEqual(10.0, w.Storage.Fields[0].Value);
            Assert.AreEqual("{\"A\":10}", js.ToString(w));

            jd.FromString(w, "{\"X\":77,\"Y\":{}}");
            Assert.AreEqual(77, w.X);
            Assert.AreEqual(1, w.Storage.Fields.Count);
            Assert.AreEqual("Y", w.Storage.Fields[0].Name);
            Assert.IsInstanceOfType(w.Storage.Fields[0].Value, typeof(Dictionary<string, object>));
            Assert.AreEqual("{\"X\":77,\"Y\":{}}", js.ToString(w));

            jd.JsonOptions.Unordered = true;
            jd.FromString(w, "{\"X\":88,\"Y\":\"qq\",\"B\":0}");
            Assert.AreEqual("{\"B\":0,\"X\":88,\"Y\":\"qq\"}", js.ToString(w));
            js.JsonOptions.Unordered = true;
            Assert.AreEqual("{\"X\":88,\"B\":0,\"Y\":\"qq\"}", js.ToString(w));
        }
示例#2
0
文件: TestBinary.cs 项目: klenin/Yuzu
        public void TestUnknownStorage()
        {
            var bs = new BinarySerializer();
            var bd = new BinaryDeserializer();
            bd.Options.AllowUnknownFields = true;

            var data =
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 03 00 " +
                XS("A", RoughType.String, "X", RoughType.Int, "Z", RoughType.Bool) +
                " 01 00 " + XS("qq") + " 02 00 02 01 00 00 03 00 01 00 00";
            var w = new SampleUnknown();
            bd.FromBytes(w, SX(data));
            Assert.AreEqual(258, w.X);
            Assert.AreEqual(2, w.Storage.Fields.Count);
            Assert.AreEqual("A", w.Storage.Fields[0].Name);
            Assert.AreEqual("qq", w.Storage.Fields[0].Value);
            Assert.AreEqual("Z", w.Storage.Fields[1].Name);
            Assert.AreEqual(true, w.Storage.Fields[1].Value);

            Assert.AreEqual(data, XS(bs.ToBytes(w)));

            bd.FromBytes(w, SX("20 01 00 00 00"));
            Assert.AreEqual(0, w.Storage.Fields.Count);

            Assert.AreEqual("20 01 00 00 00", XS(bs.ToBytes(new SampleUnknown())));

            bd.ClearClassIds();
            bs.ClearClassIds();
            bd.FromBytes(w, SX(
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 02 00 " +
                XS("A", RoughType.String, "Z", RoughType.Bool) +
                " 01 00 " + XS("tt") + " 02 00 01 00 00"));
            Assert.AreEqual(2, w.Storage.Fields.Count);
            Assert.AreEqual("A", w.Storage.Fields[0].Name);
            Assert.AreEqual("tt", w.Storage.Fields[0].Value);
            Assert.AreEqual("Z", w.Storage.Fields[1].Name);
            Assert.AreEqual(true, w.Storage.Fields[1].Value);
            Assert.AreEqual(258, w.X);

            w.X = 0;
            Assert.AreEqual(
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 03 00 " +
                XS("A", RoughType.String, "X", RoughType.Int, "Z", RoughType.Bool) +
                " 01 00 " + XS("tt") + " 03 00 01 00 00",
                XS(bs.ToBytes(w)));

            bs.ClearClassIds();
            bs.ToBytes(new SampleUnknown());
            XAssert.Throws<YuzuException>(() => bs.ToBytes(w), "SampleUnknown");

            bs.ClearClassIds();
            bd.ClearClassIds();
            var data2 =
                "20 01 00 " + XS(typeof(SampleUnknown)) + " 02 00 " +
                XS("A", RoughType.Record, "X", RoughType.Int) +
                " 01 00 02 00 " + XS("NewType") + " 01 00 " + XS("Fld", RoughType.SByte) +
                " 01 00 FE 00 00 02 00 14 00 00 00 00 00";
            var w2 = bd.FromBytes<SampleUnknown>(SX(data2));
            Assert.AreEqual(1, w2.Storage.Fields.Count);
            Assert.AreEqual("A", w2.Storage.Fields[0].Name);
            var u2 = (YuzuUnknown)w2.Storage.Fields[0].Value;
            Assert.AreEqual("NewType", u2.ClassTag);
            Assert.AreEqual(1, u2.Fields.Count);
            Assert.AreEqual((sbyte)-2, u2.Fields["Fld"]);
            Assert.AreEqual(20, w2.X);

            Assert.AreEqual("\n" + data2, "\n" + XS(bs.ToBytes(w2)));
        }