コード例 #1
0
        public static MsgPackItem SerializeObject(object item)
        {
            if (ReferenceEquals(item, null))
            {
                return(new MpNull());
            }
            Type tType = item.GetType();

            if (MsgPackSerializer.NativelySupportedTypes.Contains(tType))
            {
                return(MsgPackItem.Pack(item));
                // Maybe we should rather throw an exception here
            }
            PropertyInfo[] props = GetSerializedProps(tType);
            Dictionary <string, object> propVals = new Dictionary <string, object>(props.Length);

            for (int t = props.Length - 1; t >= 0; t--)
            {
                propVals.Add(props[t].Name, props[t].GetValue(item, null));
            }
            return(new MpMap()
            {
                Value = propVals
            });
        }
コード例 #2
0
        public static byte[] Serialize <T>(T item)
        {
            if (MsgPackSerializer.NativelySupportedTypes.Contains(typeof(T)))
            {
                return(MsgPackItem.Pack(item).ToBytes());
            }
            MemoryStream ms = new MemoryStream();

            Serialize(item, ms);
            return(ms.ToArray());
        }
コード例 #3
0
        public override byte[] ToBytes()
        {
            List <byte>   bytes  = new List <byte>();// cannot estimate this one
            MsgPackTypeId typeId = GetTypeId(value.LongLength);

            if (typeId == MsgPackTypeId.MpArray4)
            {
                bytes.Add(GetLengthBytes(typeId, value.Length));
            }
            else
            {
                bytes.Add((byte)typeId);
                bytes.AddRange(GetLengthBytes(value.LongLength, SupportedLengths.FromShortUpward));
            }
            for (int t = 0; t < value.Length; t++)
            {
                MsgPackItem item = MsgPackItem.Pack(value[t], _settings);
                bytes.AddRange(item.ToBytes());
            }
            return(bytes.ToArray());
        }
コード例 #4
0
        public override long CalcBytesSize()
        {
            long retVal;

            MsgPackTypeId typeId = GetTypeId(value.LongLength);

            if (typeId == MsgPackTypeId.MpArray4)
            {
                retVal = 1;
            }
            else
            {
                retVal = 1 + GetLengthBytesSize(value.LongLength, SupportedLengths.FromShortUpward);
            }
            for (int t = 0; t < value.Length; t++)
            {
                MsgPackItem item = MsgPackItem.Pack(value[t]);
                retVal += item.CalcBytesSize();
            }
            return(retVal);
        }
コード例 #5
0
        public override void ToStream(Stream stream)
        {
            MsgPackTypeId typeId = GetTypeId(value.LongLength);

            if (typeId == MsgPackTypeId.MpArray4)
            {
                stream.WriteByte(GetLengthBytes(typeId, value.Length));
            }
            else
            {
                stream.WriteByte((byte)typeId);

                var lenBytes = GetLengthBytes(value.LongLength, SupportedLengths.FromShortUpward);
                stream.Write(lenBytes, 0, lenBytes.Length);
            }

            for (int t = 0; t < value.Length; t++)
            {
                MsgPackItem item = MsgPackItem.Pack(value[t]);
                item.ToStream(stream);
            }
        }