PageRenderTime 1650ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/JSON@CodeTitans/Objects/JSonDictionary.cs

#
C# | 502 lines | 359 code | 101 blank | 42 comment | 24 complexity | b0e9df0c94b07322db4c77376fc107ed MD5 | raw file
  1. #region License
  2. /*
  3. Copyright (c) 2010, Paweł Hofman (CodeTitans)
  4. All Rights Reserved.
  5. Licensed under the Apache License version 2.0.
  6. For more information please visit:
  7. http://codetitans.codeplex.com/license
  8. or
  9. http://www.apache.org/licenses/
  10. For latest source code, documentation, samples
  11. and more information please visit:
  12. http://codetitans.codeplex.com/
  13. */
  14. #endregion
  15. using System;
  16. using System.Collections.Generic;
  17. namespace CodeTitans.JSon.Objects
  18. {
  19. /// <summary>
  20. /// Internal wrapper class that describes JSON object and provides <see cref="IJSonObject"/> access interface.
  21. /// </summary>
  22. internal class JSonDictionary : IJSonObject, IJSonWritable
  23. {
  24. private readonly Dictionary<String, IJSonObject> _data;
  25. /// <summary>
  26. /// Init constructor.
  27. /// </summary>
  28. public JSonDictionary(Dictionary<String, Object> data)
  29. {
  30. _data = new Dictionary<String, IJSonObject>();
  31. foreach (KeyValuePair<String, Object> d in data)
  32. _data.Add(d.Key, (IJSonObject)d.Value);
  33. }
  34. /// <summary>
  35. /// Default constructor for inheriting classes.
  36. /// </summary>
  37. protected JSonDictionary(Boolean mutable)
  38. {
  39. if (!mutable)
  40. throw new ArgumentOutOfRangeException("mutable", "Invalid value, use a different constructor if immutable");
  41. _data = new Dictionary<String, IJSonObject>();
  42. }
  43. /// <summary>
  44. /// Private constructor for cloning only.
  45. /// </summary>
  46. private JSonDictionary(Dictionary<String, IJSonObject> data)
  47. {
  48. _data = new Dictionary<String, IJSonObject>(data);
  49. }
  50. /// <summary>
  51. /// Gets or sets the value of internal data.
  52. /// </summary>
  53. protected internal Dictionary<String, IJSonObject> Data
  54. {
  55. get { return _data; }
  56. }
  57. #region IJSonObject Members
  58. string IJSonObject.StringValue
  59. {
  60. get { throw new InvalidOperationException(); }
  61. }
  62. int IJSonObject.Int32Value
  63. {
  64. get { throw new InvalidOperationException(); }
  65. }
  66. uint IJSonObject.UInt32Value
  67. {
  68. get { throw new InvalidOperationException(); }
  69. }
  70. long IJSonObject.Int64Value
  71. {
  72. get { throw new InvalidOperationException(); }
  73. }
  74. ulong IJSonObject.UInt64Value
  75. {
  76. get { throw new InvalidOperationException(); }
  77. }
  78. float IJSonObject.SingleValue
  79. {
  80. get { throw new InvalidOperationException(); }
  81. }
  82. double IJSonObject.DoubleValue
  83. {
  84. get { throw new InvalidOperationException(); }
  85. }
  86. public decimal DecimalValue
  87. {
  88. get { throw new InvalidOperationException(); }
  89. }
  90. DateTime IJSonObject.DateTimeValue
  91. {
  92. get { throw new InvalidOperationException(); }
  93. }
  94. TimeSpan IJSonObject.TimeSpanValue
  95. {
  96. get { throw new InvalidOperationException(); }
  97. }
  98. bool IJSonObject.BooleanValue
  99. {
  100. get { throw new InvalidOperationException(); }
  101. }
  102. Guid IJSonObject.GuidValue
  103. {
  104. get { throw new InvalidOperationException(); }
  105. }
  106. bool IJSonObject.IsNull
  107. {
  108. get { return false; }
  109. }
  110. bool IJSonObject.IsTrue
  111. {
  112. get { return false; }
  113. }
  114. bool IJSonObject.IsFalse
  115. {
  116. get { return true; }
  117. }
  118. bool IJSonObject.IsEnumerable
  119. {
  120. get { return true; }
  121. }
  122. public bool IsArray
  123. {
  124. get { return false; }
  125. }
  126. object IJSonObject.ObjectValue
  127. {
  128. get
  129. {
  130. Dictionary<string, object> result = new Dictionary<string, object>();
  131. foreach (KeyValuePair<string, IJSonObject> v in _data)
  132. {
  133. result.Add(v.Key, v.Value.ObjectValue);
  134. }
  135. return result;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the DateTime value for given JSON object.
  140. /// </summary>
  141. public DateTime ToDateTimeValue(JSonDateTimeKind kind)
  142. {
  143. throw new InvalidOperationException();
  144. }
  145. /// <summary>
  146. /// Gets the value of given JSON object.
  147. /// </summary>
  148. object IJSonObject.ToValue(Type t)
  149. {
  150. return JSonObjectConverter.ToObject(this, t);
  151. }
  152. /// <summary>
  153. /// Get the value of given JSON object.
  154. /// </summary>
  155. T IJSonObject.ToObjectValue<T>()
  156. {
  157. return (T)JSonObjectConverter.ToObject(this, typeof(T));
  158. }
  159. int IJSonObject.Length
  160. {
  161. get { return _data.Count; }
  162. }
  163. IJSonObject IJSonObject.this[int index]
  164. {
  165. get
  166. {
  167. if (index < 0 || index >= _data.Keys.Count)
  168. throw new IndexOutOfRangeException("Invalid key index");
  169. foreach(var key in _data.Keys)
  170. {
  171. if (index == 0)
  172. return _data[key];
  173. index--;
  174. }
  175. // shouldn't happen anymore:
  176. throw new InvalidOperationException();
  177. }
  178. }
  179. int IJSonObject.Count
  180. {
  181. get { return _data.Count; }
  182. }
  183. IJSonObject IJSonObject.this[string name]
  184. {
  185. get { return _data[name]; }
  186. }
  187. IJSonObject IJSonObject.this[String name, IJSonObject defaultValue]
  188. {
  189. get
  190. {
  191. IJSonObject result;
  192. if (_data.TryGetValue(name, out result))
  193. return result;
  194. if (defaultValue != null)
  195. return defaultValue;
  196. return null;
  197. }
  198. }
  199. IJSonObject IJSonObject.this[String name, String defaultValue]
  200. {
  201. get
  202. {
  203. IJSonObject result;
  204. if (_data.TryGetValue(name, out result))
  205. return result;
  206. return new JSonStringObject(defaultValue);
  207. }
  208. }
  209. IJSonObject IJSonObject.this[String name, String defaultValue, Boolean asJSonSerializedObject]
  210. {
  211. get
  212. {
  213. IJSonObject result;
  214. if (_data.TryGetValue(name, out result))
  215. return result;
  216. if (!asJSonSerializedObject)
  217. return new JSonStringObject(defaultValue);
  218. JSonReader reader = new JSonReader(defaultValue);
  219. return reader.ReadAsJSonObject();
  220. }
  221. }
  222. IJSonObject IJSonObject.this[String name, Int32 defaultValue]
  223. {
  224. get
  225. {
  226. IJSonObject result;
  227. if (_data.TryGetValue(name, out result))
  228. return result;
  229. return new JSonDecimalInt32Object(defaultValue);
  230. }
  231. }
  232. IJSonObject IJSonObject.this[string name, uint defaultValue]
  233. {
  234. get
  235. {
  236. IJSonObject result;
  237. if (_data.TryGetValue(name, out result))
  238. return result;
  239. return new JSonDecimalInt32Object((Int32)defaultValue);
  240. }
  241. }
  242. IJSonObject IJSonObject.this[String name, Int64 defaultValue]
  243. {
  244. get
  245. {
  246. IJSonObject result;
  247. if (_data.TryGetValue(name, out result))
  248. return result;
  249. return new JSonDecimalInt64Object(defaultValue);
  250. }
  251. }
  252. IJSonObject IJSonObject.this[string name, ulong defaultValue]
  253. {
  254. get
  255. {
  256. IJSonObject result;
  257. if (_data.TryGetValue(name, out result))
  258. return result;
  259. return new JSonDecimalInt64Object((Int64)defaultValue);
  260. }
  261. }
  262. IJSonObject IJSonObject.this[String name, Single defaultValue]
  263. {
  264. get
  265. {
  266. IJSonObject result;
  267. if (_data.TryGetValue(name, out result))
  268. return result;
  269. return new JSonDecimalSingleObject(defaultValue);
  270. }
  271. }
  272. IJSonObject IJSonObject.this[String name, Double defaultValue]
  273. {
  274. get
  275. {
  276. IJSonObject result;
  277. if (_data.TryGetValue(name, out result))
  278. return result;
  279. return new JSonDecimalDoubleObject(defaultValue);
  280. }
  281. }
  282. public IJSonObject this[string name, decimal defaultValue]
  283. {
  284. get
  285. {
  286. IJSonObject result;
  287. if (_data.TryGetValue(name, out result))
  288. return result;
  289. return new JSonDecimalDecimalObject(defaultValue);
  290. }
  291. }
  292. IJSonObject IJSonObject.this[String name, DateTime defaultValue]
  293. {
  294. get
  295. {
  296. IJSonObject result;
  297. if (_data.TryGetValue(name, out result))
  298. return result;
  299. return new JSonDecimalInt64Object(defaultValue, JSonDateTimeKind.Default);
  300. }
  301. }
  302. IJSonObject IJSonObject.this[String name, DateTime defaultValue, JSonDateTimeKind kind]
  303. {
  304. get
  305. {
  306. IJSonObject result;
  307. if (_data.TryGetValue(name, out result))
  308. return result;
  309. return new JSonDecimalInt64Object(defaultValue, kind);
  310. }
  311. }
  312. IJSonObject IJSonObject.this[String name, TimeSpan defaultValue]
  313. {
  314. get
  315. {
  316. IJSonObject result;
  317. if (_data.TryGetValue(name, out result))
  318. return result;
  319. return new JSonDecimalInt64Object(defaultValue);
  320. }
  321. }
  322. IJSonObject IJSonObject.this[String name, Guid defaultValue]
  323. {
  324. get
  325. {
  326. IJSonObject result;
  327. if (_data.TryGetValue(name, out result))
  328. return result;
  329. return new JSonStringObject(defaultValue);
  330. }
  331. }
  332. IJSonObject IJSonObject.this[String name, Boolean defaultValue]
  333. {
  334. get
  335. {
  336. IJSonObject result;
  337. if (_data.TryGetValue(name, out result))
  338. return result;
  339. return new JSonBooleanObject(defaultValue);
  340. }
  341. }
  342. bool IJSonObject.Contains(string name)
  343. {
  344. return _data.ContainsKey(name);
  345. }
  346. ICollection<string> IJSonObject.Names
  347. {
  348. get { return _data.Keys; }
  349. }
  350. IEnumerable<KeyValuePair<string, IJSonObject>> IJSonObject.ObjectItems
  351. {
  352. get { return _data; }
  353. }
  354. bool IJSonObject.IsMutable
  355. {
  356. get { return false; }
  357. }
  358. IJSonMutableObject IJSonObject.AsMutable
  359. {
  360. get { return (IJSonMutableObject)this; }
  361. }
  362. IJSonMutableObject IJSonObject.CreateMutableClone()
  363. {
  364. return JSonMutableObject.CreateDictionary(this);
  365. }
  366. IJSonObject IJSonObject.CreateImmutableClone()
  367. {
  368. return new JSonDictionary(_data);
  369. }
  370. public string ToString(string format)
  371. {
  372. bool indent = JSonObjectConverter.GetIndentAndVerifyToStringFormat(format);
  373. using (var writer = new JSonWriter(indent))
  374. {
  375. writer.CompactEnumerables = format == JSonObjectConverter.CompactEnumerables;
  376. writer.Write(_data);
  377. return writer.ToString();
  378. }
  379. }
  380. IEnumerable<IJSonObject> IJSonObject.ArrayItems
  381. {
  382. get { return _data.Values; }
  383. }
  384. #endregion
  385. #region IJSonWritable Members
  386. void IJSonWritable.Write(IJSonWriter output)
  387. {
  388. output.Write(_data);
  389. }
  390. #endregion
  391. public override string ToString()
  392. {
  393. return ToString(null);
  394. }
  395. }
  396. }