Json.Serialization.JsonFormatter.ToJson C# (CSharp) Method

ToJson() public method

Serializes an object to a string
Object definition must have the [Serializable] attribute. Serializes only members which are not marked with [NonSerialized] attribute.
public ToJson ( object o ) : string
o object Object to be serialized
return string
        public string ToJson(object o)
        {
            string JsonString = "{";
            //WriteString(stm, "{");

            Type ot = o.GetType();
            FieldInfo[] fis = ot.GetFields();
            

            //int n = fis.Length;
            bool hasStarted = false;
            foreach (FieldInfo fi in fis)
            {
                object val = fi.GetValue(o);

                if (val == null) continue;
                Type ft = fi.FieldType;
                //if (!ft.IsSerializable) continue;

                if (hasStarted) JsonString += ", ";
                hasStarted = true;
                JsonString += "\"" + fi.Name + "\": ";
                
                if (ft == typeof(Guid) || ft == typeof(string))
                {
                    JsonString += "\"" + val.ToString() + "\"";
                }
                else 
                {
                    if (ft == typeof(Hashtable))
                    {
                        JsonString += "{ ";
                        Hashtable ht = (Hashtable)val;
                        IEnumerator ie = ht.GetEnumerator();
                        bool isNext = false;
                        while (ie.MoveNext())
                        {
                            DictionaryEntry de = (DictionaryEntry) ie.Current;
                            if (isNext)
                            {
                                JsonString += ", ";
                            }
                            else
                            {
                                isNext = true;
                            }
                            JsonString += "\"" + de.Key + "\": \"" + de.Value + "\"";
                        }

                        JsonString += " }";
                    }
                    else
                    {
                        if (ft.IsEnum)
                        {
                            string ss = val.ToString();
                            
                            JsonString += "\"" + ss + "\"";
                        }
                        else
                        {
                            if (ft.IsArray)
                            {
                                JsonString += "[";
                                bool IsNext = false;

                                Array arr = (Array)val;
                                foreach (object ov in arr)
                                {
                                    if (IsNext)
                                    {
                                        JsonString += ", ";
                                    }
                                    else
                                    {
                                        IsNext = true;
                                    }
                                    JsonString += ToJson(ov);
                                }
                                JsonString += "]";
                            }
                            else
                            {
                                if (ft == typeof(int) || ft == typeof(float) || ft == typeof(double))
                                {
                                    JsonString += val.ToString();
                                }
                                else
                                {
                                    if (_objectHash.Contains(val.GetHashCode()))
                                    {
                                        throw new InvalidOperationException("Cyclic loop in object structure!");
                                    }
                                    _objectHash.Add(val.GetHashCode());
                                    JsonString += ToJson(val);
                                }
                            }
                        }
                    }
                }
            }
            JsonString += "}";
            return JsonString;
        }

Usage Example

Exemplo n.º 1
0
        private WebResponse MakeRequest(string url, object obj, string Method, string id, string key)
        {
            //lock (this)
            //{
            Debug.Print("Request: " + url);
            using (HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest)
            {

                req.Method = Method;
                req.ContentType = ContentType;
                req.KeepAlive = false;
                req.Headers.Add(IdHeader, id);
                req.Headers.Add(KeyHeader, key);
                req.ReadWriteTimeout = AsyncTimeout;
                req.Timeout = AsyncTimeout;
                if (obj != null)
                {
                    JsonFormatter js = new JsonFormatter();

                    string s = js.ToJson(obj);
                    req.ContentLength = s.Length;
                    using (Stream rs = req.GetRequestStream())
                    {
                        rs.Write(Encoding.UTF8.GetBytes(s), 0, s.Length);
                        rs.Close();
                    }
                }

                HttpWebResponse resp = GetResponseAsync(req); //(HttpWebResponse)req.GetResponse(); //
                Debug.Print("Done. Response code = " + resp.StatusCode.ToString());
                return resp;
            }
          
        }