Пример #1
0
        private static MsgPackSerializer GetSerializer(Type t)
        {
            MsgPackSerializer result = null;

            lock (DefaultContext.Serializers)
            {
                if (!DefaultContext.Serializers.TryGetValue(t, out result))
                {
                    result = DefaultContext.Serializers[t] = new MsgPackSerializer(t);
                }
            }
            return(result);
        }
Пример #2
0
        public void RegisterSerializer <T>(params string[] propertyNames)
        {
            var defs = new List <MessagePackMemberDefinition>();

            foreach (var propertyName in propertyNames)
            {
                defs.Add(new MessagePackMemberDefinition()
                {
                    PropertyName   = propertyName,
                    NilImplication = NilImplication.MemberDefault
                });
            }
            Serializers[typeof(T)] = new MsgPackSerializer(typeof(T), defs);
        }
Пример #3
0
 internal static void SerializeValue(object val, BinaryWriter writer, SerializationMethod serializationMethod)
 {
     if (val == null)
     {
         writer.Write(MsgPackConstants.Formats.NIL);
     }
     else
     {
         Type t = val.GetType();
         t = Nullable.GetUnderlyingType(t) ?? t;
         if (t == typeof(string))
         {
             WriteMsgPack(writer, (string)val);
         }
         else if (t == typeof(char) || t == typeof(System.Char))
         {
             WriteMsgPack(writer, (char)val);
         }
         else if (t == typeof(float) || t == typeof(Single))
         {
             WriteMsgPack(writer, (float)val);
         }
         else if (t == typeof(double) || t == typeof(Double))
         {
             WriteMsgPack(writer, (double)val);
         }
         else if (t == typeof(byte) || t == typeof(Byte))
         {
             WriteMsgPack(writer, (byte)val);
         }
         else if (t == typeof(sbyte) || t == typeof(SByte))
         {
             WriteMsgPack(writer, (sbyte)val);
         }
         else if (t == typeof(short) || t == (typeof(Int16)))
         {
             WriteMsgPack(writer, (short)val);
         }
         else if (t == typeof(ushort) || t == (typeof(UInt16)))
         {
             WriteMsgPack(writer, (ushort)val);
         }
         else if (t == typeof(int) || t == (typeof(Int32)))
         {
             WriteMsgPack(writer, (int)val);
         }
         else if (t == typeof(uint) || t == (typeof(UInt32)))
         {
             WriteMsgPack(writer, (uint)val);
         }
         else if (t == typeof(long) || t == (typeof(Int64)))
         {
             WriteMsgPack(writer, (long)val);
         }
         else if (t == typeof(ulong) || t == (typeof(UInt64)))
         {
             WriteMsgPack(writer, (ulong)val);
         }
         else if (t == typeof(bool) || t == (typeof(Boolean)))
         {
             WriteMsgPack(writer, (bool)val);
         }
         else if (t == typeof(DateTime))
         {
             WriteMsgPack(writer, (DateTime)val);
         }
         else if (t == typeof(TimeSpan))
         {
             WriteMsgPack(writer, (TimeSpan)val);
         }
         else if (t == typeof(decimal))
         {
             throw new ApplicationException("The Decimal Type isn't supported");
         }
         else if (t.IsEnum)
         {
             WriteMsgPack(writer, Enum.GetName(t, val));
         }
         else if (t.IsArray)
         {
             Array array = val as Array;
             if (array == null)
             {
                 writer.Write((byte)MsgPackConstants.Formats.NIL);
             }
             else
             {
                 if (array.Length <= 15)
                 {
                     byte arrayVal = (byte)(MsgPackConstants.FixedArray.MIN + array.Length);
                     writer.Write(arrayVal);
                 }
                 else if (array.Length <= UInt16.MaxValue)
                 {
                     writer.Write((byte)MsgPackConstants.Formats.ARRAY_16);
                     byte[] data = BitConverter.GetBytes((ushort)array.Length);
                     if (BitConverter.IsLittleEndian)
                     {
                         Array.Reverse(data);
                     }
                     writer.Write(data);
                 }
                 else
                 {
                     writer.Write((byte)MsgPackConstants.Formats.ARRAY_32);
                     byte[] data = BitConverter.GetBytes((uint)array.Length);
                     if (BitConverter.IsLittleEndian)
                     {
                         Array.Reverse(data);
                     }
                     writer.Write(data);
                 }
                 SerializeEnumerable(array.GetEnumerator(), writer, serializationMethod);
             }
         }
         else if (MsgPackSerializer.IsGenericList(t))
         {
             IList list = val as IList;
             if (list.Count <= 15)
             {
                 byte arrayVal = (byte)(MsgPackConstants.FixedArray.MIN + list.Count);
                 writer.Write(arrayVal);
             }
             else if (list.Count <= UInt16.MaxValue)
             {
                 writer.Write((byte)MsgPackConstants.Formats.ARRAY_16);
                 byte[] data = BitConverter.GetBytes((ushort)list.Count);
                 if (BitConverter.IsLittleEndian)
                 {
                     Array.Reverse(data);
                 }
                 writer.Write(data);
             }
             else
             {
                 writer.Write((byte)MsgPackConstants.Formats.ARRAY_32);
                 byte[] data = BitConverter.GetBytes((uint)list.Count);
                 if (BitConverter.IsLittleEndian)
                 {
                     Array.Reverse(data);
                 }
                 writer.Write(data);
             }
             SerializeEnumerable(list.GetEnumerator(), writer, serializationMethod);
         }
         else if (MsgPackSerializer.IsGenericDictionary(t))
         {
             IDictionary dictionary = val as IDictionary;
             if (dictionary.Count <= 15)
             {
                 byte header = (byte)(MsgPackConstants.FixedMap.MIN + dictionary.Count);
                 writer.Write(header);
             }
             else if (dictionary.Count <= UInt16.MaxValue)
             {
                 writer.Write((byte)MsgPackConstants.Formats.MAP_16);
                 byte[] data = BitConverter.GetBytes((ushort)dictionary.Count);
                 if (BitConverter.IsLittleEndian)
                 {
                     Array.Reverse(data);
                 }
                 writer.Write(data);
             }
             else
             {
                 writer.Write((byte)MsgPackConstants.Formats.MAP_32);
                 byte[] data = BitConverter.GetBytes((uint)dictionary.Count);
                 if (BitConverter.IsLittleEndian)
                 {
                     Array.Reverse(data);
                 }
                 writer.Write(data);
             }
             IDictionaryEnumerator enumerator = dictionary.GetEnumerator();
             while (enumerator.MoveNext())
             {
                 SerializeValue(enumerator.Key, writer, serializationMethod);
                 SerializeValue(enumerator.Value, writer, serializationMethod);
             }
         }
         else
         {
             MsgPackSerializer.SerializeObject(val, writer);
         }
     }
 }
Пример #4
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);
        }
Пример #5
0
 public void RegisterSerializer <T>(IList <MessagePackMemberDefinition> propertyDefinitions)
 {
     Serializers[typeof(T)] = new MsgPackSerializer(typeof(T), propertyDefinitions);
 }