예제 #1
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpArray4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpArray16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpArray32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpArray does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value = new object[len];
            for (int t = 0; t < len; t++)
            {
                MsgPackItem item = Unpack(data);
                value[t] = item.Value;
            }

            return(this);
        }
예제 #2
0
        public override MsgPackItem Read(MsgPackTypeId typeId, Stream data)
        {
            long len;

            if (!IsMasked(MsgPackTypeId.MpMap4, typeId, 0x0F, out len))
            {
                switch (typeId)
                {
                case MsgPackTypeId.MpMap16: len = ReadLen(data, 2); break;

                case MsgPackTypeId.MpMap32: len = ReadLen(data, 4); break;

                default: throw new MsgPackException(string.Concat("MpMap does not support a type ID of ", GetOfficialTypeName(typeId), "."), data.Position - 1, typeId);
                }
            }

            value = new KeyValuePair[len];

            for (int t = 0; t < len; t++)
            {
                MsgPackItem key = MsgPackItem.Unpack(data);
                MsgPackItem val = MsgPackItem.Unpack(data);
                value[t] = new KeyValuePair(key.Value, val.Value);
            }

            return(this);
        }
예제 #3
0
        public static void Serialize(object item, Stream target)
        {
            MsgPackItem packed = SerializeObject(item);

            byte[] buffer = packed.ToBytes();
            target.Write(buffer, 0, buffer.Length);
            return;
        }
예제 #4
0
        public static object Deserialize(Type tType, Stream stream)
        {
            if (MsgPackSerializer.NativelySupportedTypes.Contains(tType))
            {
                return(MsgPackItem.Unpack(stream).Value);
            }
            MpMap  map    = (MpMap)MsgPackItem.Unpack(stream);
            object result = Materialize(tType, map);

            return(result);
        }
예제 #5
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());
        }
예제 #6
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)));
        }
예제 #7
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
            });
        }
예제 #8
0
        /// <returns>
        /// Have to reorder stuff here since NetMf 4.2 returns true for the "is" statement for stuff like "int is int[]" and so on
        /// </returns>
        public static MsgPackItem Pack(object value)
        {
            if (ReferenceEquals(value, null))
            {
                return(new MpNull());
            }

            Type valuesType = value.GetType();

            if (valuesType.IsArray)
            {
                if (value is byte[])
                {
                    return new MpBin()
                           {
                               Value = value
                           }
                }
                ;
                if (value is KeyValuePair[])
                {
                    return new MpMap()
                           {
                               Value = value
                           }
                }
                ;
                if (value is object[])
                {
                    return new MpArray()
                           {
                               Value = value
                           }
                }
                ;
                return(new MpArray()
                {
                    Value = ToObjects((IEnumerable)value)
                });
            }

            if (value is sbyte ||
                value is short ||
                value is int ||
                value is long ||
                value is byte ||
                value is ushort ||
                value is uint ||
                value is ulong)
            {
                return new MpInt()
                       {
                           Value = value
                       }
            }
            ;
            if (value is float ||
                value is double)
            {
                return new MpFloat()
                       {
                           Value = value
                       }
            }
            ;
            if (value is string)
            {
                return new MpString()
                       {
                           Value = value
                       }
            }
            ;
            if (value is bool)
            {
                return new MpBool()
                       {
                           Value = value
                       }
            }
            ;
            if (value is KeyValuePair)
            {
                return new MpMap()
                       {
                           Value = value
                       }
            }
            ;                                                          // object[0] returns true for this one in NetMf 4.2

            if (valuesType.IsEnum)
            {
                return(new MpInt().SetEnumVal(value));
            }
            if (valuesType.IsInstanceOfType(typeof(IEnumerable)))
            {
                return new MpArray()
                       {
                           Value = ToObjects((IEnumerable)value)
                       }
            }
            ;

            // Extension types will come in like this most of the time:
            MsgPackItem val = value as MsgPackItem;

            if (!ReferenceEquals(val, null))
            {
                return(val);
            }

            return(MsgPackSerializer.SerializeObject(value));
        }
예제 #9
0
        public static MsgPackItem Unpack(Stream stream)
        {
            int typeByte = stream.ReadByte();

            if (typeByte < 0)
            {
                throw new MsgPackException("Unexpected end of data.", stream.Position);
            }
            MsgPackItem item = null;

            try {
                MsgPackTypeId type = (MsgPackTypeId)typeByte;
                switch (type)
                {
                case MsgPackTypeId.MpNull: item = new MpNull(); break;

                case MsgPackTypeId.MpBoolFalse:
                case MsgPackTypeId.MpBoolTrue: item = new MpBool(); break;

                //case MsgPackTypes.MpBytePart:
                //case MsgPackTypes.MpSBytePart:
                case MsgPackTypeId.MpSByte:
                case MsgPackTypeId.MpShort:
                case MsgPackTypeId.MpInt:
                case MsgPackTypeId.MpLong:
                case MsgPackTypeId.MpUByte:
                case MsgPackTypeId.MpUShort:
                case MsgPackTypeId.MpUInt:
                case MsgPackTypeId.MpULong: item = new MpInt(); break;

                case MsgPackTypeId.MpFloat:
                case MsgPackTypeId.MpDouble: item = new MpFloat(); break;

                //case MsgPackTypeId.MpStr5:
                case MsgPackTypeId.MpStr8:
                case MsgPackTypeId.MpStr16:
                case MsgPackTypeId.MpStr32: item = new MpString(); break;

                case MsgPackTypeId.MpBin8:
                case MsgPackTypeId.MpBin16:
                case MsgPackTypeId.MpBin32: item = new MpBin(); break;

                //case MsgPackTypeId.MpArray4:
                case MsgPackTypeId.MpArray16:
                case MsgPackTypeId.MpArray32: item = new MpArray(); break;

                //case MsgPackTypeId.MpMap4:
                case MsgPackTypeId.MpMap16:
                case MsgPackTypeId.MpMap32: item = new MpMap(); break;

                case MsgPackTypeId.MpFExt1:
                case MsgPackTypeId.MpFExt2:
                case MsgPackTypeId.MpFExt4:
                case MsgPackTypeId.MpFExt8:
                case MsgPackTypeId.MpFExt16:
                case MsgPackTypeId.MpExt8:
                case MsgPackTypeId.MpExt16:
                case MsgPackTypeId.MpExt32: item = new MpExt(); break;

                case MsgPackTypeId.NeverUsed: throw new MsgPackException("The specification specifically states that the value 0xC1 should never be used.",
                                                                         stream.Position - 1, MsgPackTypeId.NeverUsed);
                }

                if (ReferenceEquals(item, null))
                {
                    if (((byte)type & 0xE0) == 0xE0 || (((byte)type & 0x80) == 0))
                    {
                        item = new MpInt();
                    }
                    else if (((byte)type & 0xA0) == 0xA0)
                    {
                        item = new MpString();
                    }
                    else if (((byte)type & 0x90) == 0x90)
                    {
                        item = new MpArray();
                    }
                    else if (((byte)type & 0x80) == 0x80)
                    {
                        item = new MpMap();
                    }
                }

                if (!ReferenceEquals(item, null))
                {
                    return(item.Read(type, stream));
                }
                else
                {
                    throw new MsgPackException(string.Concat("The type identifier with value 0x", ((int)type).ToString("X"),
                                                             " is either new or invalid. It is not (yet) implemented in this version of LsMsgPack."), stream.Position, type);
                }
            }catch (Exception ex) {
                if (!(ex is MsgPackException))
                {
                    MsgPackException mpex = new MsgPackException("Error while reading data.", ex, stream.Position, (MsgPackTypeId)typeByte);
                    throw mpex;
                }
                else
                {
                    throw;
                }
            }
        }