Advertisement
Quantumplation

Untitled

Dec 18th, 2011
1,540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1.         public override string ToString()
  2.         {
  3.             StringBuilder sb = new StringBuilder("{ ");
  4.             ToString(sb);
  5.             sb.Append("}");
  6.             return sb.ToString();
  7.         }
  8.  
  9.         private void ToString(StringBuilder sb)
  10.         {
  11.             bool firstDict = true;
  12.             foreach (var pair in _dictionary)
  13.             {
  14.                 if (!firstDict)
  15.                     sb.Append(",");
  16.                 firstDict = false;
  17.                 sb.Append("\"" + pair.Key + "\": ");
  18.  
  19.                 if (pair.Value is string)
  20.                 {
  21.                     sb.Append("\"" + pair.Value.ToString() + "\"");
  22.                 }
  23.                 else if (pair.Value is Dictionary<string, object>)
  24.                 {
  25.                     sb.Append((new DynamicJsonObject(pair.Value as Dictionary<string, object>).ToString()));
  26.                 }
  27.                 else if (pair.Value is ArrayList)
  28.                 {
  29.                     if((pair.Value as ArrayList).Count > 1)
  30.                         sb.Append("[");
  31.                     bool firstAL = true;
  32.                     foreach (var item in pair.Value as ArrayList)
  33.                     {
  34.                         if (!firstAL)
  35.                             sb.Append(",");
  36.                         firstAL = false;
  37.                         if (item is string)
  38.                             sb.Append("\"" + item + "\"");
  39.                         else if (item is IDictionary<string, object>)
  40.                             sb.Append((new DynamicJsonObject(item as Dictionary<string, object>).ToString()));
  41.                         else
  42.                         {
  43.                             sb.Append("ERROR");
  44.                         }
  45.                     }
  46.                     if ((pair.Value as ArrayList).Count > 1)
  47.                         sb.Append("]");
  48.                 }
  49.                 else
  50.                 {
  51.                     sb.Append("ERROR");
  52.                 }
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement