public static DynamicValue MarshalViaMPSharp(Type t, object value)
        {
            using (var ms = new MemoryStream())
            {
                var mpo = ConverterContext.Encode(t, value);
                var mpw = new MPWriter(ms);
                _log.LogDebug($"emitting [{t.FullName}] as MsgPack");
                mpw.Emit(mpo);

                return(new DynamicValue
                {
                    Msgpack = ByteString.CopyFrom(ms.ToArray()),
                });
            }
        }
示例#2
0
        public static DynamicValue MarshalViaMPSharp(Type t, object value, bool withUnknowns)
        {
            using (var ms = new MemoryStream())
            {
                var mpo = withUnknowns
                    ? WithUnknownConverterContext.Encode(t, value)
                    : WithNullsConverterContext.Encode(t, value);

                _log.LogTrace("Marshalled to MPObject:");
                Visit(mpo, (l, s) => _log.LogTrace("MPVisitor: {0}", s));

                var mpw = new MPWriter(ms);
                _log.LogDebug($"emitting [{t.FullName}] as MsgPack");
                mpw.Emit(mpo);

                return(new DynamicValue
                {
                    Msgpack = ByteString.CopyFrom(ms.ToArray()),
                });
            }
        }
示例#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);
                }
            }
        }