Beispiel #1
0
        public static string ToJSON(this object obj, JSONSerializationMode mode)
        {
            //Check for null value
            if (obj == null)
            {
                return("null");
            }

            //Check if reflection should be used
            if ((mode & JSONSerializationMode.UseReflection) == JSONSerializationMode.UseReflection)
            {
                //Use Reflection to get methode
                Type         type    = obj.GetType();
                MethodInfo[] methods = type.GetMethods();

                //Check for a ToJSON method
                methods = methods.Where(c => c.Name == "ToJSON" && c.ReturnType == typeof(string)).ToArray();
                MethodInfo method = methods.SingleOrDefault(c => c.DeclaringType == type && c.GetParameters().Length == 0);

                //If there is a ToJSON method, call it
                if (method != null)
                {
                    return((string)method.Invoke(obj, new object[0]));
                }
            }

            //Call Serialize method
            return(JSON.Serialize(obj, mode));
        }
Beispiel #2
0
        /// <summary>
        /// Turn an object into a JSON-Encoded string, optionally setting parameters
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="mode">JSONSerializationModes; can be |'d together</param>
        /// <returns></returns>
        public static string Serialize(object obj, JSONSerializationMode mode)
        {
            if (obj == null)
            {
                return("null");
            }

            Type type = obj.GetType();

            if (type.IsNumeric())
            {
                return(obj.ToString());
            }
            else if (type == typeof(string) || type == typeof(char))
            {
                return("\"" + EscapeString("" + obj) + "\"");
            }
            else if (type == typeof(bool))
            {
                bool x = (bool)obj;

                return(x ? "true" : "false");
            }
            else if (type == typeof(DateTime))
            {
                DateTime dt = (DateTime)obj;

                return("\"" + dt.ToString("MM/dd/yyyy hh:mm:ss tt") + "\"");
            }

            StringBuilder result = new StringBuilder();

            if (obj.GetType().IsArray)
            {
                result.Append("[");

                Array array = (Array)obj;

                foreach (object value in array)
                {
                    result.Append(value.ToJSON(mode) + ",");
                }

                string json = result.ToString();

                if (json.EndsWith(","))
                {
                    json = json.Substring(0, json.Length - 1);
                }

                json += "]";

                return(json);
            }
            else if (obj is IDictionary)
            {
                IDictionary           d = (IDictionary)obj;
                IDictionaryEnumerator i = d.GetEnumerator();

                result.Append("{");

                while (i.MoveNext())
                {
                    if ((mode & JSONSerializationMode.JavascriptObject) == JSONSerializationMode.JavascriptObject)
                    {
                        result.Append("" + i.Key + ":");
                    }
                    else
                    {
                        result.Append("\"" + i.Key + "\":");
                    }

                    result.Append(i.Value.ToJSON(mode));
                    result.Append(",");
                }

                string json = result.ToString();

                if (json.EndsWith(","))
                {
                    json = json.Substring(0, json.Length - 1);
                }

                json += "}";

                return(json);
            }
            else if (obj is NameValueCollection)
            {
                NameValueCollection col = (NameValueCollection)obj;

                result.Append("{");

                for (int i = 0; i < col.Count; i++)
                {
                    string key   = col.Keys[i];
                    string value = col[key];

                    if ((mode & JSONSerializationMode.JavascriptObject) == JSONSerializationMode.JavascriptObject)
                    {
                        result.Append("" + key + ":");
                    }
                    else
                    {
                        result.Append("\"" + key + "\":");
                    }

                    result.Append(value.ToJSON(mode));
                    result.Append(",");
                }

                string json = result.ToString();

                if (json.EndsWith(","))
                {
                    json = json.Substring(0, json.Length - 1);
                }

                json += "}";

                return(json);
            }
            else if (obj is IEnumerable)
            {
                IEnumerable item = (IEnumerable)obj;
                IEnumerator i    = item.GetEnumerator();

                result.Append("[");

                while (i.MoveNext())
                {
                    result.Append(i.Current.ToJSON(mode) + ",");
                }

                string json = result.ToString();

                if (json.EndsWith(","))
                {
                    json = json.Substring(0, json.Length - 1);
                }

                json += "]";

                return(json);
            }
            else
            {
                result.Append("{");

                PropertyInfo[] props = obj.GetType().GetProperties();


                foreach (PropertyInfo prop in props)
                {
                    if (IgnoreProperty(prop))
                    {
                        continue;
                    }

                    if ((mode & JSONSerializationMode.JavascriptObject) == JSONSerializationMode.JavascriptObject)
                    {
                        result.Append("" + prop.Name + ":");
                    }
                    else
                    {
                        result.Append("\"" + prop.Name + "\":");
                    }

                    result.Append(prop.GetValue(obj, null).ToJSON(mode));
                    result.Append(",");
                }

                string json = result.ToString();

                if (json.EndsWith(","))
                {
                    json = json.Substring(0, json.Length - 1);
                }

                json += "}";

                return(json);
            }
        }