JsonData.cs 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Globalization;
  6. using System.IO;
  7. using UnityEngine;
  8. namespace LitJson
  9. {
  10. public class JsonData : IEquatable<JsonData>, IList, IOrderedDictionary, IEnumerable, ICollection, IDictionary, IJsonWrapper
  11. {
  12. public JsonData()
  13. {
  14. }
  15. public JsonData(bool boolean)
  16. {
  17. this.type = JsonType.Boolean;
  18. this.inst_boolean = boolean;
  19. }
  20. public JsonData(double number)
  21. {
  22. this.type = JsonType.Double;
  23. this.inst_double = number;
  24. }
  25. public JsonData(int number)
  26. {
  27. this.type = JsonType.Int;
  28. this.inst_int = number;
  29. }
  30. public JsonData(long number)
  31. {
  32. this.type = JsonType.Long;
  33. this.inst_long = number;
  34. }
  35. public JsonData(object obj)
  36. {
  37. if (obj is bool)
  38. {
  39. this.type = JsonType.Boolean;
  40. this.inst_boolean = (bool)obj;
  41. return;
  42. }
  43. if (obj is double)
  44. {
  45. this.type = JsonType.Double;
  46. this.inst_double = (double)obj;
  47. return;
  48. }
  49. if (obj is int)
  50. {
  51. this.type = JsonType.Int;
  52. this.inst_int = (int)obj;
  53. return;
  54. }
  55. if (obj is long)
  56. {
  57. this.type = JsonType.Long;
  58. this.inst_long = (long)obj;
  59. return;
  60. }
  61. if (obj is string)
  62. {
  63. this.type = JsonType.String;
  64. this.inst_string = (string)obj;
  65. return;
  66. }
  67. throw new ArgumentException("Unable to wrap the given object with JsonData");
  68. }
  69. public JsonData(string str)
  70. {
  71. this.type = JsonType.String;
  72. this.inst_string = str;
  73. }
  74. public JsonData(Vector2 vector2)
  75. {
  76. this.SetJsonType(JsonType.Object);
  77. this.inst_object["x"] = (double)vector2.x;
  78. this.inst_object["y"] = (double)vector2.y;
  79. }
  80. public JsonData(Vector3 vector3)
  81. {
  82. this.SetJsonType(JsonType.Object);
  83. this.inst_object["x"] = (double)vector3.x;
  84. this.inst_object["y"] = (double)vector3.y;
  85. this.inst_object["z"] = (double)vector3.z;
  86. }
  87. public JsonData(Vector4 vector4)
  88. {
  89. this.SetJsonType(JsonType.Object);
  90. this.inst_object["x"] = (double)vector4.x;
  91. this.inst_object["y"] = (double)vector4.y;
  92. this.inst_object["z"] = (double)vector4.z;
  93. this.inst_object["w"] = (double)vector4.w;
  94. }
  95. public JsonData(Quaternion quaternion)
  96. {
  97. this.SetJsonType(JsonType.Object);
  98. this.inst_object["x"] = (double)quaternion.x;
  99. this.inst_object["y"] = (double)quaternion.y;
  100. this.inst_object["z"] = (double)quaternion.z;
  101. this.inst_object["w"] = (double)quaternion.w;
  102. }
  103. public JsonData(Rect rect)
  104. {
  105. this.SetJsonType(JsonType.Object);
  106. this.inst_object["x"] = (double)rect.x;
  107. this.inst_object["y"] = (double)rect.y;
  108. this.inst_object["w"] = (double)rect.width;
  109. this.inst_object["h"] = (double)rect.height;
  110. }
  111. public int Count
  112. {
  113. get
  114. {
  115. return this.EnsureCollection().Count;
  116. }
  117. }
  118. public bool IsArray
  119. {
  120. get
  121. {
  122. return this.type == JsonType.Array;
  123. }
  124. }
  125. public bool IsBoolean
  126. {
  127. get
  128. {
  129. return this.type == JsonType.Boolean;
  130. }
  131. }
  132. public bool IsDouble
  133. {
  134. get
  135. {
  136. return this.type == JsonType.Double;
  137. }
  138. }
  139. public bool IsInt
  140. {
  141. get
  142. {
  143. return this.type == JsonType.Int;
  144. }
  145. }
  146. public bool IsLong
  147. {
  148. get
  149. {
  150. return this.type == JsonType.Long;
  151. }
  152. }
  153. public bool IsObject
  154. {
  155. get
  156. {
  157. return this.type == JsonType.Object;
  158. }
  159. }
  160. public bool IsString
  161. {
  162. get
  163. {
  164. return this.type == JsonType.String;
  165. }
  166. }
  167. public ICollection<string> Keys
  168. {
  169. get
  170. {
  171. this.EnsureDictionary();
  172. return this.inst_object.Keys;
  173. }
  174. }
  175. int ICollection.Count
  176. {
  177. get
  178. {
  179. return this.Count;
  180. }
  181. }
  182. bool ICollection.IsSynchronized
  183. {
  184. get
  185. {
  186. return this.EnsureCollection().IsSynchronized;
  187. }
  188. }
  189. object ICollection.SyncRoot
  190. {
  191. get
  192. {
  193. return this.EnsureCollection().SyncRoot;
  194. }
  195. }
  196. bool IDictionary.IsFixedSize
  197. {
  198. get
  199. {
  200. return this.EnsureDictionary().IsFixedSize;
  201. }
  202. }
  203. bool IDictionary.IsReadOnly
  204. {
  205. get
  206. {
  207. return this.EnsureDictionary().IsReadOnly;
  208. }
  209. }
  210. ICollection IDictionary.Keys
  211. {
  212. get
  213. {
  214. this.EnsureDictionary();
  215. IList<string> list = new List<string>();
  216. foreach (KeyValuePair<string, JsonData> keyValuePair in this.object_list)
  217. {
  218. list.Add(keyValuePair.Key);
  219. }
  220. return (ICollection)list;
  221. }
  222. }
  223. ICollection IDictionary.Values
  224. {
  225. get
  226. {
  227. this.EnsureDictionary();
  228. IList<JsonData> list = new List<JsonData>();
  229. foreach (KeyValuePair<string, JsonData> keyValuePair in this.object_list)
  230. {
  231. list.Add(keyValuePair.Value);
  232. }
  233. return (ICollection)list;
  234. }
  235. }
  236. bool IJsonWrapper.IsArray
  237. {
  238. get
  239. {
  240. return this.IsArray;
  241. }
  242. }
  243. bool IJsonWrapper.IsBoolean
  244. {
  245. get
  246. {
  247. return this.IsBoolean;
  248. }
  249. }
  250. bool IJsonWrapper.IsDouble
  251. {
  252. get
  253. {
  254. return this.IsDouble;
  255. }
  256. }
  257. bool IJsonWrapper.IsInt
  258. {
  259. get
  260. {
  261. return this.IsInt;
  262. }
  263. }
  264. bool IJsonWrapper.IsLong
  265. {
  266. get
  267. {
  268. return this.IsLong;
  269. }
  270. }
  271. bool IJsonWrapper.IsObject
  272. {
  273. get
  274. {
  275. return this.IsObject;
  276. }
  277. }
  278. bool IJsonWrapper.IsString
  279. {
  280. get
  281. {
  282. return this.IsString;
  283. }
  284. }
  285. bool IList.IsFixedSize
  286. {
  287. get
  288. {
  289. return this.EnsureList().IsFixedSize;
  290. }
  291. }
  292. bool IList.IsReadOnly
  293. {
  294. get
  295. {
  296. return this.EnsureList().IsReadOnly;
  297. }
  298. }
  299. object IDictionary.this[object key]
  300. {
  301. get
  302. {
  303. return this.EnsureDictionary()[key];
  304. }
  305. set
  306. {
  307. if (!(key is string))
  308. {
  309. throw new ArgumentException("The key has to be a string");
  310. }
  311. JsonData value2 = this.ToJsonData(value);
  312. this[(string)key] = value2;
  313. }
  314. }
  315. object IOrderedDictionary.this[int idx]
  316. {
  317. get
  318. {
  319. this.EnsureDictionary();
  320. return this.object_list[idx].Value;
  321. }
  322. set
  323. {
  324. this.EnsureDictionary();
  325. JsonData value2 = this.ToJsonData(value);
  326. KeyValuePair<string, JsonData> keyValuePair = this.object_list[idx];
  327. this.inst_object[keyValuePair.Key] = value2;
  328. KeyValuePair<string, JsonData> value3 = new KeyValuePair<string, JsonData>(keyValuePair.Key, value2);
  329. this.object_list[idx] = value3;
  330. }
  331. }
  332. object IList.this[int index]
  333. {
  334. get
  335. {
  336. return this.EnsureList()[index];
  337. }
  338. set
  339. {
  340. this.EnsureList();
  341. JsonData value2 = this.ToJsonData(value);
  342. this[index] = value2;
  343. }
  344. }
  345. public JsonData this[string prop_name]
  346. {
  347. get
  348. {
  349. this.EnsureDictionary();
  350. return this.inst_object[prop_name];
  351. }
  352. set
  353. {
  354. this.EnsureDictionary();
  355. KeyValuePair<string, JsonData> keyValuePair = new KeyValuePair<string, JsonData>(prop_name, value);
  356. if (this.inst_object.ContainsKey(prop_name))
  357. {
  358. for (int i = 0; i < this.object_list.Count; i++)
  359. {
  360. if (this.object_list[i].Key == prop_name)
  361. {
  362. this.object_list[i] = keyValuePair;
  363. break;
  364. }
  365. }
  366. }
  367. else
  368. {
  369. this.object_list.Add(keyValuePair);
  370. }
  371. this.inst_object[prop_name] = value;
  372. this.json = null;
  373. }
  374. }
  375. public JsonData this[int index]
  376. {
  377. get
  378. {
  379. this.EnsureCollection();
  380. if (this.type == JsonType.Array)
  381. {
  382. return this.inst_array[index];
  383. }
  384. return this.object_list[index].Value;
  385. }
  386. set
  387. {
  388. this.EnsureCollection();
  389. if (this.type == JsonType.Array)
  390. {
  391. this.inst_array[index] = value;
  392. }
  393. else
  394. {
  395. KeyValuePair<string, JsonData> keyValuePair = this.object_list[index];
  396. KeyValuePair<string, JsonData> value2 = new KeyValuePair<string, JsonData>(keyValuePair.Key, value);
  397. this.object_list[index] = value2;
  398. this.inst_object[keyValuePair.Key] = value;
  399. }
  400. this.json = null;
  401. }
  402. }
  403. public static implicit operator JsonData(bool data)
  404. {
  405. return new JsonData(data);
  406. }
  407. public static implicit operator JsonData(double data)
  408. {
  409. return new JsonData(data);
  410. }
  411. public static implicit operator JsonData(int data)
  412. {
  413. return new JsonData(data);
  414. }
  415. public static implicit operator JsonData(long data)
  416. {
  417. return new JsonData(data);
  418. }
  419. public static implicit operator JsonData(string data)
  420. {
  421. return new JsonData(data);
  422. }
  423. public static implicit operator JsonData(Vector2 data)
  424. {
  425. return new JsonData(data);
  426. }
  427. public static implicit operator JsonData(Vector3 data)
  428. {
  429. return new JsonData(data);
  430. }
  431. public static implicit operator JsonData(Vector4 data)
  432. {
  433. return new JsonData(data);
  434. }
  435. public static implicit operator JsonData(Quaternion data)
  436. {
  437. return new JsonData(data);
  438. }
  439. public static implicit operator JsonData(Rect data)
  440. {
  441. return new JsonData(data);
  442. }
  443. public static explicit operator bool(JsonData data)
  444. {
  445. if (data.type != JsonType.Boolean)
  446. {
  447. throw new InvalidCastException("Instance of JsonData doesn't hold a double");
  448. }
  449. return data.inst_boolean;
  450. }
  451. public static explicit operator double(JsonData data)
  452. {
  453. if (data.type != JsonType.Double)
  454. {
  455. throw new InvalidCastException("Instance of JsonData doesn't hold a double");
  456. }
  457. return data.inst_double;
  458. }
  459. public static explicit operator int(JsonData data)
  460. {
  461. if (data.type != JsonType.Int)
  462. {
  463. throw new InvalidCastException("Instance of JsonData doesn't hold an int");
  464. }
  465. return data.inst_int;
  466. }
  467. public static explicit operator long(JsonData data)
  468. {
  469. if (data.type != JsonType.Long)
  470. {
  471. throw new InvalidCastException("Instance of JsonData doesn't hold an int");
  472. }
  473. return data.inst_long;
  474. }
  475. public static explicit operator string(JsonData data)
  476. {
  477. if (data.type != JsonType.String)
  478. {
  479. throw new InvalidCastException("Instance of JsonData doesn't hold a string");
  480. }
  481. return data.inst_string;
  482. }
  483. void ICollection.CopyTo(Array array, int index)
  484. {
  485. this.EnsureCollection().CopyTo(array, index);
  486. }
  487. void IDictionary.Add(object key, object value)
  488. {
  489. JsonData value2 = this.ToJsonData(value);
  490. this.EnsureDictionary().Add(key, value2);
  491. KeyValuePair<string, JsonData> item = new KeyValuePair<string, JsonData>((string)key, value2);
  492. this.object_list.Add(item);
  493. this.json = null;
  494. }
  495. void IDictionary.Clear()
  496. {
  497. this.EnsureDictionary().Clear();
  498. this.object_list.Clear();
  499. this.json = null;
  500. }
  501. bool IDictionary.Contains(object key)
  502. {
  503. return this.EnsureDictionary().Contains(key);
  504. }
  505. IDictionaryEnumerator IDictionary.GetEnumerator()
  506. {
  507. return ((IOrderedDictionary)this).GetEnumerator();
  508. }
  509. void IDictionary.Remove(object key)
  510. {
  511. this.EnsureDictionary().Remove(key);
  512. for (int i = 0; i < this.object_list.Count; i++)
  513. {
  514. if (this.object_list[i].Key == (string)key)
  515. {
  516. this.object_list.RemoveAt(i);
  517. break;
  518. }
  519. }
  520. this.json = null;
  521. }
  522. IEnumerator IEnumerable.GetEnumerator()
  523. {
  524. return this.EnsureCollection().GetEnumerator();
  525. }
  526. bool IJsonWrapper.GetBoolean()
  527. {
  528. if (this.type != JsonType.Boolean)
  529. {
  530. throw new InvalidOperationException("JsonData instance doesn't hold a boolean");
  531. }
  532. return this.inst_boolean;
  533. }
  534. double IJsonWrapper.GetDouble()
  535. {
  536. if (this.type != JsonType.Double)
  537. {
  538. throw new InvalidOperationException("JsonData instance doesn't hold a double");
  539. }
  540. return this.inst_double;
  541. }
  542. int IJsonWrapper.GetInt()
  543. {
  544. if (this.type != JsonType.Int)
  545. {
  546. throw new InvalidOperationException("JsonData instance doesn't hold an int");
  547. }
  548. return this.inst_int;
  549. }
  550. long IJsonWrapper.GetLong()
  551. {
  552. if (this.type != JsonType.Long)
  553. {
  554. throw new InvalidOperationException("JsonData instance doesn't hold a long");
  555. }
  556. return this.inst_long;
  557. }
  558. string IJsonWrapper.GetString()
  559. {
  560. if (this.type != JsonType.String)
  561. {
  562. throw new InvalidOperationException("JsonData instance doesn't hold a string");
  563. }
  564. return this.inst_string;
  565. }
  566. void IJsonWrapper.SetBoolean(bool val)
  567. {
  568. this.type = JsonType.Boolean;
  569. this.inst_boolean = val;
  570. this.json = null;
  571. }
  572. void IJsonWrapper.SetDouble(double val)
  573. {
  574. this.type = JsonType.Double;
  575. this.inst_double = val;
  576. this.json = null;
  577. }
  578. void IJsonWrapper.SetInt(int val)
  579. {
  580. this.type = JsonType.Int;
  581. this.inst_int = val;
  582. this.json = null;
  583. }
  584. void IJsonWrapper.SetLong(long val)
  585. {
  586. this.type = JsonType.Long;
  587. this.inst_long = val;
  588. this.json = null;
  589. }
  590. void IJsonWrapper.SetString(string val)
  591. {
  592. this.type = JsonType.String;
  593. this.inst_string = val;
  594. this.json = null;
  595. }
  596. string IJsonWrapper.ToJson()
  597. {
  598. return this.ToJson();
  599. }
  600. void IJsonWrapper.ToJson(JsonWriter writer)
  601. {
  602. this.ToJson(writer);
  603. }
  604. int IList.Add(object value)
  605. {
  606. return this.Add(value);
  607. }
  608. void IList.Clear()
  609. {
  610. this.EnsureList().Clear();
  611. this.json = null;
  612. }
  613. bool IList.Contains(object value)
  614. {
  615. return this.EnsureList().Contains(value);
  616. }
  617. int IList.IndexOf(object value)
  618. {
  619. return this.EnsureList().IndexOf(value);
  620. }
  621. void IList.Insert(int index, object value)
  622. {
  623. this.EnsureList().Insert(index, value);
  624. this.json = null;
  625. }
  626. void IList.Remove(object value)
  627. {
  628. this.EnsureList().Remove(value);
  629. this.json = null;
  630. }
  631. void IList.RemoveAt(int index)
  632. {
  633. this.EnsureList().RemoveAt(index);
  634. this.json = null;
  635. }
  636. IDictionaryEnumerator IOrderedDictionary.GetEnumerator()
  637. {
  638. this.EnsureDictionary();
  639. return new OrderedDictionaryEnumerator(this.object_list.GetEnumerator());
  640. }
  641. void IOrderedDictionary.Insert(int idx, object key, object value)
  642. {
  643. string text = (string)key;
  644. JsonData value2 = this.ToJsonData(value);
  645. this[text] = value2;
  646. KeyValuePair<string, JsonData> item = new KeyValuePair<string, JsonData>(text, value2);
  647. this.object_list.Insert(idx, item);
  648. }
  649. void IOrderedDictionary.RemoveAt(int idx)
  650. {
  651. this.EnsureDictionary();
  652. this.inst_object.Remove(this.object_list[idx].Key);
  653. this.object_list.RemoveAt(idx);
  654. }
  655. private ICollection EnsureCollection()
  656. {
  657. if (this.type == JsonType.Array)
  658. {
  659. return (ICollection)this.inst_array;
  660. }
  661. if (this.type == JsonType.Object)
  662. {
  663. return (ICollection)this.inst_object;
  664. }
  665. throw new InvalidOperationException("The JsonData instance has to be initialized first");
  666. }
  667. private IDictionary EnsureDictionary()
  668. {
  669. if (this.type == JsonType.Object)
  670. {
  671. return (IDictionary)this.inst_object;
  672. }
  673. if (this.type != JsonType.None)
  674. {
  675. throw new InvalidOperationException("Instance of JsonData is not a dictionary");
  676. }
  677. this.type = JsonType.Object;
  678. this.inst_object = new Dictionary<string, JsonData>();
  679. this.object_list = new List<KeyValuePair<string, JsonData>>();
  680. return (IDictionary)this.inst_object;
  681. }
  682. private IList EnsureList()
  683. {
  684. if (this.type == JsonType.Array)
  685. {
  686. return (IList)this.inst_array;
  687. }
  688. if (this.type != JsonType.None)
  689. {
  690. throw new InvalidOperationException("Instance of JsonData is not a list");
  691. }
  692. this.type = JsonType.Array;
  693. this.inst_array = new List<JsonData>();
  694. return (IList)this.inst_array;
  695. }
  696. private JsonData ToJsonData(object obj)
  697. {
  698. if (obj == null)
  699. {
  700. return null;
  701. }
  702. if (obj is JsonData)
  703. {
  704. return (JsonData)obj;
  705. }
  706. return new JsonData(obj);
  707. }
  708. private static void WriteJson(IJsonWrapper obj, JsonWriter writer)
  709. {
  710. if (obj == null)
  711. {
  712. writer.Write(null);
  713. return;
  714. }
  715. if (obj.IsString)
  716. {
  717. writer.Write(obj.GetString());
  718. return;
  719. }
  720. if (obj.IsBoolean)
  721. {
  722. writer.Write(obj.GetBoolean());
  723. return;
  724. }
  725. if (obj.IsDouble)
  726. {
  727. writer.Write(obj.GetDouble());
  728. return;
  729. }
  730. if (obj.IsInt)
  731. {
  732. writer.Write(obj.GetInt());
  733. return;
  734. }
  735. if (obj.IsLong)
  736. {
  737. writer.Write(obj.GetLong());
  738. return;
  739. }
  740. if (obj.IsArray)
  741. {
  742. writer.WriteArrayStart();
  743. IEnumerator enumerator = obj.GetEnumerator();
  744. try
  745. {
  746. while (enumerator.MoveNext())
  747. {
  748. object obj2 = enumerator.Current;
  749. JsonData.WriteJson((JsonData)obj2, writer);
  750. }
  751. }
  752. finally
  753. {
  754. IDisposable disposable;
  755. if ((disposable = (enumerator as IDisposable)) != null)
  756. {
  757. disposable.Dispose();
  758. }
  759. }
  760. writer.WriteArrayEnd();
  761. return;
  762. }
  763. if (obj.IsObject)
  764. {
  765. writer.WriteObjectStart();
  766. IDictionaryEnumerator enumerator2 = obj.GetEnumerator();
  767. try
  768. {
  769. while (enumerator2.MoveNext())
  770. {
  771. object obj3 = enumerator2.Current;
  772. DictionaryEntry dictionaryEntry = (DictionaryEntry)obj3;
  773. writer.WritePropertyName((string)dictionaryEntry.Key);
  774. JsonData.WriteJson((JsonData)dictionaryEntry.Value, writer);
  775. }
  776. }
  777. finally
  778. {
  779. IDisposable disposable2;
  780. if ((disposable2 = (enumerator2 as IDisposable)) != null)
  781. {
  782. disposable2.Dispose();
  783. }
  784. }
  785. writer.WriteObjectEnd();
  786. return;
  787. }
  788. }
  789. private object Get(object key, object defaultValue)
  790. {
  791. return (!this.Contains(key)) ? defaultValue : this.EnsureDictionary()[key];
  792. }
  793. private object Get(object key)
  794. {
  795. return this.EnsureDictionary()[key];
  796. }
  797. public int Add(object value)
  798. {
  799. JsonData value2 = this.ToJsonData(value);
  800. this.json = null;
  801. return this.EnsureList().Add(value2);
  802. }
  803. public void Clear()
  804. {
  805. if (this.IsObject)
  806. {
  807. ((IDictionary)this).Clear();
  808. return;
  809. }
  810. if (this.IsArray)
  811. {
  812. ((IList)this).Clear();
  813. return;
  814. }
  815. }
  816. public bool Equals(JsonData x)
  817. {
  818. if (x == null)
  819. {
  820. return false;
  821. }
  822. if (x.type != this.type)
  823. {
  824. return false;
  825. }
  826. switch (this.type)
  827. {
  828. case JsonType.None:
  829. return true;
  830. case JsonType.Object:
  831. return this.inst_object.Equals(x.inst_object);
  832. case JsonType.Array:
  833. return this.inst_array.Equals(x.inst_array);
  834. case JsonType.String:
  835. return this.inst_string.Equals(x.inst_string);
  836. case JsonType.Int:
  837. return this.inst_int.Equals(x.inst_int);
  838. case JsonType.Long:
  839. return this.inst_long.Equals(x.inst_long);
  840. case JsonType.Double:
  841. return this.inst_double.Equals(x.inst_double);
  842. case JsonType.Boolean:
  843. return this.inst_boolean.Equals(x.inst_boolean);
  844. default:
  845. return false;
  846. }
  847. }
  848. public JsonType GetJsonType()
  849. {
  850. return this.type;
  851. }
  852. public void SetJsonType(JsonType type)
  853. {
  854. if (this.type == type)
  855. {
  856. return;
  857. }
  858. switch (type)
  859. {
  860. case JsonType.Object:
  861. this.inst_object = new Dictionary<string, JsonData>();
  862. this.object_list = new List<KeyValuePair<string, JsonData>>();
  863. break;
  864. case JsonType.Array:
  865. this.inst_array = new List<JsonData>();
  866. break;
  867. case JsonType.String:
  868. this.inst_string = null;
  869. break;
  870. case JsonType.Int:
  871. this.inst_int = 0;
  872. break;
  873. case JsonType.Long:
  874. this.inst_long = 0L;
  875. break;
  876. case JsonType.Double:
  877. this.inst_double = 0.0;
  878. break;
  879. case JsonType.Boolean:
  880. this.inst_boolean = false;
  881. break;
  882. }
  883. this.type = type;
  884. }
  885. public string ToJson()
  886. {
  887. if (this.json != null)
  888. {
  889. return this.json;
  890. }
  891. StringWriter stringWriter = new StringWriter();
  892. JsonData.WriteJson(this, new JsonWriter(stringWriter)
  893. {
  894. Validate = false
  895. });
  896. this.json = stringWriter.ToString();
  897. return this.json;
  898. }
  899. public void ToJson(JsonWriter writer)
  900. {
  901. bool validate = writer.Validate;
  902. writer.Validate = false;
  903. JsonData.WriteJson(this, writer);
  904. writer.Validate = validate;
  905. }
  906. public override string ToString()
  907. {
  908. switch (this.type)
  909. {
  910. case JsonType.Object:
  911. return "JsonData object";
  912. case JsonType.Array:
  913. return "JsonData array";
  914. case JsonType.String:
  915. return this.inst_string;
  916. case JsonType.Int:
  917. return this.inst_int.ToString();
  918. case JsonType.Long:
  919. return this.inst_long.ToString();
  920. case JsonType.Double:
  921. return this.inst_double.ToString(CultureInfo.InvariantCulture);
  922. case JsonType.Boolean:
  923. return this.inst_boolean.ToString();
  924. default:
  925. return "Uninitialized JsonData";
  926. }
  927. }
  928. public bool Contains(object key)
  929. {
  930. return this.EnsureDictionary().Contains(key);
  931. }
  932. public T Get<T>(int index, T defaultValue = default(T))
  933. {
  934. if (!this.IsArray)
  935. {
  936. return defaultValue;
  937. }
  938. T result;
  939. try
  940. {
  941. result = (T)((object)Convert.ChangeType(this[index].ToString(), typeof(T), CultureInfo.InvariantCulture));
  942. }
  943. catch (Exception message)
  944. {
  945. UnityEngine.Debug.LogError(message);
  946. result = defaultValue;
  947. }
  948. return result;
  949. }
  950. public T Get<T>(string key, T defaultValue = default(T))
  951. {
  952. if (!this.Contains(key))
  953. {
  954. return defaultValue;
  955. }
  956. T result;
  957. try
  958. {
  959. result = (T)((object)Convert.ChangeType(this.Get(key).ToString(), typeof(T), CultureInfo.InvariantCulture));
  960. }
  961. catch (Exception message)
  962. {
  963. UnityEngine.Debug.LogError(message);
  964. result = defaultValue;
  965. }
  966. return result;
  967. }
  968. public T[] ConventToArray<T>()
  969. {
  970. if (this.IsArray)
  971. {
  972. T[] array = new T[this.Count];
  973. for (int i = 0; i < this.Count; i++)
  974. {
  975. array[i] = (T)((object)Convert.ChangeType(this[i].ToString(), typeof(T), CultureInfo.InvariantCulture));
  976. }
  977. return array;
  978. }
  979. return null;
  980. }
  981. public void Remove(object key)
  982. {
  983. this.EnsureDictionary().Remove(key);
  984. for (int i = 0; i < this.object_list.Count; i++)
  985. {
  986. if (this.object_list[i].Key == (string)key)
  987. {
  988. this.object_list.RemoveAt(i);
  989. break;
  990. }
  991. }
  992. this.json = null;
  993. }
  994. private IList<JsonData> inst_array;
  995. private bool inst_boolean;
  996. private double inst_double;
  997. private int inst_int;
  998. private long inst_long;
  999. private IDictionary<string, JsonData> inst_object;
  1000. private string inst_string;
  1001. private string json;
  1002. private JsonType type;
  1003. private IList<KeyValuePair<string, JsonData>> object_list;
  1004. }
  1005. }