FineNerds

JsonParser

Oct 23rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. #region JSONConverter
  2.     public sealed class DynamicJsonConverter : JavaScriptConverter
  3.     {
  4.         public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
  5.         {
  6.             if (dictionary == null)
  7.                 throw new ArgumentNullException("dictionary");
  8.  
  9.             return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
  10.         }
  11.  
  12.         public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
  13.         {
  14.             throw new NotImplementedException();
  15.         }
  16.  
  17.         public override IEnumerable<Type> SupportedTypes
  18.         {
  19.             get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
  20.         }
  21.  
  22.         #region Nested type: DynamicJsonObject
  23.  
  24.         public sealed class DynamicJsonObject : DynamicObject
  25.         {
  26.             public readonly IDictionary<string, object> _dictionary;
  27.  
  28.             public DynamicJsonObject(IDictionary<string, object> dictionary)
  29.             {
  30.                 if (dictionary == null)
  31.                     throw new ArgumentNullException("dictionary");
  32.                 _dictionary = dictionary;
  33.             }
  34.  
  35.             public override string ToString()
  36.             {
  37.                 var sb = new StringBuilder("{");
  38.                 ToString(sb);
  39.                 return sb.ToString();
  40.             }
  41.  
  42.             public void ToString(StringBuilder sb)
  43.             {
  44.                 var firstInDictionary = true;
  45.                 foreach (var pair in _dictionary)
  46.                 {
  47.                     if (!firstInDictionary)
  48.                         sb.Append(",");
  49.                     firstInDictionary = false;
  50.                     var value = pair.Value;
  51.                     var name = pair.Key;
  52.                     if (value is string)
  53.                     {
  54.                         sb.AppendFormat("{0}:\"{1}\"", name, value);
  55.                     }
  56.                     else if (value is IDictionary<string, object>)
  57.                     {
  58.                         new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
  59.                     }
  60.                     else if (value is ArrayList)
  61.                     {
  62.                         sb.Append(name + ":[");
  63.                         var firstInArray = true;
  64.                         foreach (var arrayValue in (ArrayList)value)
  65.                         {
  66.                             if (!firstInArray)
  67.                                 sb.Append(",");
  68.                             firstInArray = false;
  69.                             if (arrayValue is IDictionary<string, object>)
  70.                                 new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
  71.                             else if (arrayValue is string)
  72.                                 sb.AppendFormat("\"{0}\"", arrayValue);
  73.                             else
  74.                                 sb.AppendFormat("{0}", arrayValue);
  75.  
  76.                         }
  77.                         sb.Append("]");
  78.                     }
  79.                     else
  80.                     {
  81.                         sb.AppendFormat("{0}:{1}", name, value);
  82.                     }
  83.                 }
  84.                 sb.Append("}");
  85.             }
  86.  
  87.             public override bool TryGetMember(GetMemberBinder binder, out object result)
  88.             {
  89.                 if (!_dictionary.TryGetValue(binder.Name, out result))
  90.                 {
  91.                     // return null to avoid exception.  caller can check for null this way...
  92.                     result = null;
  93.                     return true;
  94.                 }
  95.  
  96.                 result = WrapResultObject(result);
  97.                 return true;
  98.             }
  99.  
  100.             public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
  101.             {
  102.                 if (indexes.Length == 1 && indexes[0] != null)
  103.                 {
  104.                     if (!_dictionary.TryGetValue(indexes[0].ToString(), out result))
  105.                     {
  106.                         // return null to avoid exception.  caller can check for null this way...
  107.                         result = null;
  108.                         return true;
  109.                     }
  110.  
  111.                     result = WrapResultObject(result);
  112.                     return true;
  113.                 }
  114.  
  115.                 return base.TryGetIndex(binder, indexes, out result);
  116.             }
  117.  
  118.             public static object WrapResultObject(object result)
  119.             {
  120.                 var dictionary = result as IDictionary<string, object>;
  121.                 if (dictionary != null)
  122.                     return new DynamicJsonObject(dictionary);
  123.  
  124.                 var arrayList = result as ArrayList;
  125.                 if (arrayList != null && arrayList.Count > 0)
  126.                 {
  127.                     return arrayList[0] is IDictionary<string, object>
  128.                         ? new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)))
  129.                         : new List<object>(arrayList.Cast<object>());
  130.                 }
  131.  
  132.                 return result;
  133.             }
  134.         }
  135.  
  136.         #endregion
  137.     }
  138.     #endregion
Advertisement
Add Comment
Please, Sign In to add comment