コード例 #1
0
        protected virtual void Deserialize(JObject jObject, LayoutElement element)
        {
            JObject jObjectLocation = (JObject)jObject["location"];
            int     x = JObjectHelper.GetInt32(jObject, "x", 0);
            int     y = JObjectHelper.GetInt32(jObject, "y", 0);

            element.Location = new Point(x, y);

            JObject jObjectSize = (JObject)jObject["size"];
            int     width       = JObjectHelper.GetInt32(jObject, "width", 0);
            int     height      = JObjectHelper.GetInt32(jObject, "height", 0);

            element.Size = new Size(width, height);
        }
コード例 #2
0
        public void Load(string fileName)
        {
            FileName = fileName;

            if (File.Exists(fileName))
            {
                var contents        = File.ReadAllText(fileName, Encoding.UTF8);
                var jObjectSettings = JArray.Parse(contents);


                Items.Clear();

                SuspendAutoSave();

                foreach (JObject jObjectSettingsItem in jObjectSettings)
                {
                    try
                    {
                        var name = JObjectHelper.GetString(jObjectSettingsItem, "name", string.Empty);
                        var type = JObjectHelper.GetString(jObjectSettingsItem, "type", string.Empty);

                        object value = null;

                        if (type == "string")
                        {
                            value = JObjectHelper.GetString(jObjectSettingsItem, "value", string.Empty);
                        }
                        else if (type == "integer")
                        {
                            value = JObjectHelper.GetInt32(jObjectSettingsItem, "value", 0);
                        }
                        else if (type == "double")
                        {
                            value = JObjectHelper.GetDecimal(jObjectSettingsItem, "value", 0);
                        }
                        else if (type == "boolean")
                        {
                            value = JObjectHelper.GetBoolean(jObjectSettingsItem, "value", false);
                        }
                        else if (type == "guid")
                        {
                            value = JObjectHelper.GetGuid(jObjectSettingsItem, "value", Guid.Empty);
                        }
                        else if (type == "point")
                        {
                            var x = JObjectHelper.GetInt32(jObjectSettingsItem, "x", 0);
                            var y = JObjectHelper.GetInt32(jObjectSettingsItem, "y", 0);
                            value = new Point(x, y);
                        }
                        else if (type == "size")
                        {
                            var width  = JObjectHelper.GetInt32(jObjectSettingsItem, "width", 0);
                            var height = JObjectHelper.GetInt32(jObjectSettingsItem, "height", 0);
                            value = new Size(width, height);
                        }
                        else if (type == "datetime")
                        {
                            var valueStr = JObjectHelper.GetString(jObjectSettingsItem, "value", DateTime.UtcNow.ToString("o"));
                            value = DateTime.Parse(valueStr);
                        }
                        else if (type == "version")
                        {
                            value = new Version(JObjectHelper.GetString(jObjectSettingsItem, "value"));
                        }
                        else if (type == "list:String")
                        {
                            value = new List <string>();
                            var jArrayStrings = (JArray)jObjectSettingsItem["value"];
                            foreach (string str in jArrayStrings)
                            {
                                ((List <string>)value).Add(str);
                            }
                        }

                        var settingsItem = new SettingsItem(name);
                        settingsItem.OnChange += settingsItem_OnChange;
                        settingsItem.Value     = value;
                        Items.Add(settingsItem);
                    }
                    catch (Exception ex)
                    {
                    }
                }

                ResumeAutoSave();
            }
        }
コード例 #3
0
        public static void Deserialize(GenericPropertyCollection propertyCollection, JObject jObject)
        {
            foreach (var property in propertyCollection)
            {
                if (jObject[property.Name] != null)
                {
                    switch (property.Type)
                    {
                    case Enum.GenericPropertyType.String:
                    case Enum.GenericPropertyType.Path:
                        property.Value = JObjectHelper.GetString(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Integer:
                        property.Value = JObjectHelper.GetInt32(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Decimal:
                        property.Value = JObjectHelper.GetDouble(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Boolean:
                        property.Value = JObjectHelper.GetBoolean(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Enumeration:
                        property.Value = JObjectHelper.GetString(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Guid:
                        property.Value = JObjectHelper.GetGuid(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Color:
                        property.Value = JObjectHelper.GetColor(jObject, property.Name);
                        break;

                    case Enum.GenericPropertyType.Point:
                        var x = JObjectHelper.GetInt32(jObject, "x");
                        var y = JObjectHelper.GetInt32(jObject, "y");
                        property.Value = new Point(x, y);
                        break;

                    case Enum.GenericPropertyType.Size:
                        var width  = JObjectHelper.GetInt32(jObject, "width");
                        var height = JObjectHelper.GetInt32(jObject, "height");
                        property.Value = new Size(width, height);
                        break;

                    case Enum.GenericPropertyType.Rectangle:
                        var x_      = JObjectHelper.GetInt32(jObject, "x");
                        var y_      = JObjectHelper.GetInt32(jObject, "y");
                        var width_  = JObjectHelper.GetInt32(jObject, "width");
                        var height_ = JObjectHelper.GetInt32(jObject, "height");
                        property.Value = new Rectangle(x_, y_, width_, height_);
                        break;

                    case Enum.GenericPropertyType.DateTime:
                        var valueStr = JObjectHelper.GetString(jObject, property.Name, DateTime.UtcNow.ToString("o"));
                        property.Value = DateTime.Parse(valueStr);
                        break;

                    case Enum.GenericPropertyType.Version:
                        var versionStr = JObjectHelper.GetString(jObject, property.Name);
                        property.Value = new Version(versionStr);
                        break;

                    default:
                        break;
                    }
                }
            }
        }
コード例 #4
0
        public static Dictionary <string, object> DeserializeFromJArrayToDictionary(GenericPropertyCollection propertyCollection, JArray jArray)
        {
            var result = new Dictionary <string, object>();

            foreach (var property in propertyCollection)
            {
                foreach (JObject jObjectProperty in jArray)
                {
                    var propertyName = JObjectHelper.GetString(jObjectProperty, "name");
                    if (!string.IsNullOrEmpty(propertyName) && propertyName == property.Name)
                    {
                        switch (property.Type)
                        {
                        case Enum.GenericPropertyType.String:
                        case Enum.GenericPropertyType.Path:
                            result.Add(propertyName, JObjectHelper.GetString(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Integer:
                            result.Add(propertyName, JObjectHelper.GetInt32(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Decimal:
                            result.Add(propertyName, JObjectHelper.GetDouble(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Boolean:
                            result.Add(propertyName, JObjectHelper.GetBoolean(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Enumeration:
                            result.Add(propertyName, JObjectHelper.GetString(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Guid:
                            result.Add(propertyName, JObjectHelper.GetGuid(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Color:
                            result.Add(propertyName, JObjectHelper.GetColor(jObjectProperty, "value"));
                            break;

                        case Enum.GenericPropertyType.Point:
                            var x = JObjectHelper.GetInt32(jObjectProperty, "x");
                            var y = JObjectHelper.GetInt32(jObjectProperty, "y");
                            result.Add(propertyName, new Point(x, y));
                            break;

                        case Enum.GenericPropertyType.Size:
                            var width  = JObjectHelper.GetInt32(jObjectProperty, "width");
                            var height = JObjectHelper.GetInt32(jObjectProperty, "height");
                            result.Add(propertyName, new Size(width, height));
                            break;

                        case Enum.GenericPropertyType.Rectangle:
                            var x_      = JObjectHelper.GetInt32(jObjectProperty, "x");
                            var y_      = JObjectHelper.GetInt32(jObjectProperty, "y");
                            var width_  = JObjectHelper.GetInt32(jObjectProperty, "width");
                            var height_ = JObjectHelper.GetInt32(jObjectProperty, "height");
                            result.Add(propertyName, new Rectangle(x_, y_, width_, height_));
                            break;

                        case Enum.GenericPropertyType.DateTime:
                            var valueStr = JObjectHelper.GetString(jObjectProperty, "value", DateTime.UtcNow.ToString("o"));
                            result.Add(propertyName, DateTime.Parse(valueStr));
                            break;

                        case Enum.GenericPropertyType.Version:
                            var versionStr = JObjectHelper.GetString(jObjectProperty, "value");
                            result.Add(propertyName, new Version(versionStr));
                            break;

                        default:
                            break;
                        }
                        break;
                    }
                }
            }

            return(result);
        }