Ejemplo n.º 1
0
        internal static object DeserializeObject(object o, BinaryReader reader, NilImplication nilImplication = NilImplication.MemberDefault)
        {
            var list = o as IList;

            if (list != null)
            {
                return(MsgPackIO.DeserializeCollection(list, reader) ? null : o);
            }
            var dictionary = o as IDictionary;

            if (dictionary != null)
            {
                return(MsgPackIO.DeserializeCollection(dictionary, reader) ? null : o);
            }
            return(GetSerializer(o.GetType()).Deserialize(o, reader));
        }
Ejemplo n.º 2
0
        internal static object DeserializeValue(Type type, BinaryReader reader, NilImplication nilImplication)
        {
            object result     = null;
            bool   isRichType = false;

            if (type == typeof(string))
            {
                result = ReadMsgPackString(reader, nilImplication);
            }
            else if (type == typeof(int) || type == typeof(uint) ||
                     type == typeof(byte) || type == typeof(sbyte) ||
                     type == typeof(short) || type == typeof(ushort) ||
                     type == typeof(long) || type == typeof(ulong) ||
                     type == typeof(int?) || type == typeof(uint?) ||
                     type == typeof(byte?) || type == typeof(sbyte?) ||
                     type == typeof(short?) || type == typeof(ushort?) ||
                     type == typeof(long?) || type == typeof(ulong?))
            {
                result = ReadMsgPackInt(reader, nilImplication);
            }
            else if (type == typeof(char))
            {
                result = ReadMsgPackInt(reader, nilImplication);
            }
            else if (type == typeof(float))
            {
                result = ReadMsgPackFloat(reader, nilImplication);
            }
            else if (type == typeof(double))
            {
                result = ReadMsgPackDouble(reader, nilImplication);
            }
            else if (type == typeof(Boolean) || type == typeof(bool))
            {
                result = ReadMsgPackBoolean(reader, nilImplication);
            }
            else if (type == typeof(DateTime))
            {
                object boxedVal = ReadMsgPackInt(reader, nilImplication);
                if (boxedVal == null)
                {
                    result = null;
                }
                else
                {
                    long unixEpochTicks = (long)boxedVal;
                    result = ToDateTime(unixEpochTicks);
                }
            }
            else if (type == typeof(TimeSpan))
            {
                object boxedVal = ReadMsgPackInt(reader, nilImplication);
                if (boxedVal == null)
                {
                    result = null;
                }
                else
                {
                    int unixEpochTicks = (int)boxedVal;
                    result = ToTimeSpan(unixEpochTicks);
                }
            }
            else if (type.IsEnum)
            {
                object boxedVal = ReadMsgPackString(reader, nilImplication);
                if (boxedVal == null)
                {
                    result = null;
                }
                else
                {
                    string enumVal = (string)boxedVal;
                    if (enumVal == "")
                    {
                        result = null;
                    }
                    else
                    {
                        result = Enum.Parse(type, enumVal);
                    }
                }
            }
            else if (type.IsArray)
            {
                int numElements = MsgPackIO.ReadNumArrayElements(reader);
                if (numElements == -1)
                {
                    result = null;
                }
                else
                {
                    result = Activator.CreateInstance(type, new object[] { numElements });
                    MsgPackIO.DeserializeArray((Array)result, numElements, reader);
                }
            }
            else if (type == typeof(System.Object))
            {
                byte header = reader.ReadByte();
                if (header == MsgPackConstants.Formats.NIL)
                {
                    result = null;
                }
                else if (header == MsgPackConstants.Bool.TRUE)
                {
                    result = true;
                }
                else if (header == MsgPackConstants.Bool.FALSE)
                {
                    result = false;
                }
                else if (header == MsgPackConstants.Formats.FLOAT_64)
                {
                    result = ReadMsgPackDouble(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.FLOAT_32)
                {
                    result = ReadMsgPackFloat(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.INTEGER_16)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.INTEGER_32)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.INTEGER_64)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.INTEGER_8)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.STRING_8)
                {
                    result = ReadMsgPackString(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.STRING_16)
                {
                    result = ReadMsgPackString(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.STRING_32)
                {
                    result = ReadMsgPackString(reader, nilImplication, header);
                }
                else if (header >= MsgPackConstants.FixedString.MIN && header <= MsgPackConstants.FixedString.MAX)
                {
                    result = ReadMsgPackString(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_8)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_16)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_32)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header == MsgPackConstants.Formats.UNSIGNED_INTEGER_64)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if (header >= MsgPackConstants.FixedInteger.POSITIVE_MIN && header <= MsgPackConstants.FixedInteger.POSITIVE_MAX)
                {
                    if (header == 0)
                    {
                        result = 0;
                    }
                    else
                    {
                        result = ReadMsgPackInt(reader, nilImplication, header);
                    }
                }
                else if (header >= MsgPackConstants.FixedInteger.NEGATIVE_MIN && header <= MsgPackConstants.FixedInteger.NEGATIVE_MAX)
                {
                    result = ReadMsgPackInt(reader, nilImplication, header);
                }
                else if ((header >= MsgPackConstants.FixedMap.MIN && header <= MsgPackConstants.FixedMap.MAX) ||
                         header == MsgPackConstants.Formats.MAP_16 || header == MsgPackConstants.Formats.MAP_32)
                {
                    result = new Dictionary <string, object>();
                    MsgPackIO.DeserializeCollection((Dictionary <string, object>)result, reader, header);
                }
                else
                {
                    isRichType = true;
                }
            }
            else
            {
                isRichType = true;
            }

            if (isRichType)
            {
                ConstructorInfo constructorInfo = type.GetConstructor(Type.EmptyTypes);
                if (constructorInfo == null)
                {
                    throw new ApplicationException("Can't deserialize Type [" + type + "] because it has no default constructor");
                }
                result = constructorInfo.Invoke(SerializableProperty.EmptyObjArgs);
                result = MsgPackSerializer.DeserializeObject(result, reader, nilImplication);
            }
            return(result);
        }