Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MiniJSON.cs 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * Copyright (c) 2013 Calvin Rien
  3. *
  4. * Based on the JSON parser by Patrick van Bergen
  5. * http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
  6. *
  7. * Simplified it so that it doesn't throw exceptions
  8. * and can be used in Unity iPhone with maximum code stripping.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining
  11. * a copy of this software and associated documentation files (the
  12. * "Software"), to deal in the Software without restriction, including
  13. * without limitation the rights to use, copy, modify, merge, publish,
  14. * distribute, sublicense, and/or sell copies of the Software, and to
  15. * permit persons to whom the Software is furnished to do so, subject to
  16. * the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be
  19. * included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  25. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  27. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Text;
  35. namespace UnityEngine.Purchasing
  36. {
  37. namespace MiniJSON
  38. {
  39. // Example usage:
  40. //
  41. // using UnityEngine;
  42. // using System.Collections;
  43. // using System.Collections.Generic;
  44. // using MiniJSON;
  45. //
  46. // public class MiniJSONTest : MonoBehaviour {
  47. // void Start () {
  48. // var jsonString = "{ \"array\": [1.44,2,3], " +
  49. // "\"object\": {\"key1\":\"value1\", \"key2\":256}, " +
  50. // "\"string\": \"The quick brown fox \\\"jumps\\\" over the lazy dog \", " +
  51. // "\"unicode\": \"\\u3041 Men\u00fa sesi\u00f3n\", " +
  52. // "\"int\": 65536, " +
  53. // "\"float\": 3.1415926, " +
  54. // "\"bool\": true, " +
  55. // "\"null\": null }";
  56. //
  57. // var dict = Json.Deserialize(jsonString) as Dictionary<string,object>;
  58. //
  59. // Debug.Log("deserialized: " + dict.GetType());
  60. // Debug.Log("dict['array'][0]: " + ((List<object>) dict["array"])[0]);
  61. // Debug.Log("dict['string']: " + (string) dict["string"]);
  62. // Debug.Log("dict['float']: " + (double) dict["float"]); // floats come out as doubles
  63. // Debug.Log("dict['int']: " + (long) dict["int"]); // ints come out as longs
  64. // Debug.Log("dict['unicode']: " + (string) dict["unicode"]);
  65. //
  66. // var str = Json.Serialize(dict);
  67. //
  68. // Debug.Log("serialized: " + str);
  69. // }
  70. // }
  71. /// <summary>
  72. /// This class encodes and decodes JSON strings.
  73. /// Spec. details, see http://www.json.org/
  74. ///
  75. /// JSON uses Arrays and Objects. These correspond here to the datatypes IList and IDictionary.
  76. /// All numbers are parsed to doubles.
  77. /// </summary>
  78. public static class Json
  79. {
  80. /// <summary>
  81. /// Parses the string json into a value
  82. /// </summary>
  83. /// <param name="json">A JSON string.</param>
  84. /// <returns>An List&lt;object&gt;, a Dictionary&lt;string, object&gt;, a double, an integer,a string, null, true, or false</returns>
  85. public static object Deserialize(string json)
  86. {
  87. // save the string for debug information
  88. if (json == null)
  89. {
  90. return null;
  91. }
  92. return Parser.Parse(json);
  93. }
  94. sealed class Parser : IDisposable
  95. {
  96. const string WORD_BREAK = "{}[],:\"";
  97. public static bool IsWordBreak(char c)
  98. {
  99. return Char.IsWhiteSpace(c) || WORD_BREAK.IndexOf(c) != -1;
  100. }
  101. enum TOKEN
  102. {
  103. NONE,
  104. CURLY_OPEN,
  105. CURLY_CLOSE,
  106. SQUARED_OPEN,
  107. SQUARED_CLOSE,
  108. COLON,
  109. COMMA,
  110. STRING,
  111. NUMBER,
  112. TRUE,
  113. FALSE,
  114. NULL
  115. };
  116. StringReader json;
  117. Parser(string jsonString)
  118. {
  119. json = new StringReader(jsonString);
  120. }
  121. public static object Parse(string jsonString)
  122. {
  123. using (var instance = new Parser(jsonString))
  124. {
  125. return instance.ParseValue();
  126. }
  127. }
  128. public void Dispose()
  129. {
  130. json.Dispose();
  131. json = null;
  132. }
  133. Dictionary<string, object> ParseObject()
  134. {
  135. var table = new Dictionary<string, object>();
  136. // ditch opening brace
  137. json.Read();
  138. // {
  139. while (true)
  140. {
  141. switch (NextToken)
  142. {
  143. case TOKEN.NONE:
  144. return null;
  145. case TOKEN.COMMA:
  146. continue;
  147. case TOKEN.CURLY_CLOSE:
  148. return table;
  149. default:
  150. // name
  151. var name = ParseString();
  152. if (name == null)
  153. {
  154. return null;
  155. }
  156. // :
  157. if (NextToken != TOKEN.COLON)
  158. {
  159. return null;
  160. }
  161. // ditch the colon
  162. json.Read();
  163. // value
  164. table[name] = ParseValue();
  165. break;
  166. }
  167. }
  168. }
  169. List<object> ParseArray()
  170. {
  171. var array = new List<object>();
  172. // ditch opening bracket
  173. json.Read();
  174. // [
  175. var parsing = true;
  176. while (parsing)
  177. {
  178. var nextToken = NextToken;
  179. switch (nextToken)
  180. {
  181. case TOKEN.NONE:
  182. return null;
  183. case TOKEN.COMMA:
  184. continue;
  185. case TOKEN.SQUARED_CLOSE:
  186. parsing = false;
  187. break;
  188. default:
  189. var value = ParseByToken(nextToken);
  190. array.Add(value);
  191. break;
  192. }
  193. }
  194. return array;
  195. }
  196. object ParseValue()
  197. {
  198. var nextToken = NextToken;
  199. return ParseByToken(nextToken);
  200. }
  201. object ParseByToken(TOKEN token)
  202. {
  203. switch (token)
  204. {
  205. case TOKEN.STRING:
  206. return ParseString();
  207. case TOKEN.NUMBER:
  208. return ParseNumber();
  209. case TOKEN.CURLY_OPEN:
  210. return ParseObject();
  211. case TOKEN.SQUARED_OPEN:
  212. return ParseArray();
  213. case TOKEN.TRUE:
  214. return true;
  215. case TOKEN.FALSE:
  216. return false;
  217. case TOKEN.NULL:
  218. return null;
  219. default:
  220. return null;
  221. }
  222. }
  223. string ParseString()
  224. {
  225. var s = new StringBuilder();
  226. char c;
  227. // ditch opening quote
  228. json.Read();
  229. var parsing = true;
  230. while (parsing)
  231. {
  232. if (json.Peek() == -1)
  233. {
  234. break;
  235. }
  236. c = NextChar;
  237. switch (c)
  238. {
  239. case '"':
  240. parsing = false;
  241. break;
  242. case '\\':
  243. if (json.Peek() == -1)
  244. {
  245. parsing = false;
  246. break;
  247. }
  248. c = NextChar;
  249. switch (c)
  250. {
  251. case '"':
  252. case '\\':
  253. case '/':
  254. s.Append(c);
  255. break;
  256. case 'b':
  257. s.Append('\b');
  258. break;
  259. case 'f':
  260. s.Append('\f');
  261. break;
  262. case 'n':
  263. s.Append('\n');
  264. break;
  265. case 'r':
  266. s.Append('\r');
  267. break;
  268. case 't':
  269. s.Append('\t');
  270. break;
  271. case 'u':
  272. var hex = new char[4];
  273. for (var i = 0; i < 4; i++)
  274. {
  275. hex[i] = NextChar;
  276. }
  277. s.Append((char)Convert.ToInt32(new string(hex), 16));
  278. break;
  279. }
  280. break;
  281. default:
  282. s.Append(c);
  283. break;
  284. }
  285. }
  286. return s.ToString();
  287. }
  288. object ParseNumber()
  289. {
  290. var number = NextWord;
  291. if (number.IndexOf('.') == -1 && number.IndexOf('e') == -1 && number.IndexOf('E') == -1)
  292. {
  293. Int64.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedInt);
  294. return parsedInt;
  295. }
  296. Double.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedDouble);
  297. return parsedDouble;
  298. }
  299. void EatWhitespace()
  300. {
  301. while (Char.IsWhiteSpace(PeekChar))
  302. {
  303. json.Read();
  304. if (json.Peek() == -1)
  305. {
  306. break;
  307. }
  308. }
  309. }
  310. char PeekChar => Convert.ToChar(json.Peek());
  311. char NextChar => Convert.ToChar(json.Read());
  312. string NextWord
  313. {
  314. get
  315. {
  316. var word = new StringBuilder();
  317. while (!IsWordBreak(PeekChar))
  318. {
  319. word.Append(NextChar);
  320. if (json.Peek() == -1)
  321. {
  322. break;
  323. }
  324. }
  325. return word.ToString();
  326. }
  327. }
  328. TOKEN NextToken
  329. {
  330. get
  331. {
  332. EatWhitespace();
  333. if (json.Peek() == -1)
  334. {
  335. return TOKEN.NONE;
  336. }
  337. switch (PeekChar)
  338. {
  339. case '{':
  340. return TOKEN.CURLY_OPEN;
  341. case '}':
  342. json.Read();
  343. return TOKEN.CURLY_CLOSE;
  344. case '[':
  345. return TOKEN.SQUARED_OPEN;
  346. case ']':
  347. json.Read();
  348. return TOKEN.SQUARED_CLOSE;
  349. case ',':
  350. json.Read();
  351. return TOKEN.COMMA;
  352. case '"':
  353. return TOKEN.STRING;
  354. case ':':
  355. return TOKEN.COLON;
  356. case '0':
  357. case '1':
  358. case '2':
  359. case '3':
  360. case '4':
  361. case '5':
  362. case '6':
  363. case '7':
  364. case '8':
  365. case '9':
  366. case '-':
  367. return TOKEN.NUMBER;
  368. }
  369. switch (NextWord)
  370. {
  371. case "false":
  372. return TOKEN.FALSE;
  373. case "true":
  374. return TOKEN.TRUE;
  375. case "null":
  376. return TOKEN.NULL;
  377. }
  378. return TOKEN.NONE;
  379. }
  380. }
  381. }
  382. /// <summary>
  383. /// Converts a IDictionary / IList object or a simple type (string, int, etc.) into a JSON string
  384. /// </summary>
  385. /// <param name="obj">A Dictionary&lt;string, object&gt; / List&lt;object&gt;</param>
  386. /// <returns>A JSON encoded string, or null if object 'json' is not serializable</returns>
  387. public static string Serialize(object obj)
  388. {
  389. return Serializer.Serialize(obj);
  390. }
  391. sealed class Serializer
  392. {
  393. readonly StringBuilder builder;
  394. Serializer()
  395. {
  396. builder = new StringBuilder();
  397. }
  398. public static string Serialize(object obj)
  399. {
  400. var instance = new Serializer();
  401. instance.SerializeValue(obj);
  402. return instance.builder.ToString();
  403. }
  404. void SerializeValue(object value)
  405. {
  406. IList asList;
  407. IDictionary asDict;
  408. string asStr;
  409. if (value == null)
  410. {
  411. builder.Append("null");
  412. }
  413. else if ((asStr = value as string) != null)
  414. {
  415. SerializeString(asStr);
  416. }
  417. else if (value is bool)
  418. {
  419. builder.Append((bool)value ? "true" : "false");
  420. }
  421. else if ((asList = value as IList) != null)
  422. {
  423. SerializeArray(asList);
  424. }
  425. else if ((asDict = value as IDictionary) != null)
  426. {
  427. SerializeObject(asDict);
  428. }
  429. else if (value is char)
  430. {
  431. SerializeString(new string((char)value, 1));
  432. }
  433. else
  434. {
  435. SerializeOther(value);
  436. }
  437. }
  438. void SerializeObject(IDictionary obj)
  439. {
  440. var first = true;
  441. builder.Append('{');
  442. foreach (var e in obj.Keys)
  443. {
  444. if (!first)
  445. {
  446. builder.Append(',');
  447. }
  448. SerializeString(e.ToString());
  449. builder.Append(':');
  450. SerializeValue(obj[e]);
  451. first = false;
  452. }
  453. builder.Append('}');
  454. }
  455. void SerializeArray(IList anArray)
  456. {
  457. builder.Append('[');
  458. var first = true;
  459. foreach (var obj in anArray)
  460. {
  461. if (!first)
  462. {
  463. builder.Append(',');
  464. }
  465. SerializeValue(obj);
  466. first = false;
  467. }
  468. builder.Append(']');
  469. }
  470. void SerializeString(string str)
  471. {
  472. builder.Append('\"');
  473. var charArray = str.ToCharArray();
  474. foreach (var c in charArray)
  475. {
  476. switch (c)
  477. {
  478. case '"':
  479. builder.Append("\\\"");
  480. break;
  481. case '\\':
  482. builder.Append("\\\\");
  483. break;
  484. case '\b':
  485. builder.Append("\\b");
  486. break;
  487. case '\f':
  488. builder.Append("\\f");
  489. break;
  490. case '\n':
  491. builder.Append("\\n");
  492. break;
  493. case '\r':
  494. builder.Append("\\r");
  495. break;
  496. case '\t':
  497. builder.Append("\\t");
  498. break;
  499. default:
  500. var codepoint = Convert.ToInt32(c);
  501. if ((codepoint >= 32) && (codepoint <= 126))
  502. {
  503. builder.Append(c);
  504. }
  505. else
  506. {
  507. builder.Append("\\u");
  508. builder.Append(codepoint.ToString("x4"));
  509. }
  510. break;
  511. }
  512. }
  513. builder.Append('\"');
  514. }
  515. void SerializeOther(object value)
  516. {
  517. // Ensure we serialize Numbers using decimal (.) characters; avoid using comma (,) for
  518. // decimal separator. Use CultureInfo.InvariantCulture.
  519. // NOTE: decimals lose precision during serialization.
  520. // They always have, I'm just letting you know.
  521. // Previously floats and doubles lost precision too.
  522. if (value is float)
  523. {
  524. builder.Append(((float)value).ToString("R", CultureInfo.InvariantCulture));
  525. }
  526. else if (value is int
  527. || value is uint
  528. || value is long
  529. || value is sbyte
  530. || value is byte
  531. || value is short
  532. || value is ushort
  533. || value is ulong)
  534. {
  535. builder.Append(value);
  536. }
  537. else if (value is double
  538. || value is decimal)
  539. {
  540. builder.Append(Convert.ToDouble(value).ToString("R", CultureInfo.InvariantCulture));
  541. }
  542. else
  543. {
  544. SerializeString(value.ToString());
  545. }
  546. }
  547. }
  548. }
  549. // By Unity
  550. #region Extension methods
  551. /// <summary>
  552. /// Extension class for MiniJson to access values in JSON format.
  553. /// </summary>
  554. public static class MiniJsonExtensions
  555. {
  556. /// <summary>
  557. /// Get the HashDictionary of a key in JSON dictionary.
  558. /// </summary>
  559. /// <param name="dic">The JSON in dictionary representations.</param>
  560. /// <param name="key">The Key to get the HashDictionary from in the JSON dictionary.</param>
  561. /// <returns>The HashDictionary found in the JSON</returns>
  562. public static Dictionary<string, object> GetHash(this Dictionary<string, object> dic, string key)
  563. {
  564. return (Dictionary<string, object>)dic[key];
  565. }
  566. /// <summary>
  567. /// Get the casted enum in the JSON dictionary.
  568. /// </summary>
  569. /// <param name="dic">The JSON in dictionary representations.</param>
  570. /// <param name="key">The Key to get the casted enum from in the JSON dictionary.</param>
  571. /// <typeparam name="T">The class to cast the enum.</typeparam>
  572. /// <returns>The casted enum or will return T if the key was not found in the JSON dictionary.</returns>
  573. public static T GetEnum<T>(this Dictionary<string, object> dic, string key)
  574. {
  575. if (dic.ContainsKey(key))
  576. {
  577. return (T)Enum.Parse(typeof(T), dic[key].ToString(), true);
  578. }
  579. return default;
  580. }
  581. /// <summary>
  582. /// Get the string in the JSON dictionary.
  583. /// </summary>
  584. /// <param name="dic">The JSON in dictionary representations.</param>
  585. /// <param name="key">The Key to get the string from in the JSON dictionary.</param>
  586. /// <param name="defaultValue">The default value to send back if the JSON dictionary doesn't contains the key.</param>
  587. /// <returns>The string from the JSON dictionary or the default value if there is none</returns>
  588. public static string GetString(this Dictionary<string, object> dic, string key, string defaultValue = "")
  589. {
  590. if (dic.ContainsKey(key))
  591. {
  592. return dic[key].ToString();
  593. }
  594. return defaultValue;
  595. }
  596. /// <summary>
  597. /// Get the long in the JSON dictionary.
  598. /// </summary>
  599. /// <param name="dic">The JSON in dictionary representations.</param>
  600. /// <param name="key">The Key to get the long from in the JSON dictionary.</param>
  601. /// <returns>The long from the JSON dictionary or 0 if the key was not found in the JSON dictionary</returns>
  602. public static long GetLong(this Dictionary<string, object> dic, string key)
  603. {
  604. if (dic.ContainsKey(key))
  605. {
  606. return long.Parse(dic[key].ToString());
  607. }
  608. return 0;
  609. }
  610. /// <summary>
  611. /// Get the list of strings in the JSON dictionary.
  612. /// </summary>
  613. /// <param name="dic">The JSON in dictionary representations.</param>
  614. /// <param name="key">The Key to get the list of strings from in the JSON dictionary.</param>
  615. /// <returns>The list of strings from the JSON dictionary or an empty list of strings if the key was not found in the JSON dictionary</returns>
  616. public static List<string> GetStringList(this Dictionary<string, object> dic, string key)
  617. {
  618. if (dic.ContainsKey(key))
  619. {
  620. var result = new List<string>();
  621. var objs = (List<object>)dic[key];
  622. foreach (var v in objs)
  623. {
  624. result.Add(v.ToString());
  625. }
  626. return result;
  627. }
  628. return new List<string>();
  629. }
  630. /// <summary>
  631. /// Get the bool in the JSON dictionary.
  632. /// </summary>
  633. /// <param name="dic">The JSON in dictionary representations.</param>
  634. /// <param name="key">The Key to get the bool from in the JSON dictionary.</param>
  635. /// <returns>The bool from the JSON dictionary or false if the key was not found in the JSON dictionary</returns>
  636. public static bool GetBool(this Dictionary<string, object> dic, string key)
  637. {
  638. if (dic.ContainsKey(key))
  639. {
  640. return bool.Parse(dic[key].ToString());
  641. }
  642. return false;
  643. }
  644. /// <summary>
  645. /// Get the casted object in the JSON dictionary.
  646. /// </summary>
  647. /// <param name="dic">The JSON in dictionary representations.</param>
  648. /// <param name="key">The Key to get the casted object from in the JSON dictionary.</param>
  649. /// <typeparam name="T">The class to cast the object.</typeparam>
  650. /// <returns>The casted object or will return T if the key was not found in the JSON dictionary.</returns>
  651. public static T Get<T>(this Dictionary<string, object> dic, string key)
  652. {
  653. if (dic.ContainsKey(key))
  654. {
  655. return (T)dic[key];
  656. }
  657. return default;
  658. }
  659. /// <summary>
  660. /// Convert a Dictionary to JSON.
  661. /// </summary>
  662. /// <param name="obj">The dictionary to convert to JSON.</param>
  663. /// <returns>The converted dictionary in JSON string format.</returns>
  664. public static string toJson(this Dictionary<string, object> obj)
  665. {
  666. return MiniJson.JsonEncode(obj);
  667. }
  668. /// <summary>
  669. /// Convert a Dictionary to JSON.
  670. /// </summary>
  671. /// <param name="obj">The dictionary to convert to JSON.</param>
  672. /// <returns>The converted dictionary in JSON string format.</returns>
  673. public static string toJson(this Dictionary<string, string> obj)
  674. {
  675. return MiniJson.JsonEncode(obj);
  676. }
  677. /// <summary>
  678. /// Convert a string array to JSON.
  679. /// </summary>
  680. /// <param name="array">The string array to convert to JSON.</param>
  681. /// <returns>The converted dictionary in JSON string format.</returns>
  682. public static string toJson(this string[] array)
  683. {
  684. var list = new List<object>();
  685. foreach (var s in array)
  686. {
  687. list.Add(s);
  688. }
  689. return MiniJson.JsonEncode(list);
  690. }
  691. /// <summary>
  692. /// Convert string JSON into List of Objects.
  693. /// </summary>
  694. /// <param name="json">String JSON to convert.</param>
  695. /// <returns>List of Objects converted from string json.</returns>
  696. public static List<object> ArrayListFromJson(this string json)
  697. {
  698. return MiniJson.JsonDecode(json) as List<object>;
  699. }
  700. /// <summary>
  701. /// Convert string JSON into Dictionary.
  702. /// </summary>
  703. /// <param name="json">String JSON to convert.</param>
  704. /// <returns>Dictionary converted from string json.</returns>
  705. public static Dictionary<string, object> HashtableFromJson(this string json)
  706. {
  707. return MiniJson.JsonDecode(json) as Dictionary<string, object>;
  708. }
  709. }
  710. #endregion
  711. }
  712. /// <summary>
  713. /// Extension class for MiniJson to Encode and Decode JSON.
  714. /// </summary>
  715. public class MiniJson
  716. {
  717. /// <summary>
  718. /// Converts an object into a JSON string
  719. /// </summary>
  720. /// <param name="json">Object to convert to JSON string</param>
  721. /// <returns>JSON string</returns>
  722. public static string JsonEncode(object json)
  723. {
  724. return MiniJSON.Json.Serialize(json);
  725. }
  726. /// <summary>
  727. /// Converts an string into a JSON object
  728. /// </summary>
  729. /// <param name="json">String to convert to JSON object</param>
  730. /// <returns>JSON object</returns>
  731. public static object JsonDecode(string json)
  732. {
  733. return MiniJSON.Json.Deserialize(json);
  734. }
  735. }
  736. }