JsonData.cs 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. #region Header
  2. /**
  3. * JsonData.cs
  4. * Generic type to hold JSON data (objects, arrays, and so on). This is
  5. * the default type returned by JsonMapper.ToObject().
  6. *
  7. * The authors disclaim copyright to this source code. For more details, see
  8. * the COPYING file included with this distribution.
  9. **/
  10. #endregion
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.Collections.Specialized;
  15. using System.IO;
  16. namespace LitJson
  17. {
  18. public class JsonData : IJsonWrapper, IEquatable<JsonData>
  19. {
  20. #region Fields
  21. private IList<JsonData> inst_array;
  22. private bool inst_boolean;
  23. private double inst_double;
  24. private int inst_int;
  25. private long inst_long;
  26. private IDictionary<string, JsonData> inst_object;
  27. private string inst_string;
  28. private string json;
  29. private JsonType type;
  30. // Used to implement the IOrderedDictionary interface
  31. private IList<KeyValuePair<string, JsonData>> object_list;
  32. #endregion
  33. #region Properties
  34. public int Count {
  35. get { return EnsureCollection ().Count; }
  36. }
  37. public bool IsArray {
  38. get { return type == JsonType.Array; }
  39. }
  40. public bool IsBoolean {
  41. get { return type == JsonType.Boolean; }
  42. }
  43. public bool IsDouble {
  44. get { return type == JsonType.Double; }
  45. }
  46. public bool IsInt {
  47. get { return type == JsonType.Int; }
  48. }
  49. public bool IsLong {
  50. get { return type == JsonType.Long; }
  51. }
  52. public bool IsObject {
  53. get { return type == JsonType.Object; }
  54. }
  55. public bool IsString {
  56. get { return type == JsonType.String; }
  57. }
  58. public ICollection<string> Keys {
  59. get { EnsureDictionary (); return inst_object.Keys; }
  60. }
  61. /// <summary>
  62. /// Determines whether the json contains an element that has the specified key.
  63. /// </summary>
  64. /// <param name="key">The key to locate in the json.</param>
  65. /// <returns>true if the json contains an element that has the specified key; otherwise, false.</returns>
  66. public Boolean ContainsKey(String key) {
  67. EnsureDictionary();
  68. return this.inst_object.Keys.Contains(key);
  69. }
  70. #endregion
  71. #region ICollection Properties
  72. int ICollection.Count {
  73. get {
  74. return Count;
  75. }
  76. }
  77. bool ICollection.IsSynchronized {
  78. get {
  79. return EnsureCollection ().IsSynchronized;
  80. }
  81. }
  82. object ICollection.SyncRoot {
  83. get {
  84. return EnsureCollection ().SyncRoot;
  85. }
  86. }
  87. #endregion
  88. #region IDictionary Properties
  89. bool IDictionary.IsFixedSize {
  90. get {
  91. return EnsureDictionary ().IsFixedSize;
  92. }
  93. }
  94. bool IDictionary.IsReadOnly {
  95. get {
  96. return EnsureDictionary ().IsReadOnly;
  97. }
  98. }
  99. ICollection IDictionary.Keys {
  100. get {
  101. EnsureDictionary ();
  102. IList<string> keys = new List<string> ();
  103. foreach (KeyValuePair<string, JsonData> entry in
  104. object_list) {
  105. keys.Add (entry.Key);
  106. }
  107. return (ICollection) keys;
  108. }
  109. }
  110. ICollection IDictionary.Values {
  111. get {
  112. EnsureDictionary ();
  113. IList<JsonData> values = new List<JsonData> ();
  114. foreach (KeyValuePair<string, JsonData> entry in
  115. object_list) {
  116. values.Add (entry.Value);
  117. }
  118. return (ICollection) values;
  119. }
  120. }
  121. #endregion
  122. #region IJsonWrapper Properties
  123. bool IJsonWrapper.IsArray {
  124. get { return IsArray; }
  125. }
  126. bool IJsonWrapper.IsBoolean {
  127. get { return IsBoolean; }
  128. }
  129. bool IJsonWrapper.IsDouble {
  130. get { return IsDouble; }
  131. }
  132. bool IJsonWrapper.IsInt {
  133. get { return IsInt; }
  134. }
  135. bool IJsonWrapper.IsLong {
  136. get { return IsLong; }
  137. }
  138. bool IJsonWrapper.IsObject {
  139. get { return IsObject; }
  140. }
  141. bool IJsonWrapper.IsString {
  142. get { return IsString; }
  143. }
  144. #endregion
  145. #region IList Properties
  146. bool IList.IsFixedSize {
  147. get {
  148. return EnsureList ().IsFixedSize;
  149. }
  150. }
  151. bool IList.IsReadOnly {
  152. get {
  153. return EnsureList ().IsReadOnly;
  154. }
  155. }
  156. #endregion
  157. #region IDictionary Indexer
  158. object IDictionary.this[object key] {
  159. get {
  160. return EnsureDictionary ()[key];
  161. }
  162. set {
  163. if (! (key is String))
  164. throw new ArgumentException (
  165. "The key has to be a string");
  166. JsonData data = ToJsonData (value);
  167. this[(string) key] = data;
  168. }
  169. }
  170. #endregion
  171. #region IOrderedDictionary Indexer
  172. object IOrderedDictionary.this[int idx] {
  173. get {
  174. EnsureDictionary ();
  175. return object_list[idx].Value;
  176. }
  177. set {
  178. EnsureDictionary ();
  179. JsonData data = ToJsonData (value);
  180. KeyValuePair<string, JsonData> old_entry = object_list[idx];
  181. inst_object[old_entry.Key] = data;
  182. KeyValuePair<string, JsonData> entry =
  183. new KeyValuePair<string, JsonData> (old_entry.Key, data);
  184. object_list[idx] = entry;
  185. }
  186. }
  187. #endregion
  188. #region IList Indexer
  189. object IList.this[int index] {
  190. get {
  191. return EnsureList ()[index];
  192. }
  193. set {
  194. EnsureList ();
  195. JsonData data = ToJsonData (value);
  196. this[index] = data;
  197. }
  198. }
  199. #endregion
  200. #region Public Indexers
  201. public JsonData this[string prop_name] {
  202. get {
  203. EnsureDictionary ();
  204. return inst_object[prop_name];
  205. }
  206. set {
  207. EnsureDictionary ();
  208. KeyValuePair<string, JsonData> entry =
  209. new KeyValuePair<string, JsonData> (prop_name, value);
  210. if (inst_object.ContainsKey (prop_name)) {
  211. for (int i = 0; i < object_list.Count; i++) {
  212. if (object_list[i].Key == prop_name) {
  213. object_list[i] = entry;
  214. break;
  215. }
  216. }
  217. } else
  218. object_list.Add (entry);
  219. inst_object[prop_name] = value;
  220. json = null;
  221. }
  222. }
  223. public JsonData this[int index] {
  224. get {
  225. EnsureCollection ();
  226. if (type == JsonType.Array)
  227. return inst_array[index];
  228. return object_list[index].Value;
  229. }
  230. set {
  231. EnsureCollection ();
  232. if (type == JsonType.Array)
  233. inst_array[index] = value;
  234. else {
  235. KeyValuePair<string, JsonData> entry = object_list[index];
  236. KeyValuePair<string, JsonData> new_entry =
  237. new KeyValuePair<string, JsonData> (entry.Key, value);
  238. object_list[index] = new_entry;
  239. inst_object[entry.Key] = value;
  240. }
  241. json = null;
  242. }
  243. }
  244. #endregion
  245. #region Constructors
  246. public JsonData ()
  247. {
  248. }
  249. public JsonData (bool boolean)
  250. {
  251. type = JsonType.Boolean;
  252. inst_boolean = boolean;
  253. }
  254. public JsonData (double number)
  255. {
  256. type = JsonType.Double;
  257. inst_double = number;
  258. }
  259. public JsonData (int number)
  260. {
  261. type = JsonType.Int;
  262. inst_int = number;
  263. }
  264. public JsonData (long number)
  265. {
  266. type = JsonType.Long;
  267. inst_long = number;
  268. }
  269. public JsonData (object obj)
  270. {
  271. if (obj is Boolean) {
  272. type = JsonType.Boolean;
  273. inst_boolean = (bool) obj;
  274. return;
  275. }
  276. if (obj is Double) {
  277. type = JsonType.Double;
  278. inst_double = (double) obj;
  279. return;
  280. }
  281. if (obj is Int32) {
  282. type = JsonType.Int;
  283. inst_int = (int) obj;
  284. return;
  285. }
  286. if (obj is Int64) {
  287. type = JsonType.Long;
  288. inst_long = (long) obj;
  289. return;
  290. }
  291. if (obj is String) {
  292. type = JsonType.String;
  293. inst_string = (string) obj;
  294. return;
  295. }
  296. throw new ArgumentException (
  297. "Unable to wrap the given object with JsonData");
  298. }
  299. public JsonData (string str)
  300. {
  301. type = JsonType.String;
  302. inst_string = str;
  303. }
  304. #endregion
  305. #region Implicit Conversions
  306. public static implicit operator JsonData (Boolean data)
  307. {
  308. return new JsonData (data);
  309. }
  310. public static implicit operator JsonData (Double data)
  311. {
  312. return new JsonData (data);
  313. }
  314. public static implicit operator JsonData (Int32 data)
  315. {
  316. return new JsonData (data);
  317. }
  318. public static implicit operator JsonData (Int64 data)
  319. {
  320. return new JsonData (data);
  321. }
  322. public static implicit operator JsonData (String data)
  323. {
  324. return new JsonData (data);
  325. }
  326. #endregion
  327. #region Explicit Conversions
  328. public static explicit operator Boolean (JsonData data)
  329. {
  330. if (data.type != JsonType.Boolean)
  331. throw new InvalidCastException (
  332. "Instance of JsonData doesn't hold a double");
  333. return data.inst_boolean;
  334. }
  335. public static explicit operator Double (JsonData data)
  336. {
  337. if (data.type != JsonType.Double)
  338. throw new InvalidCastException (
  339. "Instance of JsonData doesn't hold a double");
  340. return data.inst_double;
  341. }
  342. public static explicit operator Int32(JsonData data)
  343. {
  344. if (data.type != JsonType.Int && data.type != JsonType.Long)
  345. {
  346. throw new InvalidCastException(
  347. "Instance of JsonData doesn't hold an int");
  348. }
  349. // cast may truncate data... but that's up to the user to consider
  350. return data.type == JsonType.Int ? data.inst_int : (int)data.inst_long;
  351. }
  352. public static explicit operator Int64(JsonData data)
  353. {
  354. if (data.type != JsonType.Long && data.type != JsonType.Int)
  355. {
  356. throw new InvalidCastException(
  357. "Instance of JsonData doesn't hold a long");
  358. }
  359. return data.type == JsonType.Long ? data.inst_long : data.inst_int;
  360. }
  361. public static explicit operator String (JsonData data)
  362. {
  363. if (data.type != JsonType.String)
  364. throw new InvalidCastException (
  365. "Instance of JsonData doesn't hold a string");
  366. return data.inst_string;
  367. }
  368. #endregion
  369. #region ICollection Methods
  370. void ICollection.CopyTo (Array array, int index)
  371. {
  372. EnsureCollection ().CopyTo (array, index);
  373. }
  374. #endregion
  375. #region IDictionary Methods
  376. void IDictionary.Add (object key, object value)
  377. {
  378. JsonData data = ToJsonData (value);
  379. EnsureDictionary ().Add (key, data);
  380. KeyValuePair<string, JsonData> entry =
  381. new KeyValuePair<string, JsonData> ((string) key, data);
  382. object_list.Add (entry);
  383. json = null;
  384. }
  385. void IDictionary.Clear ()
  386. {
  387. EnsureDictionary ().Clear ();
  388. object_list.Clear ();
  389. json = null;
  390. }
  391. bool IDictionary.Contains (object key)
  392. {
  393. return EnsureDictionary ().Contains (key);
  394. }
  395. IDictionaryEnumerator IDictionary.GetEnumerator ()
  396. {
  397. return ((IOrderedDictionary) this).GetEnumerator ();
  398. }
  399. void IDictionary.Remove (object key)
  400. {
  401. EnsureDictionary ().Remove (key);
  402. for (int i = 0; i < object_list.Count; i++) {
  403. if (object_list[i].Key == (string) key) {
  404. object_list.RemoveAt (i);
  405. break;
  406. }
  407. }
  408. json = null;
  409. }
  410. #endregion
  411. #region IEnumerable Methods
  412. IEnumerator IEnumerable.GetEnumerator ()
  413. {
  414. return EnsureCollection ().GetEnumerator ();
  415. }
  416. #endregion
  417. #region IJsonWrapper Methods
  418. bool IJsonWrapper.GetBoolean ()
  419. {
  420. if (type != JsonType.Boolean)
  421. throw new InvalidOperationException (
  422. "JsonData instance doesn't hold a boolean");
  423. return inst_boolean;
  424. }
  425. double IJsonWrapper.GetDouble ()
  426. {
  427. if (type != JsonType.Double)
  428. throw new InvalidOperationException (
  429. "JsonData instance doesn't hold a double");
  430. return inst_double;
  431. }
  432. int IJsonWrapper.GetInt ()
  433. {
  434. if (type != JsonType.Int)
  435. throw new InvalidOperationException (
  436. "JsonData instance doesn't hold an int");
  437. return inst_int;
  438. }
  439. long IJsonWrapper.GetLong ()
  440. {
  441. if (type != JsonType.Long)
  442. throw new InvalidOperationException (
  443. "JsonData instance doesn't hold a long");
  444. return inst_long;
  445. }
  446. string IJsonWrapper.GetString ()
  447. {
  448. if (type != JsonType.String)
  449. throw new InvalidOperationException (
  450. "JsonData instance doesn't hold a string");
  451. return inst_string;
  452. }
  453. void IJsonWrapper.SetBoolean (bool val)
  454. {
  455. type = JsonType.Boolean;
  456. inst_boolean = val;
  457. json = null;
  458. }
  459. void IJsonWrapper.SetDouble (double val)
  460. {
  461. type = JsonType.Double;
  462. inst_double = val;
  463. json = null;
  464. }
  465. void IJsonWrapper.SetInt (int val)
  466. {
  467. type = JsonType.Int;
  468. inst_int = val;
  469. json = null;
  470. }
  471. void IJsonWrapper.SetLong (long val)
  472. {
  473. type = JsonType.Long;
  474. inst_long = val;
  475. json = null;
  476. }
  477. void IJsonWrapper.SetString (string val)
  478. {
  479. type = JsonType.String;
  480. inst_string = val;
  481. json = null;
  482. }
  483. string IJsonWrapper.ToJson ()
  484. {
  485. return ToJson ();
  486. }
  487. void IJsonWrapper.ToJson (JsonWriter writer)
  488. {
  489. ToJson (writer);
  490. }
  491. #endregion
  492. #region IList Methods
  493. int IList.Add (object value)
  494. {
  495. return Add (value);
  496. }
  497. void IList.Clear ()
  498. {
  499. EnsureList ().Clear ();
  500. json = null;
  501. }
  502. bool IList.Contains (object value)
  503. {
  504. return EnsureList ().Contains (value);
  505. }
  506. int IList.IndexOf (object value)
  507. {
  508. return EnsureList ().IndexOf (value);
  509. }
  510. void IList.Insert (int index, object value)
  511. {
  512. EnsureList ().Insert (index, value);
  513. json = null;
  514. }
  515. void IList.Remove (object value)
  516. {
  517. EnsureList ().Remove (value);
  518. json = null;
  519. }
  520. void IList.RemoveAt (int index)
  521. {
  522. EnsureList ().RemoveAt (index);
  523. json = null;
  524. }
  525. #endregion
  526. #region IOrderedDictionary Methods
  527. IDictionaryEnumerator IOrderedDictionary.GetEnumerator ()
  528. {
  529. EnsureDictionary ();
  530. return new OrderedDictionaryEnumerator (
  531. object_list.GetEnumerator ());
  532. }
  533. void IOrderedDictionary.Insert (int idx, object key, object value)
  534. {
  535. string property = (string) key;
  536. JsonData data = ToJsonData (value);
  537. this[property] = data;
  538. KeyValuePair<string, JsonData> entry =
  539. new KeyValuePair<string, JsonData> (property, data);
  540. object_list.Insert (idx, entry);
  541. }
  542. void IOrderedDictionary.RemoveAt (int idx)
  543. {
  544. EnsureDictionary ();
  545. inst_object.Remove (object_list[idx].Key);
  546. object_list.RemoveAt (idx);
  547. }
  548. #endregion
  549. #region Private Methods
  550. private ICollection EnsureCollection ()
  551. {
  552. if (type == JsonType.Array)
  553. return (ICollection) inst_array;
  554. if (type == JsonType.Object)
  555. return (ICollection) inst_object;
  556. throw new InvalidOperationException (
  557. "The JsonData instance has to be initialized first");
  558. }
  559. private IDictionary EnsureDictionary ()
  560. {
  561. if (type == JsonType.Object)
  562. return (IDictionary) inst_object;
  563. if (type != JsonType.None)
  564. throw new InvalidOperationException (
  565. "Instance of JsonData is not a dictionary");
  566. type = JsonType.Object;
  567. inst_object = new Dictionary<string, JsonData> ();
  568. object_list = new List<KeyValuePair<string, JsonData>> ();
  569. return (IDictionary) inst_object;
  570. }
  571. private IList EnsureList ()
  572. {
  573. if (type == JsonType.Array)
  574. return (IList) inst_array;
  575. if (type != JsonType.None)
  576. throw new InvalidOperationException (
  577. "Instance of JsonData is not a list");
  578. type = JsonType.Array;
  579. inst_array = new List<JsonData> ();
  580. return (IList) inst_array;
  581. }
  582. private JsonData ToJsonData (object obj)
  583. {
  584. if (obj == null)
  585. return null;
  586. if (obj is JsonData)
  587. return (JsonData) obj;
  588. return new JsonData (obj);
  589. }
  590. private static void WriteJson (IJsonWrapper obj, JsonWriter writer)
  591. {
  592. if (obj == null) {
  593. writer.Write (null);
  594. return;
  595. }
  596. if (obj.IsString) {
  597. writer.Write (obj.GetString ());
  598. return;
  599. }
  600. if (obj.IsBoolean) {
  601. writer.Write (obj.GetBoolean ());
  602. return;
  603. }
  604. if (obj.IsDouble) {
  605. writer.Write (obj.GetDouble ());
  606. return;
  607. }
  608. if (obj.IsInt) {
  609. writer.Write (obj.GetInt ());
  610. return;
  611. }
  612. if (obj.IsLong) {
  613. writer.Write (obj.GetLong ());
  614. return;
  615. }
  616. if (obj.IsArray) {
  617. writer.WriteArrayStart ();
  618. foreach (object elem in (IList) obj)
  619. WriteJson ((JsonData) elem, writer);
  620. writer.WriteArrayEnd ();
  621. return;
  622. }
  623. if (obj.IsObject) {
  624. writer.WriteObjectStart ();
  625. foreach (DictionaryEntry entry in ((IDictionary) obj)) {
  626. writer.WritePropertyName ((string) entry.Key);
  627. WriteJson ((JsonData) entry.Value, writer);
  628. }
  629. writer.WriteObjectEnd ();
  630. return;
  631. }
  632. }
  633. #endregion
  634. public int Add (object value)
  635. {
  636. JsonData data = ToJsonData (value);
  637. json = null;
  638. return EnsureList ().Add (data);
  639. }
  640. public JsonData AddToIList(object value)
  641. {
  642. this.Add(value);
  643. return this;
  644. }
  645. public bool Remove(object obj)
  646. {
  647. json = null;
  648. if(IsObject)
  649. {
  650. JsonData value = null;
  651. if (inst_object.TryGetValue((string)obj, out value))
  652. return inst_object.Remove((string)obj) && object_list.Remove(new KeyValuePair<string, JsonData>((string)obj, value));
  653. else
  654. throw new KeyNotFoundException("The specified key was not found in the JsonData object.");
  655. }
  656. if(IsArray)
  657. {
  658. return inst_array.Remove(ToJsonData(obj));
  659. }
  660. throw new InvalidOperationException (
  661. "Instance of JsonData is not an object or a list.");
  662. }
  663. public void Clear ()
  664. {
  665. if (IsObject) {
  666. ((IDictionary) this).Clear ();
  667. return;
  668. }
  669. if (IsArray) {
  670. ((IList) this).Clear ();
  671. return;
  672. }
  673. }
  674. public bool Equals (JsonData x)
  675. {
  676. if (x == null)
  677. return false;
  678. if (x.type != this.type)
  679. {
  680. // further check to see if this is a long to int comparison
  681. if ((x.type != JsonType.Int && x.type != JsonType.Long)
  682. || (this.type != JsonType.Int && this.type != JsonType.Long))
  683. {
  684. return false;
  685. }
  686. }
  687. switch (this.type) {
  688. case JsonType.None:
  689. return true;
  690. case JsonType.Object:
  691. return this.inst_object.Equals (x.inst_object);
  692. case JsonType.Array:
  693. return this.inst_array.Equals (x.inst_array);
  694. case JsonType.String:
  695. return this.inst_string.Equals (x.inst_string);
  696. case JsonType.Int:
  697. {
  698. if (x.IsLong)
  699. {
  700. if (x.inst_long < Int32.MinValue || x.inst_long > Int32.MaxValue)
  701. return false;
  702. return this.inst_int.Equals((int)x.inst_long);
  703. }
  704. return this.inst_int.Equals(x.inst_int);
  705. }
  706. case JsonType.Long:
  707. {
  708. if (x.IsInt)
  709. {
  710. if (this.inst_long < Int32.MinValue || this.inst_long > Int32.MaxValue)
  711. return false;
  712. return x.inst_int.Equals((int)this.inst_long);
  713. }
  714. return this.inst_long.Equals(x.inst_long);
  715. }
  716. case JsonType.Double:
  717. return this.inst_double.Equals (x.inst_double);
  718. case JsonType.Boolean:
  719. return this.inst_boolean.Equals (x.inst_boolean);
  720. }
  721. return false;
  722. }
  723. public JsonType GetJsonType ()
  724. {
  725. return type;
  726. }
  727. public void SetJsonType (JsonType type)
  728. {
  729. if (this.type == type)
  730. return;
  731. switch (type) {
  732. case JsonType.None:
  733. break;
  734. case JsonType.Object:
  735. inst_object = new Dictionary<string, JsonData> ();
  736. object_list = new List<KeyValuePair<string, JsonData>> ();
  737. break;
  738. case JsonType.Array:
  739. inst_array = new List<JsonData> ();
  740. break;
  741. case JsonType.String:
  742. inst_string = default (String);
  743. break;
  744. case JsonType.Int:
  745. inst_int = default (Int32);
  746. break;
  747. case JsonType.Long:
  748. inst_long = default (Int64);
  749. break;
  750. case JsonType.Double:
  751. inst_double = default (Double);
  752. break;
  753. case JsonType.Boolean:
  754. inst_boolean = default (Boolean);
  755. break;
  756. }
  757. this.type = type;
  758. }
  759. public string ToJson ()
  760. {
  761. if (json != null)
  762. return json;
  763. StringWriter sw = new StringWriter ();
  764. JsonWriter writer = new JsonWriter (sw);
  765. writer.Validate = false;
  766. WriteJson (this, writer);
  767. json = sw.ToString ();
  768. return json;
  769. }
  770. public void ToJson (JsonWriter writer)
  771. {
  772. bool old_validate = writer.Validate;
  773. writer.Validate = false;
  774. WriteJson (this, writer);
  775. writer.Validate = old_validate;
  776. }
  777. public override string ToString ()
  778. {
  779. switch (type) {
  780. case JsonType.Array:
  781. return "JsonData array";
  782. case JsonType.Boolean:
  783. return inst_boolean.ToString ();
  784. case JsonType.Double:
  785. return inst_double.ToString ();
  786. case JsonType.Int:
  787. return inst_int.ToString ();
  788. case JsonType.Long:
  789. return inst_long.ToString ();
  790. case JsonType.Object:
  791. return "JsonData object";
  792. case JsonType.String:
  793. return inst_string;
  794. }
  795. return "Uninitialized JsonData";
  796. }
  797. }
  798. internal class OrderedDictionaryEnumerator : IDictionaryEnumerator
  799. {
  800. IEnumerator<KeyValuePair<string, JsonData>> list_enumerator;
  801. public object Current {
  802. get { return Entry; }
  803. }
  804. public DictionaryEntry Entry {
  805. get {
  806. KeyValuePair<string, JsonData> curr = list_enumerator.Current;
  807. return new DictionaryEntry (curr.Key, curr.Value);
  808. }
  809. }
  810. public object Key {
  811. get { return list_enumerator.Current.Key; }
  812. }
  813. public object Value {
  814. get { return list_enumerator.Current.Value; }
  815. }
  816. public OrderedDictionaryEnumerator (
  817. IEnumerator<KeyValuePair<string, JsonData>> enumerator)
  818. {
  819. list_enumerator = enumerator;
  820. }
  821. public bool MoveNext ()
  822. {
  823. return list_enumerator.MoveNext ();
  824. }
  825. public void Reset ()
  826. {
  827. list_enumerator.Reset ();
  828. }
  829. }
  830. }