JsonData.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 is JsonData && (obj as JsonData).type == JsonType.None)
  593. {
  594. (obj as JsonData).EnsureDictionary();
  595. }
  596. if (obj == null) {
  597. writer.Write (null);
  598. return;
  599. }
  600. if (obj.IsString) {
  601. writer.Write (obj.GetString ());
  602. return;
  603. }
  604. if (obj.IsBoolean) {
  605. writer.Write (obj.GetBoolean ());
  606. return;
  607. }
  608. if (obj.IsDouble) {
  609. writer.Write (obj.GetDouble ());
  610. return;
  611. }
  612. if (obj.IsInt) {
  613. writer.Write (obj.GetInt ());
  614. return;
  615. }
  616. if (obj.IsLong) {
  617. writer.Write (obj.GetLong ());
  618. return;
  619. }
  620. if (obj.IsArray) {
  621. writer.WriteArrayStart ();
  622. foreach (object elem in (IList) obj)
  623. WriteJson ((JsonData) elem, writer);
  624. writer.WriteArrayEnd ();
  625. return;
  626. }
  627. if (obj.IsObject) {
  628. writer.WriteObjectStart ();
  629. foreach (DictionaryEntry entry in ((IDictionary) obj)) {
  630. writer.WritePropertyName ((string) entry.Key);
  631. WriteJson ((JsonData) entry.Value, writer);
  632. }
  633. writer.WriteObjectEnd ();
  634. return;
  635. }
  636. }
  637. #endregion
  638. public int Add (object value)
  639. {
  640. JsonData data = ToJsonData (value);
  641. json = null;
  642. return EnsureList ().Add (data);
  643. }
  644. public void Clear ()
  645. {
  646. if (IsObject) {
  647. ((IDictionary) this).Clear ();
  648. return;
  649. }
  650. if (IsArray) {
  651. ((IList) this).Clear ();
  652. return;
  653. }
  654. }
  655. public bool Equals (JsonData x)
  656. {
  657. if (x == null)
  658. return false;
  659. if (x.type != this.type)
  660. {
  661. // further check to see if this is a long to int comparison
  662. if ((x.type != JsonType.Int && x.type != JsonType.Long)
  663. || (this.type != JsonType.Int && this.type != JsonType.Long))
  664. {
  665. return false;
  666. }
  667. }
  668. switch (this.type) {
  669. case JsonType.None:
  670. return true;
  671. case JsonType.Object:
  672. return this.inst_object.Equals (x.inst_object);
  673. case JsonType.Array:
  674. return this.inst_array.Equals (x.inst_array);
  675. case JsonType.String:
  676. return this.inst_string.Equals (x.inst_string);
  677. case JsonType.Int:
  678. {
  679. if (x.IsLong)
  680. {
  681. if (x.inst_long < Int32.MinValue || x.inst_long > Int32.MaxValue)
  682. return false;
  683. return this.inst_int.Equals((int)x.inst_long);
  684. }
  685. return this.inst_int.Equals(x.inst_int);
  686. }
  687. case JsonType.Long:
  688. {
  689. if (x.IsInt)
  690. {
  691. if (this.inst_long < Int32.MinValue || this.inst_long > Int32.MaxValue)
  692. return false;
  693. return x.inst_int.Equals((int)this.inst_long);
  694. }
  695. return this.inst_long.Equals(x.inst_long);
  696. }
  697. case JsonType.Double:
  698. return this.inst_double.Equals (x.inst_double);
  699. case JsonType.Boolean:
  700. return this.inst_boolean.Equals (x.inst_boolean);
  701. }
  702. return false;
  703. }
  704. public JsonType GetJsonType ()
  705. {
  706. return type;
  707. }
  708. public void SetJsonType (JsonType type)
  709. {
  710. if (this.type == type)
  711. return;
  712. switch (type) {
  713. case JsonType.None:
  714. break;
  715. case JsonType.Object:
  716. inst_object = new Dictionary<string, JsonData> ();
  717. object_list = new List<KeyValuePair<string, JsonData>> ();
  718. break;
  719. case JsonType.Array:
  720. inst_array = new List<JsonData> ();
  721. break;
  722. case JsonType.String:
  723. inst_string = default (String);
  724. break;
  725. case JsonType.Int:
  726. inst_int = default (Int32);
  727. break;
  728. case JsonType.Long:
  729. inst_long = default (Int64);
  730. break;
  731. case JsonType.Double:
  732. inst_double = default (Double);
  733. break;
  734. case JsonType.Boolean:
  735. inst_boolean = default (Boolean);
  736. break;
  737. }
  738. this.type = type;
  739. }
  740. public string ToJson ()
  741. {
  742. if (json != null)
  743. return json;
  744. StringWriter sw = new StringWriter ();
  745. JsonWriter writer = new JsonWriter (sw);
  746. writer.Validate = false;
  747. WriteJson (this, writer);
  748. json = sw.ToString ();
  749. return json;
  750. }
  751. public void ToJson (JsonWriter writer)
  752. {
  753. bool old_validate = writer.Validate;
  754. writer.Validate = false;
  755. WriteJson (this, writer);
  756. writer.Validate = old_validate;
  757. }
  758. public override string ToString ()
  759. {
  760. switch (type) {
  761. case JsonType.Array:
  762. return "JsonData array";
  763. case JsonType.Boolean:
  764. return inst_boolean.ToString ();
  765. case JsonType.Double:
  766. return inst_double.ToString ();
  767. case JsonType.Int:
  768. return inst_int.ToString ();
  769. case JsonType.Long:
  770. return inst_long.ToString ();
  771. case JsonType.Object:
  772. return "JsonData object";
  773. case JsonType.String:
  774. return inst_string;
  775. }
  776. return "Uninitialized JsonData";
  777. }
  778. }
  779. internal class OrderedDictionaryEnumerator : IDictionaryEnumerator
  780. {
  781. IEnumerator<KeyValuePair<string, JsonData>> list_enumerator;
  782. public object Current {
  783. get { return Entry; }
  784. }
  785. public DictionaryEntry Entry {
  786. get {
  787. KeyValuePair<string, JsonData> curr = list_enumerator.Current;
  788. return new DictionaryEntry (curr.Key, curr.Value);
  789. }
  790. }
  791. public object Key {
  792. get { return list_enumerator.Current.Key; }
  793. }
  794. public object Value {
  795. get { return list_enumerator.Current.Value; }
  796. }
  797. public OrderedDictionaryEnumerator (
  798. IEnumerator<KeyValuePair<string, JsonData>> enumerator)
  799. {
  800. list_enumerator = enumerator;
  801. }
  802. public bool MoveNext ()
  803. {
  804. return list_enumerator.MoveNext ();
  805. }
  806. public void Reset ()
  807. {
  808. list_enumerator.Reset ();
  809. }
  810. }
  811. }