SimpleJSON.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. #define USE_SharpZipLib_NO
  2. #define NET2_0
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace SimpleJSON
  7. {
  8. public enum JSONBinaryTag
  9. {
  10. Array = 1,
  11. Class = 2,
  12. Value = 3,
  13. IntValue = 4,
  14. DoubleValue = 5,
  15. BoolValue = 6,
  16. FloatValue = 7,
  17. }
  18. public class JSONNode : ICloneable
  19. {
  20. #region common interface
  21. public virtual void Add(string aKey, JSONNode aItem){ }
  22. public virtual JSONNode this[int aIndex] { get { return null; } set { } }
  23. public virtual JSONNode this[string aKey] { get { return null; } set { } }
  24. public virtual string Key { get { return ""; } set { } }
  25. public virtual string Value { get { return ""; } set { } }
  26. public virtual int Count { get { return 0; } }
  27. public virtual object JObject { get; set; }
  28. public virtual void Add(JSONNode aItem)
  29. {
  30. Add("", aItem);
  31. }
  32. public virtual object Clone()
  33. {
  34. return new JSONNode();
  35. }
  36. public virtual JSONNode Remove(string aKey) { return null; }
  37. public virtual JSONNode Remove(int aIndex) { return null; }
  38. public virtual JSONNode Remove(JSONNode aNode) { return aNode; }
  39. public virtual IEnumerable<JSONNode> Childs { get { yield break;} }
  40. public IEnumerable<JSONNode> DeepChilds
  41. {
  42. get
  43. {
  44. foreach (var C in Childs)
  45. foreach (var D in C.DeepChilds)
  46. yield return D;
  47. }
  48. }
  49. public override string ToString()
  50. {
  51. return "JSONNode";
  52. }
  53. public virtual string ToString(string aPrefix)
  54. {
  55. return "JSONNode";
  56. }
  57. #endregion common interface
  58. #region typecasting properties
  59. public virtual int AsInt
  60. {
  61. get
  62. {
  63. int v = 0;
  64. if (int.TryParse(Value,out v))
  65. return v;
  66. return 0;
  67. }
  68. set
  69. {
  70. Value = value.ToString();
  71. }
  72. }
  73. public virtual long AsLong
  74. {
  75. get
  76. {
  77. long v = 0;
  78. if (long.TryParse(Value, out v))
  79. return v;
  80. return 0;
  81. }
  82. set
  83. {
  84. Value = value.ToString();
  85. }
  86. }
  87. public virtual float AsFloat
  88. {
  89. get
  90. {
  91. float v = 0.0f;
  92. if (float.TryParse(Value,out v))
  93. return v;
  94. return 0.0f;
  95. }
  96. set
  97. {
  98. Value = value.ToString();
  99. }
  100. }
  101. public virtual double AsDouble
  102. {
  103. get
  104. {
  105. double v = 0.0;
  106. if (double.TryParse(Value,out v))
  107. return v;
  108. return 0.0;
  109. }
  110. set
  111. {
  112. Value = value.ToString();
  113. }
  114. }
  115. public virtual bool AsBool
  116. {
  117. get
  118. {
  119. bool v = false;
  120. if (bool.TryParse(Value,out v))
  121. return v;
  122. return !string.IsNullOrEmpty(Value);
  123. }
  124. set
  125. {
  126. Value = (value)?"true":"false";
  127. }
  128. }
  129. public virtual JSONArray AsArray
  130. {
  131. get
  132. {
  133. return this as JSONArray;
  134. }
  135. }
  136. public virtual JSONClass AsObject
  137. {
  138. get
  139. {
  140. return this as JSONClass;
  141. }
  142. }
  143. #endregion typecasting properties
  144. #region operators
  145. public static implicit operator JSONNode(string s)
  146. {
  147. return new JSONData(s);
  148. }
  149. public static implicit operator string(JSONNode d)
  150. {
  151. return (d == null) ? null : d.Value;
  152. }
  153. public static bool operator ==(JSONNode a, object b)
  154. {
  155. if (b == null && a is JSONLazyCreator)
  156. return true;
  157. return System.Object.ReferenceEquals(a, b);
  158. }
  159. public static bool operator !=(JSONNode a, object b)
  160. {
  161. return !(a == b);
  162. }
  163. public override bool Equals (object obj)
  164. {
  165. return System.Object.ReferenceEquals(this, obj);
  166. }
  167. public override int GetHashCode ()
  168. {
  169. return base.GetHashCode();
  170. }
  171. #endregion operators
  172. internal static string Escape(string aText)
  173. {
  174. if (aText == null) aText = "";
  175. string result = "";
  176. foreach(char c in aText)
  177. {
  178. switch(c)
  179. {
  180. case '\\' : result += "\\\\"; break;
  181. case '\"' : result += "\\\""; break;
  182. case '\n' : result += "\\n" ; break;
  183. case '\r' : result += "\\r" ; break;
  184. case '\t' : result += "\\t" ; break;
  185. case '\b' : result += "\\b" ; break;
  186. case '\f' : result += "\\f" ; break;
  187. default : result += c ; break;
  188. }
  189. }
  190. return result;
  191. }
  192. public static JSONNode Parse(string aJSON)
  193. {
  194. Stack<JSONNode> stack = new Stack<JSONNode>();
  195. JSONNode ctx = null;
  196. int i = 0;
  197. string Token = "";
  198. string TokenName = "";
  199. bool QuoteMode = false;
  200. while (i < aJSON.Length)
  201. {
  202. switch (aJSON[i])
  203. {
  204. case '{':
  205. if (QuoteMode)
  206. {
  207. Token += aJSON[i];
  208. break;
  209. }
  210. stack.Push(new JSONClass());
  211. if (ctx != null)
  212. {
  213. TokenName = TokenName.Trim();
  214. if (ctx is JSONArray)
  215. ctx.Add(stack.Peek());
  216. else if (TokenName != "")
  217. ctx.Add(TokenName,stack.Peek());
  218. }
  219. TokenName = "";
  220. Token = "";
  221. ctx = stack.Peek();
  222. break;
  223. case '[':
  224. if (QuoteMode)
  225. {
  226. Token += aJSON[i];
  227. break;
  228. }
  229. stack.Push(new JSONArray());
  230. if (ctx != null)
  231. {
  232. TokenName = TokenName.Trim();
  233. if (ctx is JSONArray)
  234. ctx.Add(stack.Peek());
  235. else if (TokenName != "")
  236. ctx.Add(TokenName,stack.Peek());
  237. }
  238. TokenName = "";
  239. Token = "";
  240. ctx = stack.Peek();
  241. break;
  242. case '}':
  243. case ']':
  244. if (QuoteMode)
  245. {
  246. Token += aJSON[i];
  247. break;
  248. }
  249. if (stack.Count == 0)
  250. throw new Exception("JSON Parse: Too many closing brackets");
  251. stack.Pop();
  252. if (Token != "")
  253. {
  254. TokenName = TokenName.Trim();
  255. if (ctx is JSONArray)
  256. ctx.Add(Token);
  257. else if (TokenName != "")
  258. ctx.Add(TokenName,Token);
  259. }
  260. TokenName = "";
  261. Token = "";
  262. if (stack.Count>0)
  263. ctx = stack.Peek();
  264. break;
  265. case ':':
  266. if (QuoteMode)
  267. {
  268. Token += aJSON[i];
  269. break;
  270. }
  271. TokenName = Token;
  272. Token = "";
  273. break;
  274. case '"':
  275. QuoteMode ^= true;
  276. break;
  277. case ',':
  278. if (QuoteMode)
  279. {
  280. Token += aJSON[i];
  281. break;
  282. }
  283. if (Token != "")
  284. {
  285. if (ctx is JSONArray)
  286. ctx.Add(Token);
  287. else if (TokenName != "")
  288. ctx.Add(TokenName, Token);
  289. }
  290. TokenName = "";
  291. Token = "";
  292. break;
  293. case '\r':
  294. case '\n':
  295. break;
  296. case ' ':
  297. case '\t':
  298. if (QuoteMode)
  299. Token += aJSON[i];
  300. break;
  301. case '\\':
  302. ++i;
  303. if (QuoteMode)
  304. {
  305. char C = aJSON[i];
  306. switch (C)
  307. {
  308. case 't' : Token += '\t'; break;
  309. case 'r' : Token += '\r'; break;
  310. case 'n' : Token += '\n'; break;
  311. case 'b' : Token += '\b'; break;
  312. case 'f' : Token += '\f'; break;
  313. case 'u':
  314. {
  315. string s = aJSON.Substring(i+1,4);
  316. Token += (char)int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
  317. i += 4;
  318. break;
  319. }
  320. default : Token += C; break;
  321. }
  322. }
  323. break;
  324. default:
  325. Token += aJSON[i];
  326. break;
  327. }
  328. ++i;
  329. }
  330. if (QuoteMode)
  331. {
  332. throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
  333. }
  334. return ctx;
  335. }
  336. public virtual void Serialize(System.IO.BinaryWriter aWriter) {}
  337. public void SaveToStream(System.IO.Stream aData)
  338. {
  339. var W = new System.IO.BinaryWriter(aData);
  340. Serialize(W);
  341. }
  342. #if USE_SharpZipLib
  343. public void SaveToCompressedStream(System.IO.Stream aData)
  344. {
  345. using (var gzipOut = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(aData))
  346. {
  347. gzipOut.IsStreamOwner = false;
  348. SaveToStream(gzipOut);
  349. gzipOut.Close();
  350. }
  351. }
  352. public void SaveToCompressedFile(string aFileName)
  353. {
  354. System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName);
  355. using(var F = System.IO.File.OpenWrite(aFileName))
  356. {
  357. SaveToCompressedStream(F);
  358. }
  359. }
  360. public string SaveToCompressedBase64()
  361. {
  362. using (var stream = new System.IO.MemoryStream())
  363. {
  364. SaveToCompressedStream(stream);
  365. stream.Position = 0;
  366. return System.Convert.ToBase64String(stream.ToArray());
  367. }
  368. }
  369. #else
  370. public void SaveToCompressedStream(System.IO.Stream aData)
  371. {
  372. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  373. }
  374. public void SaveToCompressedFile(string aFileName)
  375. {
  376. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  377. }
  378. public string SaveToCompressedBase64()
  379. {
  380. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  381. }
  382. #endif
  383. public void SaveToFile(string aFileName)
  384. {
  385. System.IO.Directory.CreateDirectory((new System.IO.FileInfo(aFileName)).Directory.FullName);
  386. using(var F = System.IO.File.OpenWrite(aFileName))
  387. {
  388. SaveToStream(F);
  389. }
  390. }
  391. public string SaveToBase64()
  392. {
  393. using (var stream = new System.IO.MemoryStream())
  394. {
  395. SaveToStream(stream);
  396. stream.Position = 0;
  397. return System.Convert.ToBase64String(stream.ToArray());
  398. }
  399. }
  400. public static JSONNode Deserialize(System.IO.BinaryReader aReader)
  401. {
  402. JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte();
  403. switch(type)
  404. {
  405. case JSONBinaryTag.Array:
  406. {
  407. int count = aReader.ReadInt32();
  408. JSONArray tmp = new JSONArray();
  409. for(int i = 0; i < count; i++)
  410. tmp.Add(Deserialize(aReader));
  411. return tmp;
  412. }
  413. case JSONBinaryTag.Class:
  414. {
  415. int count = aReader.ReadInt32();
  416. JSONClass tmp = new JSONClass();
  417. for(int i = 0; i < count; i++)
  418. {
  419. string key = aReader.ReadString();
  420. var val = Deserialize(aReader);
  421. tmp.Add(key, val);
  422. }
  423. return tmp;
  424. }
  425. case JSONBinaryTag.Value:
  426. {
  427. return new JSONData(aReader.ReadString());
  428. }
  429. case JSONBinaryTag.IntValue:
  430. {
  431. return new JSONData(aReader.ReadInt32());
  432. }
  433. case JSONBinaryTag.DoubleValue:
  434. {
  435. return new JSONData(aReader.ReadDouble());
  436. }
  437. case JSONBinaryTag.BoolValue:
  438. {
  439. return new JSONData(aReader.ReadBoolean());
  440. }
  441. case JSONBinaryTag.FloatValue:
  442. {
  443. return new JSONData(aReader.ReadSingle());
  444. }
  445. default:
  446. {
  447. throw new Exception("Error deserializing JSON. Unknown tag: " + type);
  448. }
  449. }
  450. }
  451. #if USE_SharpZipLib
  452. public static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
  453. {
  454. var zin = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(aData);
  455. return LoadFromStream(zin);
  456. }
  457. public static JSONNode LoadFromCompressedFile(string aFileName)
  458. {
  459. using(var F = System.IO.File.OpenRead(aFileName))
  460. {
  461. return LoadFromCompressedStream(F);
  462. }
  463. }
  464. public static JSONNode LoadFromCompressedBase64(string aBase64)
  465. {
  466. var tmp = System.Convert.FromBase64String(aBase64);
  467. var stream = new System.IO.MemoryStream(tmp);
  468. stream.Position = 0;
  469. return LoadFromCompressedStream(stream);
  470. }
  471. #else
  472. public static JSONNode LoadFromCompressedFile(string aFileName)
  473. {
  474. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  475. }
  476. public static JSONNode LoadFromCompressedStream(System.IO.Stream aData)
  477. {
  478. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  479. }
  480. public static JSONNode LoadFromCompressedBase64(string aBase64)
  481. {
  482. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  483. }
  484. #endif
  485. public static JSONNode LoadFromStream(System.IO.Stream aData)
  486. {
  487. using(var R = new System.IO.BinaryReader(aData))
  488. {
  489. return Deserialize(R);
  490. }
  491. }
  492. public static JSONNode LoadFromFile(string aFileName)
  493. {
  494. using(var F = System.IO.File.OpenRead(aFileName))
  495. {
  496. return LoadFromStream(F);
  497. }
  498. }
  499. public static JSONNode LoadFromBase64(string aBase64)
  500. {
  501. var tmp = System.Convert.FromBase64String(aBase64);
  502. var stream = new System.IO.MemoryStream(tmp);
  503. stream.Position = 0;
  504. return LoadFromStream(stream);
  505. }
  506. public static string GetKey(JSONNode json, int index)
  507. {
  508. try
  509. {
  510. JSONClass jsc = (JSONClass)json;
  511. return jsc.GetKey(index);
  512. }
  513. catch
  514. {
  515. }
  516. return "";
  517. }
  518. } // End of JSONNode
  519. public class JSONArray : JSONNode, IEnumerable
  520. {
  521. private List<JSONNode> m_List = new List<JSONNode>();
  522. public override JSONNode this[int aIndex]
  523. {
  524. get
  525. {
  526. if (aIndex<0 || aIndex >= m_List.Count)
  527. return new JSONLazyCreator(this);
  528. return m_List[aIndex];
  529. }
  530. set
  531. {
  532. if (aIndex<0 || aIndex >= m_List.Count)
  533. m_List.Add(value);
  534. else
  535. m_List[aIndex] = value;
  536. }
  537. }
  538. public override JSONNode this[string aKey]
  539. {
  540. get{ return new JSONLazyCreator(this);}
  541. set{ m_List.Add(value); }
  542. }
  543. public override object Clone()
  544. {
  545. JSONArray arr = new JSONArray();
  546. foreach (var node in m_List)
  547. {
  548. arr.m_List.Add(node.Clone() as JSONNode);
  549. }
  550. return arr;
  551. }
  552. public override int Count
  553. {
  554. get { return m_List.Count; }
  555. }
  556. public override void Add(string aKey, JSONNode aItem)
  557. {
  558. m_List.Add(aItem);
  559. }
  560. public override JSONNode Remove(int aIndex)
  561. {
  562. if (aIndex < 0 || aIndex >= m_List.Count)
  563. return null;
  564. JSONNode tmp = m_List[aIndex];
  565. m_List.RemoveAt(aIndex);
  566. return tmp;
  567. }
  568. public override JSONNode Remove(JSONNode aNode)
  569. {
  570. m_List.Remove(aNode);
  571. return aNode;
  572. }
  573. public override IEnumerable<JSONNode> Childs
  574. {
  575. get
  576. {
  577. foreach(JSONNode N in m_List)
  578. yield return N;
  579. }
  580. }
  581. public IEnumerator GetEnumerator()
  582. {
  583. foreach(JSONNode N in m_List)
  584. yield return N;
  585. }
  586. public override string ToString()
  587. {
  588. string result = "[ ";
  589. foreach (JSONNode N in m_List)
  590. {
  591. if (result.Length > 2)
  592. result += ", ";
  593. result += N.ToString();
  594. }
  595. result += " ]";
  596. return result;
  597. }
  598. public override string ToString(string aPrefix)
  599. {
  600. string result = "[ ";
  601. foreach (JSONNode N in m_List)
  602. {
  603. if (result.Length > 3)
  604. result += ", ";
  605. result += "\n" + aPrefix + " ";
  606. result += N.ToString(aPrefix+" ");
  607. }
  608. result += "\n" + aPrefix + "]";
  609. return result;
  610. }
  611. public override void Serialize (System.IO.BinaryWriter aWriter)
  612. {
  613. aWriter.Write((byte)JSONBinaryTag.Array);
  614. aWriter.Write(m_List.Count);
  615. for(int i = 0; i < m_List.Count; i++)
  616. {
  617. m_List[i].Serialize(aWriter);
  618. }
  619. }
  620. } // End of JSONArray
  621. public class JSONClass : JSONNode, IEnumerable
  622. {
  623. private Dictionary<string,JSONNode> m_Dict = new Dictionary<string,JSONNode>();
  624. public override JSONNode this[string aKey]
  625. {
  626. get
  627. {
  628. if (m_Dict.ContainsKey(aKey))
  629. return m_Dict[aKey];
  630. else
  631. return new JSONLazyCreator(this, aKey);
  632. }
  633. set
  634. {
  635. if (m_Dict.ContainsKey(aKey))
  636. m_Dict[aKey] = value;
  637. else
  638. m_Dict.Add(aKey,value);
  639. }
  640. }
  641. public override object Clone()
  642. {
  643. JSONClass cls = new JSONClass();
  644. foreach (var kp in m_Dict)
  645. cls.m_Dict.Add(kp.Key, kp.Value.Clone() as JSONNode);
  646. return cls;
  647. }
  648. public override JSONNode this[int aIndex]
  649. {
  650. get
  651. {
  652. if (aIndex < 0 || aIndex >= m_Dict.Count)
  653. return null;
  654. #if NET2_0
  655. int i = 0;
  656. JSONNode v = null;
  657. foreach(var o in m_Dict)
  658. {
  659. if (aIndex == i)
  660. {
  661. v = o.Value;
  662. break;
  663. }
  664. i++;
  665. }
  666. return v;
  667. #else
  668. return m_Dict.ElementAt(aIndex).Value;
  669. #endif
  670. }
  671. set
  672. {
  673. if (aIndex < 0 || aIndex >= m_Dict.Count)
  674. return;
  675. #if NET2_0
  676. int i = 0;
  677. string k = "";
  678. foreach (var o in m_Dict)
  679. {
  680. if (aIndex == i)
  681. {
  682. k = o.Key;
  683. break;
  684. }
  685. i++;
  686. }
  687. if (!string.IsNullOrEmpty(k)) m_Dict[k] = value;
  688. #else
  689. string key = m_Dict.ElementAt(aIndex).Key;
  690. m_Dict[key] = value;
  691. #endif
  692. }
  693. }
  694. public string GetKey(int aIndex)
  695. {
  696. string key = "";
  697. if (aIndex < 0 || aIndex >= m_Dict.Count)
  698. return null;
  699. int i = 0;
  700. //JSONNode v = null;
  701. foreach (var o in m_Dict)
  702. {
  703. if (aIndex == i)
  704. {
  705. key = o.Key;
  706. //v = o.Value;
  707. break;
  708. }
  709. i++;
  710. }
  711. return key;
  712. }
  713. public override int Count
  714. {
  715. get { return m_Dict.Count; }
  716. }
  717. public override void Add(string aKey, JSONNode aItem)
  718. {
  719. if (!string.IsNullOrEmpty(aKey))
  720. {
  721. if (m_Dict.ContainsKey(aKey))
  722. m_Dict[aKey] = aItem;
  723. else
  724. m_Dict.Add(aKey, aItem);
  725. }
  726. else
  727. m_Dict.Add(Guid.NewGuid().ToString(), aItem);
  728. }
  729. public override JSONNode Remove(string aKey)
  730. {
  731. if (!m_Dict.ContainsKey(aKey))
  732. return null;
  733. JSONNode tmp = m_Dict[aKey];
  734. m_Dict.Remove(aKey);
  735. return tmp;
  736. }
  737. public override JSONNode Remove(int aIndex)
  738. {
  739. if (aIndex < 0 || aIndex >= m_Dict.Count)
  740. return null;
  741. #if NET2_0
  742. int i = 0;
  743. string k = "";
  744. JSONNode v = null;
  745. foreach (var o in m_Dict)
  746. {
  747. if (aIndex == i)
  748. {
  749. k = o.Key;
  750. v = o.Value;
  751. break;
  752. }
  753. i++;
  754. }
  755. if (!string.IsNullOrEmpty(k)) m_Dict.Remove(k);
  756. return v;
  757. #else
  758. var item = m_Dict.ElementAt(aIndex);
  759. m_Dict.Remove(item.Key);
  760. return item.Value;
  761. #endif
  762. }
  763. public override JSONNode Remove(JSONNode aNode)
  764. {
  765. try
  766. {
  767. #if NET2_0
  768. int i = 0;
  769. string k = "";
  770. foreach (var o in m_Dict)
  771. {
  772. if (o.Value == aNode)
  773. {
  774. k = o.Key;
  775. break;
  776. }
  777. i++;
  778. }
  779. if(!string.IsNullOrEmpty(k)) m_Dict.Remove(k);
  780. return aNode;
  781. #else
  782. var item = m_Dict.Where(k => k.Value == aNode).First();
  783. m_Dict.Remove(item.Key);
  784. return aNode;
  785. #endif
  786. }
  787. catch
  788. {
  789. return null;
  790. }
  791. }
  792. public override IEnumerable<JSONNode> Childs
  793. {
  794. get
  795. {
  796. foreach(KeyValuePair<string,JSONNode> N in m_Dict)
  797. yield return N.Value;
  798. }
  799. }
  800. public IEnumerator GetEnumerator()
  801. {
  802. foreach(KeyValuePair<string, JSONNode> N in m_Dict)
  803. yield return N;
  804. }
  805. public override string ToString()
  806. {
  807. string result = "{";
  808. foreach (KeyValuePair<string, JSONNode> N in m_Dict)
  809. {
  810. if (N.Value == null) continue;
  811. if (result.Length > 2)
  812. result += ", ";
  813. result += "\"" + Escape(N.Key) + "\":" + N.Value.ToString();
  814. }
  815. result += "}";
  816. return result;
  817. }
  818. public override string ToString(string aPrefix)
  819. {
  820. string result = "{ ";
  821. foreach (KeyValuePair<string, JSONNode> N in m_Dict)
  822. {
  823. if (result.Length > 3)
  824. result += ", ";
  825. result += "\n" + aPrefix + " ";
  826. result += "\"" + Escape(N.Key) + "\" : " + N.Value.ToString(aPrefix+" ");
  827. }
  828. result += "\n" + aPrefix + "}";
  829. return result;
  830. }
  831. public override void Serialize (System.IO.BinaryWriter aWriter)
  832. {
  833. aWriter.Write((byte)JSONBinaryTag.Class);
  834. aWriter.Write(m_Dict.Count);
  835. foreach(string K in m_Dict.Keys)
  836. {
  837. aWriter.Write(K);
  838. m_Dict[K].Serialize(aWriter);
  839. }
  840. }
  841. } // End of JSONClass
  842. public class JSONData : JSONNode
  843. {
  844. private string m_Data;
  845. public override string Value
  846. {
  847. get { return m_Data; }
  848. set { m_Data = value; }
  849. }
  850. public override object Clone()
  851. {
  852. JSONData d = new JSONData(m_Data);
  853. return d;
  854. }
  855. public JSONData(string aData)
  856. {
  857. m_Data = aData;
  858. }
  859. public JSONData(float aData)
  860. {
  861. AsFloat = aData;
  862. }
  863. public JSONData(double aData)
  864. {
  865. AsDouble = aData;
  866. }
  867. public JSONData(bool aData)
  868. {
  869. AsBool = aData;
  870. }
  871. public JSONData(int aData)
  872. {
  873. AsInt = aData;
  874. }
  875. public override string ToString()
  876. {
  877. return "\"" + Escape(m_Data) + "\"";
  878. }
  879. public override string ToString(string aPrefix)
  880. {
  881. return "\"" + Escape(m_Data) + "\"";
  882. }
  883. public override void Serialize (System.IO.BinaryWriter aWriter)
  884. {
  885. var tmp = new JSONData("");
  886. tmp.AsInt = AsInt;
  887. if (tmp.m_Data == this.m_Data)
  888. {
  889. aWriter.Write((byte)JSONBinaryTag.IntValue);
  890. aWriter.Write(AsInt);
  891. return;
  892. }
  893. tmp.AsFloat = AsFloat;
  894. if (tmp.m_Data == this.m_Data)
  895. {
  896. aWriter.Write((byte)JSONBinaryTag.FloatValue);
  897. aWriter.Write(AsFloat);
  898. return;
  899. }
  900. tmp.AsDouble = AsDouble;
  901. if (tmp.m_Data == this.m_Data)
  902. {
  903. aWriter.Write((byte)JSONBinaryTag.DoubleValue);
  904. aWriter.Write(AsDouble);
  905. return;
  906. }
  907. tmp.AsBool = AsBool;
  908. if (tmp.m_Data == this.m_Data)
  909. {
  910. aWriter.Write((byte)JSONBinaryTag.BoolValue);
  911. aWriter.Write(AsBool);
  912. return;
  913. }
  914. aWriter.Write((byte)JSONBinaryTag.Value);
  915. aWriter.Write(m_Data);
  916. }
  917. } // End of JSONData
  918. internal class JSONLazyCreator : JSONNode
  919. {
  920. private JSONNode m_Node = null;
  921. private string m_Key = null;
  922. public JSONLazyCreator(JSONNode aNode)
  923. {
  924. m_Node = aNode;
  925. m_Key = null;
  926. }
  927. public JSONLazyCreator(JSONNode aNode, string aKey)
  928. {
  929. m_Node = aNode;
  930. m_Key = aKey;
  931. }
  932. private void Set(JSONNode aVal)
  933. {
  934. if (m_Key == null)
  935. {
  936. m_Node.Add(aVal);
  937. }
  938. else
  939. {
  940. m_Node.Add(m_Key, aVal);
  941. }
  942. m_Node = null; // Be GC friendly.
  943. }
  944. public override JSONNode this[int aIndex]
  945. {
  946. get
  947. {
  948. return new JSONLazyCreator(this);
  949. }
  950. set
  951. {
  952. var tmp = new JSONArray();
  953. tmp.Add(value);
  954. Set(tmp);
  955. }
  956. }
  957. public override JSONNode this[string aKey]
  958. {
  959. get
  960. {
  961. return new JSONLazyCreator(this, aKey);
  962. }
  963. set
  964. {
  965. var tmp = new JSONClass();
  966. tmp.Add(aKey, value);
  967. Set(tmp);
  968. }
  969. }
  970. public override void Add (JSONNode aItem)
  971. {
  972. var tmp = new JSONArray();
  973. tmp.Add(aItem);
  974. Set(tmp);
  975. }
  976. public override void Add (string aKey, JSONNode aItem)
  977. {
  978. var tmp = new JSONClass();
  979. tmp.Add(aKey, aItem);
  980. Set(tmp);
  981. }
  982. public static bool operator ==(JSONLazyCreator a, object b)
  983. {
  984. if (b == null)
  985. return true;
  986. return System.Object.ReferenceEquals(a,b);
  987. }
  988. public static bool operator !=(JSONLazyCreator a, object b)
  989. {
  990. return !(a == b);
  991. }
  992. public override bool Equals (object obj)
  993. {
  994. if (obj == null)
  995. return true;
  996. return System.Object.ReferenceEquals(this, obj);
  997. }
  998. public override int GetHashCode ()
  999. {
  1000. return base.GetHashCode();
  1001. }
  1002. public override string ToString()
  1003. {
  1004. return "";
  1005. }
  1006. public override string ToString(string aPrefix)
  1007. {
  1008. return "";
  1009. }
  1010. public override int AsInt
  1011. {
  1012. get
  1013. {
  1014. JSONData tmp = new JSONData(0);
  1015. Set(tmp);
  1016. return 0;
  1017. }
  1018. set
  1019. {
  1020. JSONData tmp = new JSONData(value);
  1021. Set(tmp);
  1022. }
  1023. }
  1024. public override float AsFloat
  1025. {
  1026. get
  1027. {
  1028. JSONData tmp = new JSONData(0.0f);
  1029. Set(tmp);
  1030. return 0.0f;
  1031. }
  1032. set
  1033. {
  1034. JSONData tmp = new JSONData(value);
  1035. Set(tmp);
  1036. }
  1037. }
  1038. public override double AsDouble
  1039. {
  1040. get
  1041. {
  1042. JSONData tmp = new JSONData(0.0);
  1043. Set(tmp);
  1044. return 0.0;
  1045. }
  1046. set
  1047. {
  1048. JSONData tmp = new JSONData(value);
  1049. Set(tmp);
  1050. }
  1051. }
  1052. public override bool AsBool
  1053. {
  1054. get
  1055. {
  1056. JSONData tmp = new JSONData(false);
  1057. Set(tmp);
  1058. return false;
  1059. }
  1060. set
  1061. {
  1062. JSONData tmp = new JSONData(value);
  1063. Set(tmp);
  1064. }
  1065. }
  1066. public override JSONArray AsArray
  1067. {
  1068. get
  1069. {
  1070. JSONArray tmp = new JSONArray();
  1071. Set(tmp);
  1072. return tmp;
  1073. }
  1074. }
  1075. public override JSONClass AsObject
  1076. {
  1077. get
  1078. {
  1079. JSONClass tmp = new JSONClass();
  1080. Set(tmp);
  1081. return tmp;
  1082. }
  1083. }
  1084. } // End of JSONLazyCreator
  1085. public static class JSON
  1086. {
  1087. public static JSONNode Parse(string aJSON)
  1088. {
  1089. return JSONNode.Parse(aJSON);
  1090. }
  1091. }
  1092. }