Example #1
0
        public string Execute(object src)
        {
            string processObj = "{";

            FieldInfo[] fieldInfo = src.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            for (int i = 0; i < fieldInfo.Length; i++)
            {
                processObj += "\"" + fieldInfo[i].Name + "\":";
                processObj += Jsonfier.ToJson(fieldInfo[i].GetValue(src), this);
                if (i + 1 != fieldInfo.Length)
                {
                    processObj += ",";
                }
            }
            processObj += "}";
            return(processObj);
        }
Example #2
0
        public string Execute(object src)
        {
            string processObj = "{";

            PropertyInfo[] propInfo = src.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            for (int i = 0; i < propInfo.Length; i++)
            {
                MethodInfo m   = propInfo[i].GetGetMethod();
                object     val = propInfo[i].GetGetMethod().Invoke(src, new object[0]);
                processObj += "\"" + propInfo[i].Name + "\":";
                processObj += Jsonfier.ToJson(val, this);
                if (i + 1 != propInfo.Length)
                {
                    processObj += ",";
                }
            }
            processObj += "}";
            return(processObj);
        }
        public string Execute(object src)
        {
            string processObj = "{";

            MethodInfo[] methodInfo = src.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);
            foreach (MethodInfo t in methodInfo)
            {
                if (t.ReturnType == typeof(void) || t.GetParameters().Length != 0)
                {
                    continue;
                }
                processObj += "\"" + t.Name + "\":";
                object methodReturn = t.Invoke(src, new object[0]);
                processObj += Jsonfier.ToJson(methodReturn, this);
                processObj += ",";
            }
            processObj  = processObj.Substring(0, processObj.Length - 1);
            processObj += "}";
            return(processObj);
        }
Example #4
0
        public static string ToJson(object src)
        {
            if (src == null)
            {
                return("null");
            }

            Type t = src.GetType();

            if (IsBuiltInType(t))
            {
                if (t.Name.Equals("Char") || t.Name.Equals("String") || t.Name.Equals("DateTime"))
                {
                    return(EncloseString(src.ToString(), quote, quote));
                }
                else
                {
                    return(src.ToString().ToLower().Replace(',', '.'));
                }
            }
            else if (src is IEnumerable)
            {
                StringBuilder sb = new StringBuilder();

                foreach (object item in (IEnumerable)src)
                {
                    sb.Append(Jsonfier.ToJson(item) + ",");
                }

                sb.Remove(sb.Length - 1, 1);

                return(EncloseString(sb.ToString(), iniSquareBracket, endSquareBracket));
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                MemberInfo[] members = t.GetMembers(bindFlags).Where(filter).ToArray();

                foreach (MemberInfo item in members)
                {
                    sb.Append(EncloseString(item.Name.Replace("get_", ""), quote, quote) + ":");

                    if (item is PropertyInfo)
                    {
                        sb.Append(Jsonfier.ToJson(((PropertyInfo)item).GetValue(src)));
                    }
                    else if (item is FieldInfo)
                    {
                        sb.Append(Jsonfier.ToJson(((FieldInfo)item).GetValue(src)));
                    }
                    else if (item is MethodInfo && ((MethodInfo)item).GetParameters().Length == 0)
                    {
                        sb.Append(Jsonfier.ToJson(((MethodInfo)item).Invoke(src, new object[0])));
                    }
                    sb.Append(",");
                }

                sb.Remove(sb.Length - 1, 1);

                return(EncloseString(sb.ToString(), iniBracket, endBracket));
            }
        }