예제 #1
0
        public static MsgPackItem Pack(object value, MsgPackSettings settings)
        {
            if (ReferenceEquals(value, null))
            {
                return(new MpNull(settings));
            }
            if (value is bool)
            {
                return new MpBool(settings)
                       {
                           Value = 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(settings)
                       {
                           Value = value
                       }
            }
            ;
            if (value is float ||
                value is double)
            {
                return new MpFloat(settings)
                       {
                           Value = value
                       }
            }
            ;
            if (value is string)
            {
                return new MpString(settings)
                       {
                           Value = value
                       }
            }
            ;
            if (value is byte[])
            {
                return new MpBin(settings)
                       {
                           Value = value
                       }
            }
            ;
            if (value is object[])
            {
                return new MpArray(settings)
                       {
                           Value = value
                       }
            }
            ;

            Type valuesType = value.GetType();

            if (valuesType.IsEnum)
            {
                return(new MpInt(settings).SetEnumVal(value));
            }
            if (IsSubclassOfArrayOfRawGeneric(typeof(KeyValuePair <,>), valuesType))
            {
                return new MpMap(settings)
                       {
                           Value = value
                       }
            }
            ;
            if (IsSubclassOfRawGeneric(typeof(Dictionary <,>), valuesType))
            {
                return new MpMap(settings)
                       {
                           Value = value
                       }
            }
            ;
            if (valuesType.IsArray)
            {
                return new MpArray(settings)
                       {
                           Value = ((IEnumerable)value).Cast <Object>().ToArray()
                       }
            }
            ;
            if (typeof(IEnumerable).IsAssignableFrom(valuesType))
            {
                return new MpArray(settings)
                       {
                           Value = ((IEnumerable)value).Cast <Object>().ToArray()
                       }
            }
            ;

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

            if (!ReferenceEquals(val, null))
            {
                val._settings = settings;

                return(val);
            }

            return(MsgPackSerializer.SerializeObject(value, settings));
        }
예제 #2
0
        public static MsgPackItem Pack(object value)
        {
            if (ReferenceEquals(value, null))
            {
                return(new MpNull());
            }

            var typeCode = Type.GetTypeCode(value.GetType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return(new MpBool()
                {
                    Value = value
                });

            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return(new MpInt()
                {
                    Value = value
                });

            case TypeCode.Single:
            case TypeCode.Double:
                return(new MpFloat()
                {
                    Value = value
                });

            case TypeCode.String:
                return(new MpString()
                {
                    Value = value
                });

            case TypeCode.DateTime:
                return(new MpDateTime()
                {
                    Value = value
                });

            default:
                if (value is byte[])
                {
                    return new MpBin()
                           {
                               Value = value
                           }
                }
                ;
                if (value is object[])
                {
                    return new MpArray()
                           {
                               Value = value
                           }
                }
                ;
                if (value is DateTimeOffset)
                {
                    return new MpDateTime()
                           {
                               Value = value
                           }
                }
                ;
                break;
            }


            Type valuesType = value.GetType();

            //if (valuesType.IsEnum) return new MpInt().SetEnumVal(value);
            if (valuesType.IsEnum)
            {
                return new MpInt()
                       {
                           Value = value
                       }
            }
            ;
            if (IsSubclassOfArrayOfRawGeneric(typeof(KeyValuePair <,>), valuesType))
            {
                return new MpMap()
                       {
                           Value = value
                       }
            }
            ;
            if (IsSubclassOfRawGeneric(typeof(Dictionary <,>), valuesType))
            {
                return new MpMap()
                       {
                           Value = value
                       }
            }
            ;
            if (valuesType.IsArray)
            {
                return new MpArray()
                       {
                           Value = ((IEnumerable)value).Cast <Object>().ToArray()
                       }
            }
            ;
            if (typeof(IEnumerable).IsAssignableFrom(valuesType))
            {
                return new MpArray()
                       {
                           Value = ((IEnumerable)value).Cast <Object>().ToArray()
                       }
            }
            ;

            // 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));
        }