Example #1
0
        Dictionary <string, object> TypifyDictionary(IDictionary <MessagePackObject, MessagePackObject> dict)
        {
            Dictionary <string, object> returnDictionary = new Dictionary <string, object>();

            foreach (var pair in dict)
            {
                MessagePackObject obj = (MessagePackObject)pair.Value;

                if (obj.UnderlyingType == null)
                {
                    continue;
                }

                if (obj.IsRaw)
                {
                    if (obj.IsTypeOf(typeof(string)).Value)
                    {
                        returnDictionary[pair.Key.AsString()] = new string(obj.AsCharArray());
                    }
                    else if (obj.IsTypeOf(typeof(int)).Value)
                    {
                        returnDictionary[pair.Key.AsString()] = (int)obj.ToObject();
                    }
                    else
                    {
                        throw new Exception("I don't know type: " + pair.Value.GetType().Name);
                    }
                }
                else if (obj.IsArray)
                {
                    List <object> arr = new List <object>();
                    //Console.WriteLine(obj.UnderlyingType.Name);
                    foreach (var o in obj.ToObject() as MessagePackObject[])
                    {
                        if (o.IsDictionary)
                        {
                            arr.Add(TypifyDictionary(o.AsDictionary()));
                        }
                        else if (o.IsRaw)
                        {
                            arr.Add(o.AsString());
                        }
                    }

                    returnDictionary.Add(pair.Key.AsString(), arr);
                }
                else if (obj.IsDictionary)
                {
                    returnDictionary[pair.Key.AsString()] = TypifyDictionary(obj.AsDictionary() as IDictionary <MessagePackObject, MessagePackObject>);
                }
                else if (obj.IsTypeOf(typeof(UInt16)).Value)
                {
                    returnDictionary[pair.Key.AsString()] = obj.AsUInt16();
                }
                else if (obj.IsTypeOf(typeof(UInt32)).Value)
                {
                    returnDictionary[pair.Key.AsString()] = obj.AsUInt32();
                }
                else if (obj.IsTypeOf(typeof(bool)).Value)
                {
                    returnDictionary[pair.Key.AsString()] = obj.AsBoolean();
                }
                else
                {
                    throw new Exception("hey what the f**k are you: " + obj.ToObject().GetType().Name);
                }
            }

            return(returnDictionary);
        }
		//this is a ridiculous method
		Dictionary<string, object> TypifyDictionary(MessagePackObjectDictionary dict)
		{
			Dictionary<string, object> returnDictionary = new Dictionary<string, object>();
			
			foreach (var pair in dict)
			{
				MessagePackObject obj = (MessagePackObject)pair.Value;
				string key = System.Text.Encoding.ASCII.GetString ((byte[])pair.Key);

				if (obj.UnderlyingType == null)
					continue;
				
				if (obj.IsRaw) {
					if (obj.UnderlyingType == typeof(string)) {
						if (pair.Key.IsRaw && pair.Key.IsTypeOf (typeof(Byte[])).Value)
							returnDictionary [key] = obj.AsString ();
						else
							returnDictionary [pair.Key.ToString ()] = obj.AsString ();
					}
					else if (obj.IsTypeOf (typeof(int)).Value)
						returnDictionary [pair.Key.ToString ()] = (int)obj.ToObject ();
					else if (obj.IsTypeOf (typeof(Byte[])).Value) {
						if (key == "payload") 
							returnDictionary [key] = (byte[])obj;
						else 
							returnDictionary [key] = System.Text.Encoding.ASCII.GetString ((Byte[])obj.ToObject ());
					} else
						throw new Exception ("I don't know type: " + pair.Value.GetType ().Name);
				} else if (obj.IsArray) {
					List<object> arr = new List<object> ();
					foreach (var o in obj.ToObject() as MessagePackObject[]) {
						if (o.IsDictionary)
							arr.Add (TypifyDictionary (o.AsDictionary ()));
						else if (o.IsRaw)
							arr.Add (System.Text.Encoding.ASCII.GetString ((byte[])o));
						else if (o.IsArray) {
							var enu = o.AsEnumerable ();
							List<object> array = new List<object> ();
							foreach (var blah in enu)
								array.Add (blah as object);

							arr.Add (array.ToArray ());
						} else if (o.ToObject ().GetType () == typeof(Byte)) //this is a hack because I don't know what type you are...
							arr.Add (o.ToString ());
					}

					if (pair.Key.IsRaw && pair.Key.IsTypeOf (typeof(Byte[])).Value)
						returnDictionary.Add (key, arr);
					else
						returnDictionary.Add (key, arr);
				} else if (obj.IsDictionary) {
					if (pair.Key.IsRaw && pair.Key.IsTypeOf(typeof(Byte[])).Value)
						returnDictionary [key] = TypifyDictionary (obj.AsDictionary ());
					else 
						returnDictionary [pair.Key.ToString ()] = TypifyDictionary (obj.AsDictionary ());
				} else if (obj.IsTypeOf (typeof(UInt16)).Value) {
					if (pair.Key.IsRaw && pair.Key.IsTypeOf (typeof(Byte[])).Value)
						returnDictionary [key] = obj.AsUInt16 ();
					else
						returnDictionary [pair.Key.ToString ()] = obj.AsUInt16 ();
				} else if (obj.IsTypeOf (typeof(UInt32)).Value) {
					if (pair.Key.IsRaw && pair.Key.IsTypeOf (typeof(Byte[])).Value)
						returnDictionary [key] = obj.AsUInt32 ();
					else
						returnDictionary [pair.Key.ToString ()] = obj.AsUInt32 ();
				} else if (obj.IsTypeOf (typeof(bool)).Value) {
					if (pair.Key.IsRaw && pair.Key.IsTypeOf (typeof(Byte[])).Value)
						returnDictionary [key] = obj.AsBoolean ();
					else
						returnDictionary [pair.Key.ToString ()] = obj.AsBoolean ();
				}
				else 
					throw new Exception("Don't know type: " + obj.ToObject().GetType().Name);
			}
			
			return returnDictionary;
		}
Example #3
0
        public static object Map(Type targetType, MessagePackObject source, PropertyInfo property = null)
        {
            if (source.IsNil)
            {
                return(null);
            }
            if (targetType == typeof(string))
            {
                return(source.AsString());
            }
            if (targetType == typeof(int) || targetType == typeof(int?))
            {
                return(source.AsInt32());
            }
            if (targetType == typeof(uint) || targetType == typeof(uint?))
            {
                return(source.AsUInt32());
            }
            if (targetType == typeof(long) || targetType == typeof(long?))
            {
                return(source.AsInt64());
            }
            if (targetType == typeof(ulong) || targetType == typeof(ulong?))
            {
                return(source.AsUInt64());
            }
            if (targetType == typeof(float) || targetType == typeof(float?))
            {
                return(source.AsSingle());
            }
            if (targetType == typeof(double) || targetType == typeof(double?))
            {
                return(source.AsDouble());
            }
            if (targetType == typeof(bool) || targetType == typeof(bool?))
            {
                return(source.AsBoolean());
            }
            if (targetType == typeof(byte[]))
            {
                return(source.AsBinary());
            }
            if (targetType == typeof(byte) || targetType == typeof(byte?))
            {
                return(source.AsByte());
            }
            if (targetType == typeof(sbyte) || targetType == typeof(sbyte?))
            {
                return(source.AsSByte());
            }
            if (targetType == typeof(char[]))
            {
                return(source.AsCharArray());
            }
            if (targetType == typeof(short) || targetType == typeof(short?))
            {
                return(source.AsInt16());
            }
            if (targetType == typeof(ushort) || targetType == typeof(ushort?))
            {
                return(source.AsUInt16());
            }
            if (targetType == typeof(DateTime) || targetType == typeof(DateTime?))
            {
                return(MapDateTime(property, source));
            }
            if (targetType == typeof(IList <MessagePackObject>))
            {
                return(source.AsList());
            }
            if (targetType == typeof(IEnumerable <MessagePackObject>))
            {
                return(source.AsEnumerable());
            }

            var ti = targetType.GetTypeInfo();

            if (targetType == typeof(MessagePackObject))
            {
                return(source);
            }

            if (ti.IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(List <>) ||
                                     targetType.GetGenericTypeDefinition() == typeof(IList <>)))
            {
                return(MapList(targetType, source.AsList()));
            }

            if (ti.IsClass && source.IsList)
            {
                return(MapClass(targetType, source));
            }

            if (ti.IsClass && source.IsMap)
            {
                return(MapDictionary(targetType, source.AsDictionary()));
            }

            if (ti.IsEnum)
            {
                return(MapEnum(targetType, source));
            }

            throw new MessagePackMapperException(
                      $"Cannot find MsgPackObject converter for type {targetType.FullName}.");
        }