Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <param name="originalValue"></param>
        /// <returns></returns>
        public bool IsNullEmptyOrDefault(ref object defaultValue, ref object originalValue, ConfigOption configOption)
        {
            if (defaultValue == null)
            {
                defaultValue = "NULL";
            }

            if (originalValue == null)
            {
                originalValue = "NULL";
            }
            else if (configOption.Serialization.Mode == JsonMode.Custom &&
                     configOption.Serialization.JsonConverter == typeof(LazyControlJsonConverter) &&
                     originalValue is Control &&
                     !((Control)originalValue).Visible)
            {
                return(true);
            }
            else if (!(originalValue is string) && originalValue is IEnumerable)
            {
                if (!((IEnumerable)originalValue).GetEnumerator().MoveNext())
                {
                    originalValue = "NULL";
                }
            }
            else if (originalValue is DateTime)
            {
                DateTime t = (DateTime)originalValue;

                if (t.Equals(DateTime.MinValue) || t.Equals(DateTime.MaxValue))
                {
                    return(true);
                }
            }
            else if (originalValue is Unit)
            {
                Unit defaultVal  = (Unit)defaultValue;
                Unit originalVal = (Unit)originalValue;

                if (originalVal.IsEmpty || defaultVal.Equals(originalValue))
                {
                    return(true);
                }
            }
            else if (originalValue is XControl)
            {
                return(((XControl)originalValue).IsDefault);
            }
            else if (originalValue is StateManagedItem)
            {
                return(((StateManagedItem)originalValue).IsDefault);
            }
            else if (originalValue is Margins)
            {
                return(((Margins)originalValue).IsDefault);
            }

            return(defaultValue.Equals(originalValue));
        }
Beispiel #2
0
        private void ToExtConfig(object obj, ConfigOption configOption)
        {
            if (configOption.Serialization.Mode != JsonMode.Ignore)
            {
                object originalValue = configOption.Value;
                object defaultValue  = configOption.DefaultValue;

                if (!IsNullEmptyOrDefault(ref defaultValue, ref originalValue, configOption))
                {
                    if (originalValue.Equals("NULL"))
                    {
                        originalValue = null;
                    }

                    string name = configOption.PropertyName.ToLowerCamelCase();

                    if (configOption.Serialization.Name.IsNotEmpty())
                    {
                        if (configOption.Serialization.Name.Contains(">"))
                        {
                            string[] parts = configOption.Serialization.Name.Split('>');
                            name = parts[0];
                            PropertyInfo          subProp = originalValue.GetType().GetProperty(parts[1]);
                            ConfigOptionAttribute subAttr = ClientConfig.GetClientConfigAttribute(subProp);

                            if (subAttr != null)
                            {
                                configOption.Serialization = this.ConfigOptionAttr2SerializationOptions(subAttr);
                                originalValue = subProp.GetValue(originalValue, null);
                            }
                        }
                        else
                        {
                            name = configOption.Serialization.Name;
                        }
                    }

                    if (this.exclude.Contains(name))
                    {
                        return;
                    }

                    StringBuilder temp = new StringBuilder(128);

                    switch (configOption.Serialization.Mode)
                    {
                    case JsonMode.ToLower:
                        this.WriteValue(name, originalValue.ToString().ToLowerInvariant());
                        break;

                    case JsonMode.ToCamelLower:
                        this.WriteValue(name, originalValue.ToString().ToLowerCamelCase());
                        break;

                    case JsonMode.Raw:
                        this.WriteRawValue(name, originalValue);
                        break;

                    case JsonMode.ObjectAllowEmpty:
                    case JsonMode.Object:
                        temp.Append(new ClientConfig().SerializeInternal(originalValue, this.owner));

                        if (!IsEmptyObject(temp.ToString()) || configOption.Serialization.Mode == JsonMode.ObjectAllowEmpty)
                        {
                            string type = this.GetInstanceOf(originalValue);

                            if (type.IsNotEmpty())
                            {
                                this.WriteRawValue(name, "new {0}({1})".FormatWith(type, temp.ToString()));
                            }
                            else
                            {
                                this.WriteRawValue(name, temp.ToString());
                            }
                        }
                        break;

                    case JsonMode.UnrollCollection:
                        IEnumerable si = (IEnumerable)originalValue;

                        foreach (object unrollingObject in si)
                        {
                            if (unrollingObject != null)
                            {
                                this.Process(unrollingObject);
                            }
                        }
                        break;

                    case JsonMode.UnrollObject:
                        this.Process(originalValue);
                        break;

                    case JsonMode.Array:
                    case JsonMode.AlwaysArray:
                        if (originalValue is IEnumerable)
                        {
                            IList list = (IList)originalValue;

                            if (list.Count == 1 && configOption.Serialization.Mode != JsonMode.AlwaysArray)
                            {
                                temp.Append(new ClientConfig().SerializeInternal(list[0], this.owner));

                                if (!IsEmptyObject(temp.ToString()))
                                {
                                    this.WriteRawValue(name, temp.ToString());
                                }
                            }
                            else
                            {
                                bool comma = false;
                                temp.Append("[");

                                foreach (object o in list)
                                {
                                    if (comma)
                                    {
                                        temp.Append(",");
                                    }

                                    if (o.GetType().IsPrimitive || o is string)
                                    {
                                        temp.Append(JSON.Serialize(o));
                                    }
                                    else
                                    {
                                        temp.Append(new ClientConfig().SerializeInternal(o, this.owner));
                                    }

                                    comma = true;
                                }
                                temp.Append("]");

                                string type = this.GetInstanceOf(originalValue);

                                if (type.IsNotEmpty())
                                {
                                    this.WriteRawValue(name, "new {0}({1})".FormatWith(type, temp.ToString()));
                                }
                                else
                                {
                                    this.WriteRawValue(name, temp.ToString());
                                }
                            }
                        }
                        break;

                    case JsonMode.ArrayToObject:
                        if (originalValue is IEnumerable)
                        {
                            IList list = (IList)originalValue;

                            temp.Append("{");
                            bool comma = false;

                            foreach (object o in list)
                            {
                                if (comma)
                                {
                                    temp.Append(",");
                                }
                                temp.Append(o.ToString());
                                comma = true;
                            }
                            temp.Append("}");

                            if (!IsEmptyObject(temp.ToString()))
                            {
                                this.WriteRawValue(name, temp.ToString());
                            }
                        }
                        break;

                    case JsonMode.Custom:
                        if (originalValue != null)
                        {
                            if (originalValue is IList && ((IList)originalValue).Count == 0)
                            {
                                break;
                            }

                            if (name != "-")
                            {
                                this.writer.WritePropertyName(name);
                            }

                            ExtJsonConverter converter = (ExtJsonConverter)Activator.CreateInstance(configOption.Serialization.JsonConverter);
                            converter.Name         = name;
                            converter.PropertyName = configOption.PropertyName.ToTitleCase(CultureInfo.InvariantCulture);
                            converter.Owner        = obj;

                            converter.WriteJson(this.writer, originalValue, null);
                        }
                        break;

                    case JsonMode.ToClientID:
                        Control control   = null;
                        string  controlID = "";
                        bool    rawID     = false;

                        if (originalValue is Control)
                        {
                            control = (Control)originalValue;
                        }
                        else
                        {
                            controlID = originalValue.ToString() ?? "";

                            if (controlID.StartsWith("{raw}"))
                            {
                                controlID = controlID.Substring(5);
                                rawID     = true;
                            }
                            else
                            {
                                control = ControlUtils.FindControl(this.owner, controlID, true);
                            }
                        }

                        if (control != null || rawID)
                        {
                            if (name.StartsWith("{raw}"))
                            {
                                name = name.Substring(5);
                                this.WriteValue(name, control != null ? control.ClientID : controlID);
                            }
                            else
                            {
                                this.WriteRawValue(name, control != null ? control.ClientID : controlID);
                            }
                        }
                        else
                        {
                            this.WriteRawValue(name, controlID);
                        }

                        break;

                    case JsonMode.ToString:
                        this.WriteValue(name, originalValue.ToString());
                        break;

                    case JsonMode.Url:
                        string url = originalValue.ToString();

                        this.WriteValue(name, this.owner == null ? url : this.owner.ResolveClientUrl(url));
                        break;

                    case JsonMode.Value:
                    default:
                        this.WriteValue(name, originalValue);
                        break;
                    }
                }
            }
        }