Beispiel #1
0
        public static byte[] Serialize(object item)
        {
            if (MsgPackSerializer.NativelySupportedTypes.Contains(item.GetType()))
            {
                return(MsgPackItem.Pack(item).ToBytes());
            }
            MemoryStream ms = new MemoryStream();

            Serialize(item, ms);
            return(ms.ToArray());
        }
Beispiel #2
0
        public override byte[] ToBytes()
        {
            ArrayList     bytes  = new ArrayList();// cannot estimate this one
            MsgPackTypeId typeId = GetTypeId(value.Length);

            if (typeId == MsgPackTypeId.MpArray4)
            {
                bytes.Add(GetLengthBytes(typeId, value.Length));
            }
            else
            {
                bytes.Add((byte)typeId);
                bytes.AddRange(GetLengthBytes(value.Length, SupportedLengths.FromShortUpward));
            }
            for (int t = 0; t < value.Length; t++)
            {
                MsgPackItem item = MsgPackItem.Pack(value[t]);
                bytes.AddRange(item.ToBytes());
            }
            return((byte[])bytes.ToArray(typeof(byte)));
        }
Beispiel #3
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);
            KeyValuePair[] propVals = new KeyValuePair[props.Length];
            for (int t = props.Length - 1; t >= 0; t--)
            {
                propVals[t] = new KeyValuePair(props[t].Name, props[t].GetValue(item, null));
            }
            return(new MpMap()
            {
                Value = propVals
            });
        }