Dynamic Json Object


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:04 p.m.

FORMAT: Text only

SIZE: 7.5 kB

HITS: 1078

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using System.Text;
  7. using System.IO;
  8. using System.Dynamic;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using Newtonsoft.Json.Serialization;
  12. ///////////////////////////////////////////////////////////////////////
  13. // Needs JSon.Net (Newtonsoft.Json.dll) from http://json.codeplex.com/
  14. //////////////////////////////////////////////////////////////////////
  15. namespace JsonUtils
  16. {
  17. /// <summary>
  18. /// Creates a dynamic object
  19. /// Methods that can be used on arrays: foreach, ToArray(), ToList(), Count, Length
  20. /// </summary>
  21. public class JsonObject : DynamicObject, IEnumerable, IEnumerator
  22. {
  23. object _object;
  24. JsonObject(object jObject)
  25. {
  26. this._object = jObject;
  27. }
  28. public static dynamic GetDynamicJsonObject(byte[] buf)
  29. {
  30. return GetDynamicJsonObject(buf, Encoding.UTF8);
  31. }
  32. public static dynamic GetDynamicJsonObject(byte[] buf, Encoding encoding)
  33. {
  34. return GetDynamicJsonObject(encoding.GetString(buf));
  35. }
  36. public static dynamic GetDynamicJsonObject(string json)
  37. {
  38. object o = JsonConvert.DeserializeObject(json);
  39. return new JsonUtils.JsonObject(o);
  40. }
  41. internal static dynamic GetDynamicJsonObject(JObject jObj)
  42. {
  43. return new JsonUtils.JsonObject(jObj);
  44. }
  45. public object this[string s]
  46. {
  47. get
  48. {
  49. JObject jObject = _object as JObject;
  50. object obj = jObject.SelectToken(s);
  51. if (obj == null) return true;
  52. if (obj is JValue)
  53. return GetValue(obj);
  54. else
  55. return new JsonObject(obj);
  56. }
  57. }
  58. public object this[int i]
  59. {
  60. get
  61. {
  62. if (!(_object is JArray)) return null;
  63. object obj = (_object as JArray)[i];
  64. if (obj is JValue)
  65. {
  66. return GetValue(obj);
  67. }
  68. return new JsonObject(obj);
  69. }
  70. }
  71. public override bool TryGetMember(GetMemberBinder binder, out object result)
  72. {
  73. result = null;
  74. if (_object is JArray)
  75. {
  76. JArray jArray = _object as JArray;
  77. switch (binder.Name)
  78. {
  79. case "Length":
  80. case "Count": result = jArray.Count; break;
  81. case "ToList": result = (Func<List<string>>)(() => jArray.Values().Select(x => x.ToString()).ToList()); break;
  82. case "ToArray": result = (Func<string[]>)(() => jArray.Values().Select(x => x.ToString()).ToArray()); break;
  83. }
  84. return true;
  85. }
  86. JObject jObject = _object as JObject;
  87. object obj = jObject.SelectToken(binder.Name);
  88. if (obj == null) return true;
  89. if (obj is JValue)
  90. result = GetValue(obj);
  91. else
  92. result = new JsonObject(obj);
  93. return true;
  94. }
  95. object GetValue(object obj)
  96. {
  97. string val = ((JValue)obj).ToString();
  98. int resInt; double resDouble; DateTime resDateTime;
  99. if (int.TryParse(val, out resInt)) return resInt;
  100. if (DateTime.TryParse(val, out resDateTime)) return resDateTime;
  101. if (double.TryParse(val, out resDouble)) return resDouble;
  102. return val;
  103. }
  104. public override string ToString()
  105. {
  106. return _object.ToString();
  107. }
  108. int _index = -1;
  109. public IEnumerator GetEnumerator()
  110. {
  111. _index = -1;
  112. return this;
  113. }
  114. public object Current
  115. {
  116. get
  117. {
  118. if (!(_object is JArray)) return null;
  119. object obj = (_object as JArray)[_index];
  120. if (obj is JValue) return GetValue(obj);
  121. return new JsonObject(obj);
  122. }
  123. }
  124. public bool MoveNext()
  125. {
  126. if (!(_object is JArray)) return false;
  127. _index++;
  128. return _index < (_object as JArray).Count;
  129. }
  130. public void Reset()
  131. {
  132. throw new NotImplementedException();
  133. }
  134. }
  135. public class XmlObject
  136. {
  137. public static dynamic GetDynamicJsonObject(string xmlString)
  138. {
  139. var xmlDoc = XDocument.Load(new StringReader(xmlString));
  140. return JsonObject.GetDynamicJsonObject(XmlToJObject(xmlDoc.Root));
  141. }
  142. public static dynamic GetDynamicJsonObject(Stream xmlStream)
  143. {
  144. var xmlDoc = XDocument.Load(xmlStream);
  145. return JsonObject.GetDynamicJsonObject(XmlToJObject(xmlDoc.Root));
  146. }
  147. static JObject XmlToJObject(XElement node)
  148. {
  149. JObject jObj = new JObject();
  150. foreach (var attr in node.Attributes())
  151. {
  152. jObj.Add(attr.Name.LocalName, attr.Value);
  153. }
  154. foreach (var childs in node.Elements().GroupBy(x => x.Name.LocalName))
  155. {
  156. string name = childs.ElementAt(0).Name.LocalName;
  157. if (childs.Count() > 1)
  158. {
  159. JArray jArray = new JArray();
  160. foreach (var child in childs)
  161. {
  162. jArray.Add(XmlToJObject(child));
  163. }
  164. jObj.Add(name, jArray);
  165. }
  166. else
  167. {
  168. jObj.Add(name, XmlToJObject(childs.ElementAt(0)));
  169. }
  170. }
  171. node.Elements().Remove();
  172. if (!String.IsNullOrEmpty(node.Value))
  173. {
  174. string name = "Value";
  175. while (jObj[name] != null) name = "_" + name;
  176. jObj.Add(name, node.Value);
  177. }
  178. return jObj;
  179. }
  180. }
  181. }
  182. namespace System
  183. {
  184. public static class JsonExtensions
  185. {
  186. public static JsonUtils.JsonObject GetDynamicJsonObject(this Uri uri)
  187. {
  188. using(Net.WebClient wc = new Net.WebClient())
  189. {
  190. wc.Encoding = System.Text.Encoding.UTF8;
  191. wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
  192. return JsonUtils.JsonObject.GetDynamicJsonObject(wc.DownloadString(uri.ToString()));
  193. }
  194. }
  195. }
  196. }

comments powered by Disqus