JsonParser.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. internal class JsonParser
  6. {
  7. private enum Token
  8. {
  9. None = -1,
  10. Curly_Open,
  11. Curly_Close,
  12. Squared_Open,
  13. Squared_Close,
  14. Colon,
  15. Comma,
  16. String,
  17. Number,
  18. True,
  19. False,
  20. Null
  21. }
  22. private readonly char[] json;
  23. private readonly StringBuilder s = new StringBuilder();
  24. private Token lookAheadToken = Token.None;
  25. private int index;
  26. private bool _ignorecase = false;
  27. internal JsonParser(string json, bool ignorecase)
  28. {
  29. this.json = json.ToCharArray();
  30. _ignorecase = ignorecase;
  31. }
  32. public object Decode()
  33. {
  34. return ParseValue();
  35. }
  36. private Dictionary<string, object> ParseObject()
  37. {
  38. Dictionary<string, object> table = new Dictionary<string, object>();
  39. ConsumeToken();
  40. while (true)
  41. {
  42. switch (LookAhead())
  43. {
  44. case Token.Comma:
  45. ConsumeToken();
  46. continue;
  47. case Token.Curly_Close:
  48. ConsumeToken();
  49. return table;
  50. }
  51. string name = ParseString();
  52. if (_ignorecase)
  53. {
  54. name = name.ToLower();
  55. }
  56. if (NextToken() != Token.Colon)
  57. {
  58. throw new Exception("Expected colon at index " + index);
  59. }
  60. object value = (table[name] = ParseValue());
  61. }
  62. }
  63. private ArrayList ParseArray()
  64. {
  65. ArrayList array = new ArrayList();
  66. ConsumeToken();
  67. while (true)
  68. {
  69. switch (LookAhead())
  70. {
  71. case Token.Comma:
  72. ConsumeToken();
  73. break;
  74. case Token.Squared_Close:
  75. ConsumeToken();
  76. return array;
  77. default:
  78. array.Add(ParseValue());
  79. break;
  80. }
  81. }
  82. }
  83. private object ParseValue()
  84. {
  85. switch (LookAhead())
  86. {
  87. case Token.Number:
  88. return ParseNumber();
  89. case Token.String:
  90. return ParseString();
  91. case Token.Curly_Open:
  92. return ParseObject();
  93. case Token.Squared_Open:
  94. return ParseArray();
  95. case Token.True:
  96. ConsumeToken();
  97. return true;
  98. case Token.False:
  99. ConsumeToken();
  100. return false;
  101. case Token.Null:
  102. ConsumeToken();
  103. return null;
  104. default:
  105. throw new Exception("Unrecognized token at index" + index);
  106. }
  107. }
  108. private string ParseString()
  109. {
  110. ConsumeToken();
  111. s.Length = 0;
  112. int runIndex = -1;
  113. while (index < json.Length)
  114. {
  115. switch (json[index++])
  116. {
  117. case '"':
  118. if (runIndex != -1)
  119. {
  120. if (s.Length == 0)
  121. {
  122. return new string(json, runIndex, index - runIndex - 1);
  123. }
  124. s.Append(json, runIndex, index - runIndex - 1);
  125. }
  126. return s.ToString();
  127. default:
  128. if (runIndex == -1)
  129. {
  130. runIndex = index - 1;
  131. }
  132. continue;
  133. case '\\':
  134. break;
  135. }
  136. if (index == json.Length)
  137. {
  138. break;
  139. }
  140. if (runIndex != -1)
  141. {
  142. s.Append(json, runIndex, index - runIndex - 1);
  143. runIndex = -1;
  144. }
  145. switch (json[index++])
  146. {
  147. case '"':
  148. s.Append('"');
  149. break;
  150. case '\\':
  151. s.Append('\\');
  152. break;
  153. case '/':
  154. s.Append('/');
  155. break;
  156. case 'b':
  157. s.Append('\b');
  158. break;
  159. case 'f':
  160. s.Append('\f');
  161. break;
  162. case 'n':
  163. s.Append('\n');
  164. break;
  165. case 'r':
  166. s.Append('\r');
  167. break;
  168. case 't':
  169. s.Append('\t');
  170. break;
  171. case 'u':
  172. {
  173. int remainingLength = json.Length - index;
  174. if (remainingLength >= 4)
  175. {
  176. uint codePoint = ParseUnicode(json[index], json[index + 1], json[index + 2], json[index + 3]);
  177. s.Append((char)codePoint);
  178. index += 4;
  179. }
  180. break;
  181. }
  182. }
  183. }
  184. throw new Exception("Unexpectedly reached end of string");
  185. }
  186. private uint ParseSingleChar(char c1, uint multipliyer)
  187. {
  188. uint p1 = 0u;
  189. if (c1 >= '0' && c1 <= '9')
  190. {
  191. p1 = (uint)(c1 - 48) * multipliyer;
  192. }
  193. else if (c1 >= 'A' && c1 <= 'F')
  194. {
  195. p1 = (uint)(c1 - 65 + 10) * multipliyer;
  196. }
  197. else if (c1 >= 'a' && c1 <= 'f')
  198. {
  199. p1 = (uint)(c1 - 97 + 10) * multipliyer;
  200. }
  201. return p1;
  202. }
  203. private uint ParseUnicode(char c1, char c2, char c3, char c4)
  204. {
  205. uint p1 = ParseSingleChar(c1, 4096u);
  206. uint p2 = ParseSingleChar(c2, 256u);
  207. uint p3 = ParseSingleChar(c3, 16u);
  208. uint p4 = ParseSingleChar(c4, 1u);
  209. return p1 + p2 + p3 + p4;
  210. }
  211. private string ParseNumber()
  212. {
  213. ConsumeToken();
  214. int startIndex = index - 1;
  215. while (true)
  216. {
  217. char c = json[index];
  218. if ((c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E')
  219. {
  220. if (++index == json.Length)
  221. {
  222. throw new Exception("Unexpected end of string whilst parsing number");
  223. }
  224. bool flag = true;
  225. continue;
  226. }
  227. break;
  228. }
  229. return new string(json, startIndex, index - startIndex);
  230. }
  231. private Token LookAhead()
  232. {
  233. if (lookAheadToken != Token.None)
  234. {
  235. return lookAheadToken;
  236. }
  237. return lookAheadToken = NextTokenCore();
  238. }
  239. private void ConsumeToken()
  240. {
  241. lookAheadToken = Token.None;
  242. }
  243. private Token NextToken()
  244. {
  245. Token result = ((lookAheadToken != Token.None) ? lookAheadToken : NextTokenCore());
  246. lookAheadToken = Token.None;
  247. return result;
  248. }
  249. private Token NextTokenCore()
  250. {
  251. char c;
  252. do
  253. {
  254. c = json[index];
  255. }
  256. while (c <= ' ' && (c == ' ' || c == '\t' || c == '\n' || c == '\r') && ++index < json.Length);
  257. if (index == json.Length)
  258. {
  259. throw new Exception("Reached end of string unexpectedly");
  260. }
  261. c = json[index];
  262. index++;
  263. switch (c)
  264. {
  265. case '{':
  266. return Token.Curly_Open;
  267. case '}':
  268. return Token.Curly_Close;
  269. case '[':
  270. return Token.Squared_Open;
  271. case ']':
  272. return Token.Squared_Close;
  273. case ',':
  274. return Token.Comma;
  275. case '"':
  276. return Token.String;
  277. case '+':
  278. case '-':
  279. case '.':
  280. case '0':
  281. case '1':
  282. case '2':
  283. case '3':
  284. case '4':
  285. case '5':
  286. case '6':
  287. case '7':
  288. case '8':
  289. case '9':
  290. return Token.Number;
  291. case ':':
  292. return Token.Colon;
  293. case 'f':
  294. if (json.Length - index >= 4 && json[index] == 'a' && json[index + 1] == 'l' && json[index + 2] == 's' && json[index + 3] == 'e')
  295. {
  296. index += 4;
  297. return Token.False;
  298. }
  299. break;
  300. case 't':
  301. if (json.Length - index >= 3 && json[index] == 'r' && json[index + 1] == 'u' && json[index + 2] == 'e')
  302. {
  303. index += 3;
  304. return Token.True;
  305. }
  306. break;
  307. case 'n':
  308. if (json.Length - index >= 3 && json[index] == 'u' && json[index + 1] == 'l' && json[index + 2] == 'l')
  309. {
  310. index += 3;
  311. return Token.Null;
  312. }
  313. break;
  314. }
  315. throw new Exception("Could not find token at index " + --index);
  316. }
  317. }