JsonData.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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 bool Remove(object obj)
  641. {
  642. json = null;
  643. if(IsObject)
  644. {
  645. JsonData value = null;
  646. if (inst_object.TryGetValue((string)obj, out value))
  647. return inst_object.Remove((string)obj) && object_list.Remove(new KeyValuePair<string, JsonData>((string)obj, value));
  648. else
  649. throw new KeyNotFoundException("The specified key was not found in the JsonData object.");
  650. }
  651. if(IsArray)
  652. {
  653. return inst_array.Remove(ToJsonData(obj));
  654. }
  655. throw new InvalidOperationException (
  656. "Instance of JsonData is not an object or a list.");
  657. }
  658. public void Clear ()
  659. {
  660. if (IsObject) {
  661. ((IDictionary) this).Clear ();
  662. return;
  663. }
  664. if (IsArray) {
  665. ((IList) this).Clear ();
  666. return;
  667. }
  668. }
  669. public bool Equals (JsonData x)
  670. {
  671. if (x == null)
  672. return false;
  673. if (x.type != this.type)
  674. {
  675. // further check to see if this is a long to int comparison
  676. if ((x.type != JsonType.Int && x.type != JsonType.Long)
  677. || (this.type != JsonType.Int && this.type != JsonType.Long))
  678. {
  679. return false;
  680. }
  681. }
  682. switch (this.type) {
  683. case JsonType.None:
  684. return true;
  685. case JsonType.Object:
  686. return this.inst_object.Equals (x.inst_object);
  687. case JsonType.Array:
  688. return this.inst_array.Equals (x.inst_array);
  689. case JsonType.String:
  690. return this.inst_string.Equals (x.inst_string);
  691. case JsonType.Int:
  692. {
  693. if (x.IsLong)
  694. {
  695. if (x.inst_long < Int32.MinValue || x.inst_long > Int32.MaxValue)
  696. return false;
  697. return this.inst_int.Equals((int)x.inst_long);
  698. }
  699. return this.inst_int.Equals(x.inst_int);
  700. }
  701. case JsonType.Long:
  702. {
  703. if (x.IsInt)
  704. {
  705. if (this.inst_long < Int32.MinValue || this.inst_long > Int32.MaxValue)
  706. return false;
  707. return x.inst_int.Equals((int)this.inst_long);
  708. }
  709. return this.inst_long.Equals(x.inst_long);
  710. }
  711. case JsonType.Double:
  712. return this.inst_double.Equals (x.inst_double);
  713. case JsonType.Boolean:
  714. return this.inst_boolean.Equals (x.inst_boolean);
  715. }
  716. return false;
  717. }
  718. public JsonType GetJsonType ()
  719. {
  720. return type;
  721. }
  722. public void SetJsonType (JsonType type)
  723. {
  724. if (this.type == type)
  725. return;
  726. switch (type) {
  727. case JsonType.None:
  728. break;
  729. case JsonType.Object:
  730. inst_object = new Dictionary<string, JsonData> ();
  731. object_list = new List<KeyValuePair<string, JsonData>> ();
  732. break;
  733. case JsonType.Array:
  734. inst_array = new List<JsonData> ();
  735. break;
  736. case JsonType.String:
  737. inst_string = default (String);
  738. break;
  739. case JsonType.Int:
  740. inst_int = default (Int32);
  741. break;
  742. case JsonType.Long:
  743. inst_long = default (Int64);
  744. break;
  745. case JsonType.Double:
  746. inst_double = default (Double);
  747. break;
  748. case JsonType.Boolean:
  749. inst_boolean = default (Boolean);
  750. break;
  751. }
  752. this.type = type;
  753. }
  754. public string ToJson ()
  755. {
  756. if (json != null)
  757. return json;
  758. StringWriter sw = new StringWriter ();
  759. JsonWriter writer = new JsonWriter (sw);
  760. writer.Validate = false;
  761. WriteJson (this, writer);
  762. json = sw.ToString ();
  763. return json;
  764. }
  765. public void ToJson (JsonWriter writer)
  766. {
  767. bool old_validate = writer.Validate;
  768. writer.Validate = false;
  769. WriteJson (this, writer);
  770. writer.Validate = old_validate;
  771. }
  772. public override string ToString ()
  773. {
  774. switch (type) {
  775. case JsonType.Array:
  776. return "JsonData array";
  777. case JsonType.Boolean:
  778. return inst_boolean.ToString ();
  779. case JsonType.Double:
  780. return inst_double.ToString ();
  781. case JsonType.Int:
  782. return inst_int.ToString ();
  783. case JsonType.Long:
  784. return inst_long.ToString ();
  785. case JsonType.Object:
  786. return "JsonData object";
  787. case JsonType.String:
  788. return inst_string;
  789. }
  790. return "Uninitialized JsonData";
  791. }
  792. }
  793. internal class OrderedDictionaryEnumerator : IDictionaryEnumerator
  794. {
  795. IEnumerator<KeyValuePair<string, JsonData>> list_enumerator;
  796. public object Current {
  797. get { return Entry; }
  798. }
  799. public DictionaryEntry Entry {
  800. get {
  801. KeyValuePair<string, JsonData> curr = list_enumerator.Current;
  802. return new DictionaryEntry (curr.Key, curr.Value);
  803. }
  804. }
  805. public object Key {
  806. get { return list_enumerator.Current.Key; }
  807. }
  808. public object Value {
  809. get { return list_enumerator.Current.Value; }
  810. }
  811. public OrderedDictionaryEnumerator (
  812. IEnumerator<KeyValuePair<string, JsonData>> enumerator)
  813. {
  814. list_enumerator = enumerator;
  815. }
  816. public bool MoveNext ()
  817. {
  818. return list_enumerator.MoveNext ();
  819. }
  820. public void Reset ()
  821. {
  822. list_enumerator.Reset ();
  823. }
  824. }
  825. }