JSONNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. namespace I2.Loc.SimpleJSON
  6. {
  7. public class JSONNode
  8. {
  9. public virtual void Add(string aKey, JSONNode aItem)
  10. {
  11. }
  12. public virtual JSONNode this[int aIndex]
  13. {
  14. get
  15. {
  16. return null;
  17. }
  18. set
  19. {
  20. }
  21. }
  22. public virtual JSONNode this[string aKey]
  23. {
  24. get
  25. {
  26. return null;
  27. }
  28. set
  29. {
  30. }
  31. }
  32. public virtual string Value
  33. {
  34. get
  35. {
  36. return string.Empty;
  37. }
  38. set
  39. {
  40. }
  41. }
  42. public virtual int Count
  43. {
  44. get
  45. {
  46. return 0;
  47. }
  48. }
  49. public virtual void Add(JSONNode aItem)
  50. {
  51. this.Add(string.Empty, aItem);
  52. }
  53. public virtual JSONNode Remove(string aKey)
  54. {
  55. return null;
  56. }
  57. public virtual JSONNode Remove(int aIndex)
  58. {
  59. return null;
  60. }
  61. public virtual JSONNode Remove(JSONNode aNode)
  62. {
  63. return aNode;
  64. }
  65. public virtual IEnumerable<JSONNode> Childs
  66. {
  67. get
  68. {
  69. yield break;
  70. }
  71. }
  72. public IEnumerable<JSONNode> DeepChilds
  73. {
  74. get
  75. {
  76. foreach (JSONNode C in this.Childs)
  77. {
  78. foreach (JSONNode D in C.DeepChilds)
  79. {
  80. yield return D;
  81. }
  82. }
  83. yield break;
  84. }
  85. }
  86. public override string ToString()
  87. {
  88. return "JSONNode";
  89. }
  90. public virtual string ToString(string aPrefix)
  91. {
  92. return "JSONNode";
  93. }
  94. public virtual int AsInt
  95. {
  96. get
  97. {
  98. int result = 0;
  99. if (int.TryParse(this.Value, out result))
  100. {
  101. return result;
  102. }
  103. return 0;
  104. }
  105. set
  106. {
  107. this.Value = value.ToString();
  108. }
  109. }
  110. public virtual float AsFloat
  111. {
  112. get
  113. {
  114. float result = 0f;
  115. if (float.TryParse(this.Value, out result))
  116. {
  117. return result;
  118. }
  119. return 0f;
  120. }
  121. set
  122. {
  123. this.Value = value.ToString();
  124. }
  125. }
  126. public virtual double AsDouble
  127. {
  128. get
  129. {
  130. double result = 0.0;
  131. if (double.TryParse(this.Value, out result))
  132. {
  133. return result;
  134. }
  135. return 0.0;
  136. }
  137. set
  138. {
  139. this.Value = value.ToString();
  140. }
  141. }
  142. public virtual bool AsBool
  143. {
  144. get
  145. {
  146. bool result = false;
  147. if (bool.TryParse(this.Value, out result))
  148. {
  149. return result;
  150. }
  151. return !string.IsNullOrEmpty(this.Value);
  152. }
  153. set
  154. {
  155. this.Value = ((!value) ? "false" : "true");
  156. }
  157. }
  158. public virtual JSONArray AsArray
  159. {
  160. get
  161. {
  162. return this as JSONArray;
  163. }
  164. }
  165. public virtual JSONClass AsObject
  166. {
  167. get
  168. {
  169. return this as JSONClass;
  170. }
  171. }
  172. public static implicit operator JSONNode(string s)
  173. {
  174. return new JSONData(s);
  175. }
  176. public static implicit operator string(JSONNode d)
  177. {
  178. return (!(d == null)) ? d.Value : null;
  179. }
  180. public static bool operator ==(JSONNode a, object b)
  181. {
  182. return (b == null && a is JSONLazyCreator) || object.ReferenceEquals(a, b);
  183. }
  184. public static bool operator !=(JSONNode a, object b)
  185. {
  186. return !(a == b);
  187. }
  188. public override bool Equals(object obj)
  189. {
  190. return object.ReferenceEquals(this, obj);
  191. }
  192. public override int GetHashCode()
  193. {
  194. return base.GetHashCode();
  195. }
  196. internal static string Escape(string aText)
  197. {
  198. string text = string.Empty;
  199. foreach (char c in aText)
  200. {
  201. switch (c)
  202. {
  203. case '\b':
  204. text += "\\b";
  205. break;
  206. case '\t':
  207. text += "\\t";
  208. break;
  209. case '\n':
  210. text += "\\n";
  211. break;
  212. default:
  213. if (c != '"')
  214. {
  215. if (c != '\\')
  216. {
  217. text += c;
  218. }
  219. else
  220. {
  221. text += "\\\\";
  222. }
  223. }
  224. else
  225. {
  226. text += "\\\"";
  227. }
  228. break;
  229. case '\f':
  230. text += "\\f";
  231. break;
  232. case '\r':
  233. text += "\\r";
  234. break;
  235. }
  236. }
  237. return text;
  238. }
  239. public static JSONNode Parse(string aJSON)
  240. {
  241. Stack<JSONNode> stack = new Stack<JSONNode>();
  242. JSONNode jsonnode = null;
  243. int i = 0;
  244. string text = string.Empty;
  245. string text2 = string.Empty;
  246. bool flag = false;
  247. while (i < aJSON.Length)
  248. {
  249. char c = aJSON[i];
  250. switch (c)
  251. {
  252. case '\t':
  253. goto IL_333;
  254. case '\n':
  255. case '\r':
  256. break;
  257. default:
  258. switch (c)
  259. {
  260. case '[':
  261. if (flag)
  262. {
  263. text += aJSON[i];
  264. goto IL_45C;
  265. }
  266. stack.Push(new JSONArray());
  267. if (jsonnode != null)
  268. {
  269. text2 = text2.Trim();
  270. if (jsonnode is JSONArray)
  271. {
  272. jsonnode.Add(stack.Peek());
  273. }
  274. else if (text2 != string.Empty)
  275. {
  276. jsonnode.Add(text2, stack.Peek());
  277. }
  278. }
  279. text2 = string.Empty;
  280. text = string.Empty;
  281. jsonnode = stack.Peek();
  282. goto IL_45C;
  283. case '\\':
  284. i++;
  285. if (flag)
  286. {
  287. char c2 = aJSON[i];
  288. switch (c2)
  289. {
  290. case 'r':
  291. text += '\r';
  292. break;
  293. default:
  294. if (c2 != 'b')
  295. {
  296. if (c2 != 'f')
  297. {
  298. if (c2 != 'n')
  299. {
  300. text += c2;
  301. }
  302. else
  303. {
  304. text += '\n';
  305. }
  306. }
  307. else
  308. {
  309. text += '\f';
  310. }
  311. }
  312. else
  313. {
  314. text += '\b';
  315. }
  316. break;
  317. case 't':
  318. text += '\t';
  319. break;
  320. case 'u':
  321. {
  322. string s = aJSON.Substring(i + 1, 4);
  323. text += (char)int.Parse(s, NumberStyles.AllowHexSpecifier);
  324. i += 4;
  325. break;
  326. }
  327. }
  328. }
  329. goto IL_45C;
  330. case ']':
  331. break;
  332. default:
  333. switch (c)
  334. {
  335. case ' ':
  336. goto IL_333;
  337. default:
  338. switch (c)
  339. {
  340. case '{':
  341. if (flag)
  342. {
  343. text += aJSON[i];
  344. goto IL_45C;
  345. }
  346. stack.Push(new JSONClass());
  347. if (jsonnode != null)
  348. {
  349. text2 = text2.Trim();
  350. if (jsonnode is JSONArray)
  351. {
  352. jsonnode.Add(stack.Peek());
  353. }
  354. else if (text2 != string.Empty)
  355. {
  356. jsonnode.Add(text2, stack.Peek());
  357. }
  358. }
  359. text2 = string.Empty;
  360. text = string.Empty;
  361. jsonnode = stack.Peek();
  362. goto IL_45C;
  363. default:
  364. if (c != ',')
  365. {
  366. if (c != ':')
  367. {
  368. text += aJSON[i];
  369. goto IL_45C;
  370. }
  371. if (flag)
  372. {
  373. text += aJSON[i];
  374. goto IL_45C;
  375. }
  376. text2 = text;
  377. text = string.Empty;
  378. goto IL_45C;
  379. }
  380. else
  381. {
  382. if (flag)
  383. {
  384. text += aJSON[i];
  385. goto IL_45C;
  386. }
  387. if (text != string.Empty)
  388. {
  389. if (jsonnode is JSONArray)
  390. {
  391. jsonnode.Add(text);
  392. }
  393. else if (text2 != string.Empty)
  394. {
  395. jsonnode.Add(text2, text);
  396. }
  397. }
  398. text2 = string.Empty;
  399. text = string.Empty;
  400. goto IL_45C;
  401. }
  402. break;
  403. case '}':
  404. break;
  405. }
  406. break;
  407. case '"':
  408. flag ^= true;
  409. goto IL_45C;
  410. }
  411. break;
  412. }
  413. if (flag)
  414. {
  415. text += aJSON[i];
  416. }
  417. else
  418. {
  419. if (stack.Count == 0)
  420. {
  421. throw new Exception("JSON Parse: Too many closing brackets");
  422. }
  423. stack.Pop();
  424. if (text != string.Empty)
  425. {
  426. text2 = text2.Trim();
  427. if (jsonnode is JSONArray)
  428. {
  429. jsonnode.Add(text);
  430. }
  431. else if (text2 != string.Empty)
  432. {
  433. jsonnode.Add(text2, text);
  434. }
  435. }
  436. text2 = string.Empty;
  437. text = string.Empty;
  438. if (stack.Count > 0)
  439. {
  440. jsonnode = stack.Peek();
  441. }
  442. }
  443. break;
  444. }
  445. IL_45C:
  446. i++;
  447. continue;
  448. IL_333:
  449. if (flag)
  450. {
  451. text += aJSON[i];
  452. }
  453. goto IL_45C;
  454. }
  455. if (flag)
  456. {
  457. throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
  458. }
  459. return jsonnode;
  460. }
  461. public virtual void Serialize(BinaryWriter aWriter)
  462. {
  463. }
  464. public void SaveToStream(Stream aData)
  465. {
  466. BinaryWriter aWriter = new BinaryWriter(aData);
  467. this.Serialize(aWriter);
  468. }
  469. public void SaveToCompressedStream(Stream aData)
  470. {
  471. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  472. }
  473. public void SaveToCompressedFile(string aFileName)
  474. {
  475. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  476. }
  477. public string SaveToCompressedBase64()
  478. {
  479. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  480. }
  481. public void SaveToFile(string aFileName)
  482. {
  483. Directory.CreateDirectory(new FileInfo(aFileName).Directory.FullName);
  484. using (FileStream fileStream = File.OpenWrite(aFileName))
  485. {
  486. this.SaveToStream(fileStream);
  487. }
  488. }
  489. public string SaveToBase64()
  490. {
  491. string result;
  492. using (MemoryStream memoryStream = new MemoryStream())
  493. {
  494. this.SaveToStream(memoryStream);
  495. memoryStream.Position = 0L;
  496. result = Convert.ToBase64String(memoryStream.ToArray());
  497. }
  498. return result;
  499. }
  500. public static JSONNode Deserialize(BinaryReader aReader)
  501. {
  502. JSONBinaryTag jsonbinaryTag = (JSONBinaryTag)aReader.ReadByte();
  503. switch (jsonbinaryTag)
  504. {
  505. case JSONBinaryTag.Array:
  506. {
  507. int num = aReader.ReadInt32();
  508. JSONArray jsonarray = new JSONArray();
  509. for (int i = 0; i < num; i++)
  510. {
  511. jsonarray.Add(JSONNode.Deserialize(aReader));
  512. }
  513. return jsonarray;
  514. }
  515. case JSONBinaryTag.Class:
  516. {
  517. int num2 = aReader.ReadInt32();
  518. JSONClass jsonclass = new JSONClass();
  519. for (int j = 0; j < num2; j++)
  520. {
  521. string aKey = aReader.ReadString();
  522. JSONNode aItem = JSONNode.Deserialize(aReader);
  523. jsonclass.Add(aKey, aItem);
  524. }
  525. return jsonclass;
  526. }
  527. case JSONBinaryTag.Value:
  528. return new JSONData(aReader.ReadString());
  529. case JSONBinaryTag.IntValue:
  530. return new JSONData(aReader.ReadInt32());
  531. case JSONBinaryTag.DoubleValue:
  532. return new JSONData(aReader.ReadDouble());
  533. case JSONBinaryTag.BoolValue:
  534. return new JSONData(aReader.ReadBoolean());
  535. case JSONBinaryTag.FloatValue:
  536. return new JSONData(aReader.ReadSingle());
  537. default:
  538. throw new Exception("Error deserializing JSON. Unknown tag: " + jsonbinaryTag);
  539. }
  540. }
  541. public static JSONNode LoadFromCompressedFile(string aFileName)
  542. {
  543. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  544. }
  545. public static JSONNode LoadFromCompressedStream(Stream aData)
  546. {
  547. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  548. }
  549. public static JSONNode LoadFromCompressedBase64(string aBase64)
  550. {
  551. throw new Exception("Can't use compressed functions. You need include the SharpZipLib and uncomment the define at the top of SimpleJSON");
  552. }
  553. public static JSONNode LoadFromStream(Stream aData)
  554. {
  555. JSONNode result;
  556. using (BinaryReader binaryReader = new BinaryReader(aData))
  557. {
  558. result = JSONNode.Deserialize(binaryReader);
  559. }
  560. return result;
  561. }
  562. public static JSONNode LoadFromFile(string aFileName)
  563. {
  564. JSONNode result;
  565. using (FileStream fileStream = File.OpenRead(aFileName))
  566. {
  567. result = JSONNode.LoadFromStream(fileStream);
  568. }
  569. return result;
  570. }
  571. public static JSONNode LoadFromBase64(string aBase64)
  572. {
  573. byte[] buffer = Convert.FromBase64String(aBase64);
  574. return JSONNode.LoadFromStream(new MemoryStream(buffer)
  575. {
  576. Position = 0L
  577. });
  578. }
  579. }
  580. }