Пример #1
0
 private DFormSerializer(object toSerial,DFormOptions options,PropertyInfo[] getFields)
 {
     Contract.Requires(getFields != null);
     _options = options;
     _toserial = toSerial;
     _serialized = getFields;
 }
Пример #2
0
        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;

        }