MiniJSON.cs 31 KB

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. Dictionary<string, object> 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. string 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. List<object> array = new List<object>();
  172. // ditch opening bracket
  173. json.Read();
  174. // [
  175. var parsing = true;
  176. while (parsing)
  177. {
  178. TOKEN 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. object value = ParseByToken(nextToken);
  190. array.Add(value);
  191. break;
  192. }
  193. }
  194. return array;
  195. }
  196. object ParseValue()
  197. {
  198. TOKEN 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. StringBuilder s = new StringBuilder();
  226. char c;
  227. // ditch opening quote
  228. json.Read();
  229. bool parsing = true;
  230. while (parsing)
  231. {
  232. if (json.Peek() == -1)
  233. {
  234. parsing = false;
  235. break;
  236. }
  237. c = NextChar;
  238. switch (c)
  239. {
  240. case '"':
  241. parsing = false;
  242. break;
  243. case '\\':
  244. if (json.Peek() == -1)
  245. {
  246. parsing = false;
  247. break;
  248. }
  249. c = NextChar;
  250. switch (c)
  251. {
  252. case '"':
  253. case '\\':
  254. case '/':
  255. s.Append(c);
  256. break;
  257. case 'b':
  258. s.Append('\b');
  259. break;
  260. case 'f':
  261. s.Append('\f');
  262. break;
  263. case 'n':
  264. s.Append('\n');
  265. break;
  266. case 'r':
  267. s.Append('\r');
  268. break;
  269. case 't':
  270. s.Append('\t');
  271. break;
  272. case 'u':
  273. var hex = new char[4];
  274. for (int i = 0; i < 4; i++)
  275. {
  276. hex[i] = NextChar;
  277. }
  278. s.Append((char)Convert.ToInt32(new string(hex), 16));
  279. break;
  280. }
  281. break;
  282. default:
  283. s.Append(c);
  284. break;
  285. }
  286. }
  287. return s.ToString();
  288. }
  289. object ParseNumber()
  290. {
  291. string number = NextWord;
  292. if (number.IndexOf('.') == -1 && number.IndexOf('e') == -1 && number.IndexOf('E') == -1)
  293. {
  294. long parsedInt;
  295. Int64.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedInt);
  296. return parsedInt;
  297. }
  298. double parsedDouble;
  299. Double.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedDouble);
  300. return parsedDouble;
  301. }
  302. void EatWhitespace()
  303. {
  304. while (Char.IsWhiteSpace(PeekChar))
  305. {
  306. json.Read();
  307. if (json.Peek() == -1)
  308. {
  309. break;
  310. }
  311. }
  312. }
  313. char PeekChar
  314. {
  315. get
  316. {
  317. return Convert.ToChar(json.Peek());
  318. }
  319. }
  320. char NextChar
  321. {
  322. get
  323. {
  324. return Convert.ToChar(json.Read());
  325. }
  326. }
  327. string NextWord
  328. {
  329. get
  330. {
  331. StringBuilder word = new StringBuilder();
  332. while (!IsWordBreak(PeekChar))
  333. {
  334. word.Append(NextChar);
  335. if (json.Peek() == -1)
  336. {
  337. break;
  338. }
  339. }
  340. return word.ToString();
  341. }
  342. }
  343. TOKEN NextToken
  344. {
  345. get
  346. {
  347. EatWhitespace();
  348. if (json.Peek() == -1)
  349. {
  350. return TOKEN.NONE;
  351. }
  352. switch (PeekChar)
  353. {
  354. case '{':
  355. return TOKEN.CURLY_OPEN;
  356. case '}':
  357. json.Read();
  358. return TOKEN.CURLY_CLOSE;
  359. case '[':
  360. return TOKEN.SQUARED_OPEN;
  361. case ']':
  362. json.Read();
  363. return TOKEN.SQUARED_CLOSE;
  364. case ',':
  365. json.Read();
  366. return TOKEN.COMMA;
  367. case '"':
  368. return TOKEN.STRING;
  369. case ':':
  370. return TOKEN.COLON;
  371. case '0':
  372. case '1':
  373. case '2':
  374. case '3':
  375. case '4':
  376. case '5':
  377. case '6':
  378. case '7':
  379. case '8':
  380. case '9':
  381. case '-':
  382. return TOKEN.NUMBER;
  383. }
  384. switch (NextWord)
  385. {
  386. case "false":
  387. return TOKEN.FALSE;
  388. case "true":
  389. return TOKEN.TRUE;
  390. case "null":
  391. return TOKEN.NULL;
  392. }
  393. return TOKEN.NONE;
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// Converts a IDictionary / IList object or a simple type (string, int, etc.) into a JSON string
  399. /// </summary>
  400. /// <param name="obj">A Dictionary&lt;string, object&gt; / List&lt;object&gt;</param>
  401. /// <returns>A JSON encoded string, or null if object 'json' is not serializable</returns>
  402. public static string Serialize(object obj)
  403. {
  404. return Serializer.Serialize(obj);
  405. }
  406. sealed class Serializer
  407. {
  408. StringBuilder builder;
  409. Serializer()
  410. {
  411. builder = new StringBuilder();
  412. }
  413. public static string Serialize(object obj)
  414. {
  415. var instance = new Serializer();
  416. instance.SerializeValue(obj);
  417. return instance.builder.ToString();
  418. }
  419. void SerializeValue(object value)
  420. {
  421. IList asList;
  422. IDictionary asDict;
  423. string asStr;
  424. if (value == null)
  425. {
  426. builder.Append("null");
  427. }
  428. else if ((asStr = value as string) != null)
  429. {
  430. SerializeString(asStr);
  431. }
  432. else if (value is bool)
  433. {
  434. builder.Append((bool)value ? "true" : "false");
  435. }
  436. else if ((asList = value as IList) != null)
  437. {
  438. SerializeArray(asList);
  439. }
  440. else if ((asDict = value as IDictionary) != null)
  441. {
  442. SerializeObject(asDict);
  443. }
  444. else if (value is char)
  445. {
  446. SerializeString(new string((char)value, 1));
  447. }
  448. else
  449. {
  450. SerializeOther(value);
  451. }
  452. }
  453. void SerializeObject(IDictionary obj)
  454. {
  455. bool first = true;
  456. builder.Append('{');
  457. foreach (object e in obj.Keys)
  458. {
  459. if (!first)
  460. {
  461. builder.Append(',');
  462. }
  463. SerializeString(e.ToString());
  464. builder.Append(':');
  465. SerializeValue(obj[e]);
  466. first = false;
  467. }
  468. builder.Append('}');
  469. }
  470. void SerializeArray(IList anArray)
  471. {
  472. builder.Append('[');
  473. bool first = true;
  474. foreach (object obj in anArray)
  475. {
  476. if (!first)
  477. {
  478. builder.Append(',');
  479. }
  480. SerializeValue(obj);
  481. first = false;
  482. }
  483. builder.Append(']');
  484. }
  485. void SerializeString(string str)
  486. {
  487. builder.Append('\"');
  488. char[] charArray = str.ToCharArray();
  489. foreach (var c in charArray)
  490. {
  491. switch (c)
  492. {
  493. case '"':
  494. builder.Append("\\\"");
  495. break;
  496. case '\\':
  497. builder.Append("\\\\");
  498. break;
  499. case '\b':
  500. builder.Append("\\b");
  501. break;
  502. case '\f':
  503. builder.Append("\\f");
  504. break;
  505. case '\n':
  506. builder.Append("\\n");
  507. break;
  508. case '\r':
  509. builder.Append("\\r");
  510. break;
  511. case '\t':
  512. builder.Append("\\t");
  513. break;
  514. default:
  515. int codepoint = Convert.ToInt32(c);
  516. if ((codepoint >= 32) && (codepoint <= 126))
  517. {
  518. builder.Append(c);
  519. }
  520. else
  521. {
  522. builder.Append("\\u");
  523. builder.Append(codepoint.ToString("x4"));
  524. }
  525. break;
  526. }
  527. }
  528. builder.Append('\"');
  529. }
  530. void SerializeOther(object value)
  531. {
  532. // Ensure we serialize Numbers using decimal (.) characters; avoid using comma (,) for
  533. // decimal separator. Use CultureInfo.InvariantCulture.
  534. // NOTE: decimals lose precision during serialization.
  535. // They always have, I'm just letting you know.
  536. // Previously floats and doubles lost precision too.
  537. if (value is float)
  538. {
  539. builder.Append(((float)value).ToString("R", CultureInfo.InvariantCulture));
  540. }
  541. else if (value is int
  542. || value is uint
  543. || value is long
  544. || value is sbyte
  545. || value is byte
  546. || value is short
  547. || value is ushort
  548. || value is ulong)
  549. {
  550. builder.Append(value);
  551. }
  552. else if (value is double
  553. || value is decimal)
  554. {
  555. builder.Append(Convert.ToDouble(value).ToString("R", CultureInfo.InvariantCulture));
  556. }
  557. else
  558. {
  559. SerializeString(value.ToString());
  560. }
  561. }
  562. }
  563. }
  564. // By Unity
  565. #region Extension methods
  566. /// <summary>
  567. /// Extension class for MiniJson to access values in JSON format.
  568. /// </summary>
  569. public static class MiniJsonExtensions
  570. {
  571. /// <summary>
  572. /// Get the HashDictionary of a key in JSON dictionary.
  573. /// </summary>
  574. /// <param name="dic">The JSON in dictionary representations.</param>
  575. /// <param name="key">The Key to get the HashDictionary from in the JSON dictionary.</param>
  576. /// <returns>The HashDictionary found in the JSON</returns>
  577. public static Dictionary<string, object> GetHash(this Dictionary<string, object> dic, string key)
  578. {
  579. return (Dictionary<string, object>)dic[key];
  580. }
  581. /// <summary>
  582. /// Get the casted enum 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 casted enum from in the JSON dictionary.</param>
  586. /// <typeparam name="T">The class to cast the enum.</typeparam>
  587. /// <returns>The casted enum or will return T if the key was not found in the JSON dictionary.</returns>
  588. public static T GetEnum<T>(this Dictionary<string, object> dic, string key)
  589. {
  590. if (dic.ContainsKey(key))
  591. return (T)Enum.Parse(typeof(T), dic[key].ToString(), true);
  592. return default(T);
  593. }
  594. /// <summary>
  595. /// Get the string in the JSON dictionary.
  596. /// </summary>
  597. /// <param name="dic">The JSON in dictionary representations.</param>
  598. /// <param name="key">The Key to get the string from in the JSON dictionary.</param>
  599. /// <param name="defaultValue">The default value to send back if the JSON dictionary doesn't contains the key.</param>
  600. /// <returns>The string from the JSON dictionary or the default value if there is none</returns>
  601. public static string GetString(this Dictionary<string, object> dic, string key, string defaultValue = "")
  602. {
  603. if (dic.ContainsKey(key))
  604. return dic[key].ToString();
  605. return defaultValue;
  606. }
  607. /// <summary>
  608. /// Get the long in the JSON dictionary.
  609. /// </summary>
  610. /// <param name="dic">The JSON in dictionary representations.</param>
  611. /// <param name="key">The Key to get the long from in the JSON dictionary.</param>
  612. /// <returns>The long from the JSON dictionary or 0 if the key was not found in the JSON dictionary</returns>
  613. public static long GetLong(this Dictionary<string, object> dic, string key)
  614. {
  615. if (dic.ContainsKey(key))
  616. return long.Parse(dic[key].ToString());
  617. return 0;
  618. }
  619. /// <summary>
  620. /// Get the list of strings in the JSON dictionary.
  621. /// </summary>
  622. /// <param name="dic">The JSON in dictionary representations.</param>
  623. /// <param name="key">The Key to get the list of strings from in the JSON dictionary.</param>
  624. /// <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>
  625. public static List<string> GetStringList(this Dictionary<string, object> dic, string key)
  626. {
  627. if (dic.ContainsKey(key))
  628. {
  629. List<string> result = new List<string>();
  630. var objs = (List<object>)dic[key];
  631. foreach (var v in objs)
  632. result.Add(v.ToString());
  633. return result;
  634. }
  635. return new List<string>();
  636. }
  637. /// <summary>
  638. /// Get the bool in the JSON dictionary.
  639. /// </summary>
  640. /// <param name="dic">The JSON in dictionary representations.</param>
  641. /// <param name="key">The Key to get the bool from in the JSON dictionary.</param>
  642. /// <returns>The bool from the JSON dictionary or false if the key was not found in the JSON dictionary</returns>
  643. public static bool GetBool(this Dictionary<string, object> dic, string key)
  644. {
  645. if (dic.ContainsKey(key))
  646. return bool.Parse(dic[key].ToString());
  647. return false;
  648. }
  649. /// <summary>
  650. /// Get the casted object in the JSON dictionary.
  651. /// </summary>
  652. /// <param name="dic">The JSON in dictionary representations.</param>
  653. /// <param name="key">The Key to get the casted object from in the JSON dictionary.</param>
  654. /// <typeparam name="T">The class to cast the object.</typeparam>
  655. /// <returns>The casted object or will return T if the key was not found in the JSON dictionary.</returns>
  656. public static T Get<T>(this Dictionary<string, object> dic, string key)
  657. {
  658. if (dic.ContainsKey(key))
  659. return (T)dic[key];
  660. return default(T);
  661. }
  662. /// <summary>
  663. /// Convert a Dictionary to JSON.
  664. /// </summary>
  665. /// <param name="obj">The dictionary to convert to JSON.</param>
  666. /// <returns>The converted dictionary in JSON string format.</returns>
  667. public static string toJson(this Dictionary<string, object> obj)
  668. {
  669. return MiniJson.JsonEncode(obj);
  670. }
  671. /// <summary>
  672. /// Convert a Dictionary to JSON.
  673. /// </summary>
  674. /// <param name="obj">The dictionary to convert to JSON.</param>
  675. /// <returns>The converted dictionary in JSON string format.</returns>
  676. public static string toJson(this Dictionary<string, string> obj)
  677. {
  678. return MiniJson.JsonEncode(obj);
  679. }
  680. /// <summary>
  681. /// Convert a string array to JSON.
  682. /// </summary>
  683. /// <param name="array">The string array to convert to JSON.</param>
  684. /// <returns>The converted dictionary in JSON string format.</returns>
  685. public static string toJson(this string[] array)
  686. {
  687. var list = new List<object>();
  688. foreach (var s in array)
  689. list.Add(s);
  690. return MiniJson.JsonEncode(list);
  691. }
  692. /// <summary>
  693. /// Convert string JSON into List of Objects.
  694. /// </summary>
  695. /// <param name="json">String JSON to convert.</param>
  696. /// <returns>List of Objects converted from string json.</returns>
  697. public static List<object> ArrayListFromJson(this string json)
  698. {
  699. return MiniJson.JsonDecode(json) as List<object>;
  700. }
  701. /// <summary>
  702. /// Convert string JSON into Dictionary.
  703. /// </summary>
  704. /// <param name="json">String JSON to convert.</param>
  705. /// <returns>Dictionary converted from string json.</returns>
  706. public static Dictionary<string, object> HashtableFromJson(this string json)
  707. {
  708. return MiniJson.JsonDecode(json) as Dictionary<string, object>;
  709. }
  710. }
  711. #endregion
  712. }
  713. /// <summary>
  714. /// Extension class for MiniJson to Encode and Decode JSON.
  715. /// </summary>
  716. public class MiniJson
  717. {
  718. /// <summary>
  719. /// Converts an object into a JSON string
  720. /// </summary>
  721. /// <param name="json">Object to convert to JSON string</param>
  722. /// <returns>JSON string</returns>
  723. public static string JsonEncode(object json)
  724. {
  725. return MiniJSON.Json.Serialize(json);
  726. }
  727. /// <summary>
  728. /// Converts an string into a JSON object
  729. /// </summary>
  730. /// <param name="json">String to convert to JSON object</param>
  731. /// <returns>JSON object</returns>
  732. public static object JsonDecode(string json)
  733. {
  734. return MiniJSON.Json.Deserialize(json);
  735. }
  736. }
  737. }