internal void Deserialize(object o, BinaryReader reader)
        {
            object val       = MsgPackIO.DeserializeValue(ValueType, reader, _nilImplication);
            object safeValue = (val == null) ? null : Convert.ChangeType(val, ValueType);

            PropInfo.SetValue(o, safeValue, EmptyObjArgs);
        }
Example #2
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));
        }
Example #3
0
        internal static object DeserializeObjectType(Type type, BinaryReader reader, NilImplication nilImplication = NilImplication.MemberDefault)
        {
            if (type.IsPrimitive ||
                type == typeof(string) ||
                IsSerializableGenericCollection(type))
            {
                return(MsgPackIO.DeserializeValue(type, reader, nilImplication));
            }
            ConstructorInfo constructorInfo = type.GetConstructor(Type.EmptyTypes);

            if (constructorInfo == null)
            {
                throw new ApplicationException("Can't deserialize Type [" + type + "] in MsgPackSerializer because it has no default constructor");
            }
            object result = constructorInfo.Invoke(SerializableProperty.EmptyObjArgs);

            return(GetSerializer(type).Deserialize(result, reader));
        }
 internal void Serialize(object o, BinaryWriter writer, SerializationMethod serializationMethod)
 {
     MsgPackIO.SerializeValue(PropInfo.GetValue(o, EmptyObjArgs), writer, serializationMethod);
 }
Example #5
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);
        }
Example #6
0
 private void Serialize(object o, BinaryWriter writer)
 {
     if (o == null)
     {
         writer.Write((byte)MsgPackConstants.Formats.NIL);
     }
     else
     {
         if (serializedType.IsPrimitive ||
             serializedType == typeof(string) ||
             IsSerializableGenericCollection(serializedType))
         {
             MsgPackIO.SerializeValue(o, writer, DefaultContext.SerializationMethod);
         }
         else
         {
             if (DefaultContext.SerializationMethod == SerializationMethod.Map)
             {
                 if (props.Count <= 15)
                 {
                     byte arrayVal = (byte)(MsgPackConstants.FixedMap.MIN + props.Count);
                     writer.Write(arrayVal);
                 }
                 else if (props.Count <= UInt16.MaxValue)
                 {
                     writer.Write((byte)MsgPackConstants.Formats.MAP_16);
                     byte[] data = BitConverter.GetBytes((ushort)props.Count);
                     if (BitConverter.IsLittleEndian)
                     {
                         Array.Reverse(data);
                     }
                     writer.Write(data);
                 }
                 else
                 {
                     writer.Write((byte)MsgPackConstants.Formats.MAP_32);
                     byte[] data = BitConverter.GetBytes((uint)props.Count);
                     if (BitConverter.IsLittleEndian)
                     {
                         Array.Reverse(data);
                     }
                     writer.Write(data);
                 }
                 foreach (SerializableProperty prop in props)
                 {
                     MsgPackIO.WriteMsgPack(writer, prop.Name);
                     prop.Serialize(o, writer, DefaultContext.SerializationMethod);
                 }
             }
             else
             {
                 if (props.Count <= 15)
                 {
                     byte arrayVal = (byte)(MsgPackConstants.FixedArray.MIN + props.Count);
                     writer.Write(arrayVal);
                 }
                 else if (props.Count <= UInt16.MaxValue)
                 {
                     writer.Write((byte)MsgPackConstants.Formats.ARRAY_16);
                     byte[] data = BitConverter.GetBytes((ushort)props.Count);
                     if (BitConverter.IsLittleEndian)
                     {
                         Array.Reverse(data);
                     }
                     writer.Write(data);
                 }
                 else
                 {
                     writer.Write((byte)MsgPackConstants.Formats.ARRAY_32);
                     byte[] data = BitConverter.GetBytes((uint)props.Count);
                     if (BitConverter.IsLittleEndian)
                     {
                         Array.Reverse(data);
                     }
                     writer.Write(data);
                 }
                 foreach (SerializableProperty prop in props)
                 {
                     prop.Serialize(o, writer, DefaultContext.SerializationMethod);
                 }
             }
         }
     }
 }
Example #7
0
        internal object Deserialize(object result, BinaryReader reader)
        {
            byte header = reader.ReadByte();

            if (header == MsgPackConstants.Formats.NIL)
            {
                result = null;
            }
            else
            {
                if (DefaultContext.SerializationMethod == SerializationMethod.Array)
                {
                    if (header == MsgPackConstants.Formats.ARRAY_16)
                    {
                        reader.ReadByte();
                        reader.ReadByte();
                    }
                    else if (header == MsgPackConstants.Formats.ARRAY_32)
                    {
                        reader.ReadByte();
                        reader.ReadByte();
                        reader.ReadByte();
                        reader.ReadByte();
                    }
                    else if (header < MsgPackConstants.FixedArray.MIN || header > MsgPackConstants.FixedArray.MAX)
                    {
                        throw new ApplicationException("The serialized array format isn't valid for header [" + header + "]");
                    }

                    foreach (SerializableProperty prop in props)
                    {
                        prop.Deserialize(result, reader);
                    }
                }
                else
                {
                    int numElements;
                    if (header >= MsgPackConstants.FixedMap.MIN && header <= MsgPackConstants.FixedMap.MAX)
                    {
                        numElements = header & 0x0F;
                    }
                    else if (header == MsgPackConstants.Formats.MAP_16)
                    {
                        numElements = (reader.ReadByte() << 8) +
                                      reader.ReadByte();
                    }
                    else if (header == MsgPackConstants.Formats.MAP_32)
                    {
                        numElements = (reader.ReadByte() << 24) +
                                      (reader.ReadByte() << 16) +
                                      (reader.ReadByte() << 8) +
                                      reader.ReadByte();
                    }
                    else
                    {
                        throw new ApplicationException("The serialized map format isn't valid");
                    }

                    for (int i = 0; i < numElements; i++)
                    {
                        string propName = (string)MsgPackIO.ReadMsgPackString(reader, NilImplication.Null);
                        SerializableProperty propToProcess = null;
                        if (propsByName.TryGetValue(propName, out propToProcess))
                        {
                            propToProcess.Deserialize(result, reader);
                        }
                    }
                }
            }
            return(result);
        }