internal override string AsJSON(object actualField, PropertyInfo field, DFormSerializer serializer)
        {
            Contract.Assume(containedFields != null);
            StringBuilder generatedJSON = new StringBuilder(containedFields.Length);
            foreach (var containedField in containedFields)
            {
                generatedJSON.Append(serializer.serializeNamedField(containedField));
            }

            return string.Format("{{ {0} ,\"html\":[{1}] }}", base.AsJSON(actualField,field, serializer), generatedJSON);
        }
        internal virtual string AsJSON(object actualField, DFormSerializer serializer)
        {
            Contract.Requires(serializer != null);
            Contract.Ensures(Contract.Result<string>() != null);

            List<string> fields = new List<string>(3);
            fields.Add(
                !String.IsNullOrEmpty(this.id)
                    ? string.Format("\"id\":\"{0}\"", this.id)
                    : string.Format("\"id\":\"{0}\"", actualField.GetType().Name));

            fields.Add(
                !String.IsNullOrEmpty(this.name)
                    ? string.Format("\"name\":\"{0}\"", this.name)
                    : string.Format("\"name\":\"{0}\"", actualField.GetType().Name));

            if (!String.IsNullOrEmpty(@class))
                fields.Add(string.Format("\"class\":\"{0}\"", @class));

            if (!String.IsNullOrEmpty(style))
                fields.Add(string.Format("\"style\":\"{0}\"", style));

            if (!String.IsNullOrEmpty(lang))
                fields.Add(string.Format("\"lang\":\"{0}\"", lang));

            if (!String.IsNullOrEmpty(title))
                fields.Add(string.Format("\"title\":\"{0}\"", title));

            if (autocomplete)
                fields.Add(string.Format("\"autocomplete\":\"on\""));
            else
                fields.Add(string.Format("\"autocomplete\":\"off\""));

            if (noValidate)
                fields.Add(string.Format("\"novaildate\":\"novaildate\""));

            if (!String.IsNullOrEmpty(enc_type))
                fields.Add(string.Format("\"enc-type\":\"{0}\"", enc_type));

            if (!String.IsNullOrEmpty(accept_charset))
                fields.Add(string.Format("\"accept-charset\":\"{0}\"", accept_charset));

            if (!String.IsNullOrEmpty(target))
                fields.Add(string.Format("\"target\":\"{0}\"", target));

            string toRet = fields.Aggregate((a, b) => a + "," + b);
            return toRet ?? "";
        }
        public static string serialize(object toSerialize, DFormOptions opts)
        {
            Contract.Requires(toSerialize != null);
            Contract.Ensures(Contract.Result<string>() != null);

            PropertyInfo[] infos = toSerialize.GetType().GetProperties();
            DFormSerializer serializer = new DFormSerializer(toSerialize, opts = opts ?? new DFormOptions(), infos);
            List<string> fullySerialized = new List<string>();

            
            foreach (var field in infos)
            {
                foreach (var atti in field.GetCustomAttributes(typeof(DFormField), false))
                {
                    var at = (DFormField)atti;
                    if (!at.hasParent) //this will be serialized by anouther..
                        fullySerialized.Add(at.AsJSON(toSerialize,field,serializer));
                }
            }
            string toRet;

            var formAttri = toSerialize.GetType().GetCustomAttributes(typeof(DFormAttribute), false);
            if(formAttri.Length > 0)
            {
                
                if (opts.HaveCancel)
                    fullySerialized.Add(string.Format("{{\"type\":\"reset\",\"Value\":\"{0}\" }}", opts.CancelText));

                if (opts.HaveSubmit)
                    fullySerialized.Add(string.Format("{{\"type\":\"submit\",\"Value\":\"{0}\" }}", opts.SubmitText));

                var dform = (DFormAttribute)formAttri[0];
                toRet = String.Format(
                    "{{{0},\"html\":[{1}]}}",
                    dform.AsJSON(toSerialize,serializer), fullySerialized.Aggregate((s, i) => s + "," + i), dform.id, dform.name);
            }
            else
            {
                toRet = string.Format("{{\"html\":[{0}]}}",fullySerialized.Aggregate((s,i) => s + "," + i) );
            }

            return toRet;

        }
        internal override string AsJSON(object actualField, PropertyInfo field, DFormSerializer serializer)
        {
            List<string> json = new List<string>();

            if (!(actualField is IEnumerable))
                throw new InvalidCastException("Cannot use an non enumrable type for a selection list");

            foreach (var option in actualField as IEnumerable)
            {
              
                Type optType = option.GetType();
                string val = option.ToString();
                string text = option.ToString();
                if (_textField != null)
                {
                    try
                    {
                        val = optType.GetProperty(_textField).GetValue(option, null).ToString();
                    }
                    catch (Exception)
                    {
                        val = option.ToString();
                    }
                }
                if (_valueField != null)
                {
                    try
                    {
                        text = optType.GetProperty(_valueField).GetValue(option, null).ToString();
                    }
                    catch (Exception)
                    {
                        text = option.ToString();
                    }
                }
                json.Add(string.Format("\"{0}\":\"{1}\"", text, val));
            }
            return string.Format("{{ {0},{1} \"options\":{{ {2} }} }}", base.AsJSON(actualField,field, serializer), Multiple ? "\"multiple\":\"multiple\"," : ""  
                ,json.Aggregate((s,i) => s + "," + i));
        }
Exemple #5
0
        internal virtual string AsJSON(object actualField, PropertyInfo field, DFormSerializer serializer)
        {
            Contract.Requires(serializer != null);
            Contract.Ensures(Contract.Result<string>() != null);

            List<string> fields = new List<string>(3) { string.Format("\"type\":\"{0}\"", this.type.ToString().ToLower()) };
            fields.Add(
                !String.IsNullOrEmpty(this.id)
                    ? string.Format("\"id\":\"{0}\"", this.id)
                    : string.Format("\"id\":\"{0}\"", field.Name));

            var args = field.GetValue(actualField,null);
            if (type == DFORM_TYPE.Checkbox || type == DFORM_TYPE.Radio)
            {
                var val = args as bool?;
                if (val != null)
                {
                    if (val.Value)
                    {
                        fields.Add("\"checked\":\"checked\"");
                    }
                }
            }
            else
                fields.Add(string.Format("\"value\":\"{0}\"", args ?? ""));

            fields.Add(
                !String.IsNullOrEmpty(this.name)
                    ? string.Format("\"name\":\"{0}\"", this.name)
                    : string.Format("\"name\":\"{0}\"", actualField.GetType().Name + "." + field.Name));

            if (!String.IsNullOrEmpty(@class))
                fields.Add(string.Format("\"class\":\"{0}\"", @class));

            if (!String.IsNullOrEmpty(style))
                fields.Add(string.Format("\"style\":\"{0}\"", style));

            if (!String.IsNullOrEmpty(lang))
                fields.Add(string.Format("\"lang\":\"{0}\"", lang));

            if (!String.IsNullOrEmpty(title))
                fields.Add(string.Format("\"title\":\"{0}\"", title));

            if (!String.IsNullOrEmpty(caption))
            {
                if (Resource != null)
                {
                    var resProp = Resource.GetProperty(caption);
                    if (resProp != null)
                    {
                        var localProp = resProp.GetValue(null, null);
                        if (localProp != null)
                            fields.Add(string.Format("\"caption\":\"{0}\"", localProp));
                    }
                    else
                    {
                        fields.Add(string.Format("\"caption\":\"{0}\"", caption));
                    }

                }
                else
                    fields.Add(string.Format("\"caption\":\"{0}\"", caption));
            }

            string toRet = fields.Aggregate((a, b) => a + "," + b);
           return toRet ?? "";
        }
 internal override string AsJSON(object actualField, PropertyInfo field, DFormSerializer serializer)
 {
     return string.Format("{{ \"type\":\"div\", \"class\":\"editor\",\"html\":[{{{0}}}]}}",
                          base.AsJSON(actualField,field, serializer));
 }