Beispiel #1
0
        static void Main3()
        {
            var ctx = MPConverterContext.CreateDefault();

            (Type type, object value) Make <T>(T val) => (typeof(T), val);

            var vals = new[] {
                // Make(new[] { 1, 2, 3, 4, }),
                // Make(new object[] { 5, 6, 7, 8, }),
                // Make(new List<int> { 3, 4, 5, 6, }),
                // Make(new ArrayList { 1, 2, 7, 8, }),
                // Make(new Collection<object> { 'a', 'b', 'c', }),
                // Make(new Collection<object> { 'a', 1, false, }),

                Make(new Dictionary <string, string> {
                    ["a"] = "A",
                    ["b"] = "B",
                    ["c"] = "C",
                }),
                Make(new Hashtable {
                    ["x"] = "X",
                    ["y"] = "Y",
                    ["z"] = "Z",
                }),
                Make(new Dictionary <string, object> {
                    ["p1"] = 1,
                    ["p2"] = true,
                    ["p3"] = "ABC",
                    ["p4"] = Encoding.UTF8.GetBytes("ABC"),
                    ["p5"] = new[] { 1, 2, 3, 4, },
                }),

                Make(new Sample1 {
                }),
            };

            foreach (var tv in vals)
            {
                Console.WriteLine($"[{tv.type.Name,-15}]");
                Console.WriteLine($"    Original [{JsonConvert.SerializeObject(tv.value),45}]:");
                var encoded = ctx.Encode(tv.type, tv.value);
                Console.WriteLine($"    Encoded: [{JsonConvert.SerializeObject(encoded),45}]");
                var decoded = ctx.Decode(tv.type, encoded);
                Console.WriteLine($"    Decoded: [{JsonConvert.SerializeObject(decoded),45}]");
                Console.WriteLine($"    Rebuilt: [{JsonConvert.SerializeObject(tv.value),45}]:");
            }
        }
Beispiel #2
0
        static void Main4()
        {
            var ctx = MPConverterContext.CreateDefault(
                new [] { new Converters.ObjectConverter(new MyNameResolver()) }, true);

            // ctx = new MPConverterContext
            // {
            //     Converters = {
            //         Converters.BasicConverter.Instance,
            //         new Converters.CommonConverter(true, true),
            //         Converters.MapConverter.Instance,
            //         Converters.ArrayConverter.Instance,
            //         new Converters.ObjectConverter(new MyNameResolver()),
            //         Converters.DefaultConverter.Instance,
            //     },
            // };


            (Type type, object value) Make <T>(T val) => (typeof(T), val);

            var vals = new[] {
                Make(new Sample1 {
                }),
            };

            foreach (var tv in vals)
            {
                Console.WriteLine($"[{tv.type.Name,-15}]");
                Console.WriteLine($"    Original [{JsonConvert.SerializeObject(tv.value),45}]:");
                var encoded = ctx.Encode(tv.type, tv.value);
                Console.WriteLine($"    Encoded: [{JsonConvert.SerializeObject(encoded),45}]");
                var decoded = ctx.Decode(tv.type, encoded);
                Console.WriteLine($"    Decoded: [{JsonConvert.SerializeObject(decoded),45}]");
                Console.WriteLine($"    Rebuilt: [{JsonConvert.SerializeObject(tv.value),45}]:");

                Console.WriteLine();
                Console.WriteLine(JsonConvert.SerializeObject(new {
                    type = tv.type.Name,
                    orig = tv.value,
                    enco = encoded,
                    deco = decoded,
                }));
            }
        }
Beispiel #3
0
        static void Main5()
        {
            var vals = new object[] {
                -99, 99,
                sbyte.MinValue, sbyte.MaxValue,
                byte.MinValue, byte.MaxValue,

                short.MinValue, short.MaxValue,
                ushort.MinValue, ushort.MaxValue,

                int.MinValue, int.MaxValue,
                uint.MinValue, uint.MaxValue,

                long.MinValue, long.MaxValue,
                ulong.MinValue, (ulong.MaxValue / 2), // ulong.MaxValue,

                float.MinValue, float.MaxValue,
                double.MinValue, double.MaxValue,

                Encoding.UTF8.GetBytes("Hello World!"),
                "Goodbye World!",

                new[] {
                    1, 2, 3, 4,
                },
                new List <string> {
                    "FOO", "BAR", "NON",
                },
                new ArrayList {
                    1, true, "FOO", Encoding.UTF8.GetBytes("BAR"),
                },

                new MPExt(10, Encoding.UTF8.GetBytes("H")),
                new MPExt(10, Encoding.UTF8.GetBytes("HELO")),
                new MPExt(10, Encoding.UTF8.GetBytes("HELOWRLD")),
                new MPExt(10, Encoding.UTF8.GetBytes("G'DAYTOTHEWORLD!")),
                new MPExt(10, Encoding.UTF8.GetBytes("G'DAYTOTHEWORLD!")),

                new MPExt(10, Encoding.UTF8.GetBytes("HE")),
                new MPExt(10, Encoding.UTF8.GetBytes("HE LO")),
            };

            int ndx = 0;

            byte[] msBytes;
            using (var ms = new MemoryStream())
            {
                var ctx = MPConverterContext.CreateDefault();
                var mpw = new MPWriter(ms);
                foreach (var v in vals)
                {
                    Console.WriteLine($"[{ndx++,2}] Writing [{v}]:");
                    var mpo = ctx.Encode(v.GetType(), v);
                    mpw.Emit(mpo);
                }

                msBytes = ms.ToArray();
            }

            Console.WriteLine();
            Console.WriteLine($"Wrote [{msBytes.Length}] bytes");
            Console.WriteLine();

            ndx = 0;
            using (var ms = new MemoryStream(msBytes))
            {
                var mpo = MPReader.Parse(ms);
                while (mpo != null)
                {
                    Console.WriteLine($"[{ndx++,2}] Read [{mpo.Value.Type}=[{mpo.Value.Value}]");
                    if (mpo.Value.Type == MPType.Ext)
                    {
                        var ext = (MPExt)mpo.Value.Value;
                        Console.WriteLine($"  EXT [{ext.Type}]: [{Encoding.UTF8.GetString(ext.Data.ToArray())}]");
                    }

                    mpo = MPReader.Parse(ms);
                }
            }
        }
Beispiel #4
0
        static void Main2()
        {
            var ctx = MPConverterContext.CreateDefault();

            (Type type, object value) Make <T>(T val) => (typeof(T), val);

            var vals = new[] {
                // Boolean
                Make(false),
                Make(true),
                Make((bool?)null),
                Make((bool?)true),
                Make((bool?)false),

                // Integer
                Make(byte.MinValue), Make((byte?)byte.MinValue),
                Make(byte.MaxValue), Make((byte?)byte.MaxValue),
                Make(sbyte.MinValue), Make((sbyte?)sbyte.MinValue),
                Make(sbyte.MaxValue), Make((sbyte?)sbyte.MaxValue),
                Make(short.MinValue), Make((short?)short.MinValue),
                Make(short.MaxValue), Make((short?)short.MaxValue),
                Make(ushort.MinValue), Make((ushort?)ushort.MinValue),
                Make(ushort.MaxValue), Make((ushort?)ushort.MaxValue),
                Make(int.MinValue), Make((int?)int.MinValue),
                Make(int.MaxValue), Make((int?)int.MaxValue),
                Make(uint.MinValue), Make((uint?)uint.MinValue),
                Make(uint.MaxValue), Make((uint?)uint.MaxValue),
                Make(long.MinValue), Make((long?)long.MinValue),
                Make(long.MaxValue), Make((long?)long.MaxValue),
                Make(ulong.MinValue), Make((ulong?)ulong.MinValue),
                Make(ulong.MaxValue), Make((ulong?)ulong.MaxValue),
                Make(char.MinValue), Make((char?)char.MinValue),
                Make(char.MaxValue), Make((char?)char.MaxValue),

                // Float
                Make(float.MinValue), Make((float?)float.MinValue),
                Make(float.MaxValue), Make((float?)float.MaxValue),
                Make(double.MinValue), Make((double?)double.MinValue),
                Make(double.MaxValue), Make((double?)double.MaxValue),
                Make(decimal.MinValue), Make((decimal?)decimal.MinValue),
                Make(decimal.MaxValue), Make((decimal?)decimal.MaxValue),

                // Binary
                Make(Encoding.UTF8.GetBytes("ABCDEFG")),

                // String
                Make("abcdefg"),
            };

            foreach (var tv in vals)
            {
                Console.WriteLine($"[{tv.type.Name,-15}]");
                Console.WriteLine($"  Original [{tv.value,45}]:");
                var encoded = ctx.Encode(tv.type, tv.value);
                Console.WriteLine($"  Encoded: [{encoded,45}]");
                var decoded = ctx.Decode(tv.type, encoded);
                Console.WriteLine($"  Decoded: [{decoded,45}]");

                if (!object.Equals(tv.value, decoded))
                {
                    throw new Exception("encoded->decoded does not match original value");
                }
            }
        }