Пример #1
0
        public void TestDictionaryKeys()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.JsonOptions.FieldSeparator = "";

            var v0 = new SampleDictKeys {
                I =  new Dictionary<int, int> { { 5, 7 } },
                E =  new Dictionary<SampleEnum, int> { { SampleEnum.E2, 8 } },
                K =  new Dictionary<SampleKey, int> { { new SampleKey { V = 3 }, 9 } },
            };
            var result0 = js.ToString(v0);
            Assert.AreEqual(
                "{" +
                "\"E\":{\"E2\":8}," +
                "\"I\":{\"5\":7}," +
                "\"K\":{\"3!\":9}" +
                "}", result0);

            JsonDeserializer.RegisterKeyParser(
                typeof(SampleKey),
                s => new SampleKey { V = int.Parse(s.Substring(0, s.Length - 1)) });

            var jd = new JsonDeserializer();
            var w = new SampleDictKeys();
            jd.FromString(w, result0);
            Assert.AreEqual(1, w.I.Count);
            Assert.AreEqual(7, w.I[5]);
            Assert.AreEqual(1, w.E.Count);
            Assert.AreEqual(8, w.E[SampleEnum.E2]);
            Assert.AreEqual(1, w.K.Count);
            Assert.AreEqual(9, w.K[new SampleKey { V = 3 }]);

            w = (SampleDictKeys)SampleDictKeys_JsonDeserializer.Instance.FromString(result0);
            Assert.AreEqual(1, w.I.Count);
            Assert.AreEqual(7, w.I[5]);
            Assert.AreEqual(1, w.E.Count);
            Assert.AreEqual(8, w.E[SampleEnum.E2]);
            Assert.AreEqual(1, w.K.Count);
            Assert.AreEqual(9, w.K[new SampleKey { V = 3 }]);
        }
Пример #2
0
        public void TestDictionaryKeys()
        {
            var bs = new BinarySerializer();

            var v0 = new SampleDictKeys {
                I = new Dictionary<int, int> { { 5, 7 } },
                E = new Dictionary<SampleEnum, int> { { SampleEnum.E2, 8 } },
                K = new Dictionary<SampleKey, int> { { new SampleKey { V = 3 }, 9 } },
            };
            var result0 = bs.ToBytes(v0);
            Assert.AreEqual(
                "20 01 00 " + XS(typeof(SampleDictKeys)) + " 03 00 " +
                XS("E", RoughType.Mapping) + " 05 05 " +
                XS("I", RoughType.Mapping) + " 05 05 " +
                XS("K", RoughType.Mapping) + " 20 05" +
                " 01 00 01 00 00 00 01 00 00 00 08 00 00 00 " +
                "02 00 01 00 00 00 05 00 00 00 07 00 00 00 " +
                "03 00 01 00 00 00 02 00 " + XS(typeof(SampleKey)) +
                " 01 00 " + XS("V", RoughType.Int) +
                " 01 00 03 00 00 00 00 00 09 00 00 00 00 00", XS(result0));

            CheckDeserializers(bd => {
                var w = new SampleDictKeys();
                bd.FromBytes(w, result0);
                Assert.AreEqual(1, w.I.Count);
                Assert.AreEqual(7, w.I[5]);
                Assert.AreEqual(1, w.E.Count);
                Assert.AreEqual(8, w.E[SampleEnum.E2]);
                Assert.AreEqual(1, w.K.Count);
                Assert.AreEqual(9, w.K[new SampleKey { V = 3 }]);
            });
        }