示例#1
0
        public void Send(Message message)
        {
            JsonObject msg = JsonExportAttribute.GetJson(message);

            if (message is InGameMessage)
            {
                msg.Add("game_id", GameID);
            }

            Socket.Send(SimpleJson.SimpleJson.SerializeObject(msg));
        }
示例#2
0
        public static JsonObject GetJson(object obj)
        {
            PropertyInfo[] PropInf = obj.GetType().GetProperties();
            JsonObject     jobj    = new JsonObject();

            foreach (PropertyInfo p in PropInf)
            {
                JsonExportAttribute attr = (JsonExportAttribute)Attribute.GetCustomAttribute(p, typeof(JsonExportAttribute));
                if (attr == null)
                {
                    continue;
                }
                jobj.Add(attr.name, p.GetValue(obj));
            }
            return(jobj);
        }
示例#3
0
        //TODO maybe put this in a separate attribute with class as target
        //TODO replace try catch with proper errorhandling / exceptions
        //TODO implement nested classes / handle json objs and json arrays
        public static bool Deserialize(JsonObject obj, out Message msg)
        {
            Init();

            msg = null;

            string MessageType;

            try
            {
                MessageType = (string)obj["type"];
            }
            catch
            {
                //DEBUG
                Console.WriteLine("Couldn't deserialize, because type was missing");
                Console.WriteLine(obj);
                return(false);
            }

            foreach (Type t in MessageTypes)
            {
                Message Output = (Message)Activator.CreateInstance(t);

                if (Output.Type == (string)obj["type"])
                {
                    foreach (PropertyInfo p in t.GetRuntimeProperties())
                    {
                        JsonExportAttribute attr = (JsonExportAttribute)Attribute.GetCustomAttribute(p, typeof(JsonExportAttribute));
                        if (attr == null || attr.name == "type")
                        {
                            continue;
                        }

                        object ObjectBuf;

                        try
                        {
                            ObjectBuf = obj[attr.name];
                        }
                        catch
                        {
                            //DEBUG
                            Console.WriteLine("Couldn't deserialize to " + t.ToString() + " , because a property was missing");
                            Console.WriteLine(obj);
                            return(false);
                        }
                        if (typeof(ICollection).IsAssignableFrom(p.PropertyType) && p.PropertyType.IsGenericType && ObjectBuf is JsonArray)
                        {
                            JsonArray JsonArray = (ObjectBuf as JsonArray);

                            if (JsonArray.Count == 0)
                            {
                                ObjectBuf = Activator.CreateInstance(p.PropertyType);
                            }
                            else
                            {
                                Type[] ContainedTypes = p.PropertyType.GetGenericArguments();
                                if (ContainedTypes.Length == 1 && typeof(ICollection <>).MakeGenericType(ContainedTypes).IsAssignableFrom(p.PropertyType))
                                {
                                    if (ContainedTypes[0].IsAssignableFrom(JsonArray[0].GetType()))
                                    {
                                        dynamic Collection = (dynamic)Activator.CreateInstance(p.PropertyType);
                                        foreach (object o in JsonArray)
                                        {
                                            dynamic added = Convert.ChangeType(o, ContainedTypes[0]);
                                            Collection.Add(added);
                                        }
                                        ObjectBuf = Collection;
                                    }
                                }
                                if (ContainedTypes.Length == 2 && typeof(IDictionary <,>).MakeGenericType(ContainedTypes).IsAssignableFrom(p.PropertyType))
                                {
                                    if (ContainedTypes[0].IsAssignableFrom((JsonArray[0] as JsonObject)["Key"].GetType()) &&
                                        ContainedTypes[1].IsAssignableFrom((JsonArray[0] as JsonObject)["Value"].GetType()))
                                    {
                                        dynamic Dict = (dynamic)Activator.CreateInstance(p.PropertyType);
                                        foreach (object o in JsonArray)
                                        {
                                            JsonObject JsonObject = o as JsonObject;
                                            Dict.Add((dynamic)JsonObject["Key"], (dynamic)JsonObject["Value"]);
                                        }
                                        ObjectBuf = Dict;
                                    }
                                }
                            }
                        }
                        try
                        {
                            p.SetValue(Output, Convert.ChangeType(ObjectBuf, p.PropertyType));
                        }
                        catch
                        {
                            //DEBUG
                            Console.WriteLine("Couldn't deserialize to " + t.ToString() + " , because conversion from " + ObjectBuf.GetType().ToString() + " to " + p.PropertyType.ToString() + " was not possible");
                            Console.WriteLine(obj);
                            return(false);
                        }
                    }

                    msg = Output;
                    return(true);
                }
            }
            Console.WriteLine("Couldn't deserialize, because the given type doesn't exist");
            Console.WriteLine(obj);
            return(false);
        }