[关闭]
@zwh8800 2017-08-23T10:23:01.000000Z 字数 6651 阅读 191144

寒假写了个 JSON 解析器

blog 归档 JSON 解析器


一个 JSON 解析器


使用方法:

  1. string data = File.ReadAllText(@"f:\1.Json");
  2. dynamic obj = Json.Parse(data);
  3. Console.WriteLine(obj.employees[0].firstName);
  4. foreach (var it in obj.employees)
  5. {
  6. Console.WriteLine(it.lastName);
  7. }

假设 json 文件如下:

  1. {
  2. "employees": [
  3. {
  4. "firstName": "Bill",
  5. "lastName": "Gates"
  6. },
  7. {
  8. "firstName": "George",
  9. "lastName": "Bush"
  10. },
  11. {
  12. "firstName": "Thomas",
  13. "lastName": "Carter"
  14. }
  15. ]
  16. }

使用了 c#4.0 的动态特性, c# 越来越像动态语言了, 用着真舒服.
代码如下, 就是个递归下降:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Dynamic;
  7. namespace Json
  8. {
  9. public class Json
  10. {
  11. private class JsonObject : DynamicObject
  12. {
  13. public JsonObject(Dictionary<string, dynamic> member)
  14. {
  15. this.member = member;
  16. }
  17. public override bool TryGetMember(GetMemberBinder binder, out object result)
  18. {
  19. if (member.ContainsKey(binder.Name))
  20. {
  21. result = member[binder.Name];
  22. return true;
  23. }
  24. else
  25. {
  26. result = null;
  27. return false;
  28. }
  29. }
  30. private Dictionary<string, dynamic> member;
  31. }
  32. private static int CharToInt(char c)
  33. {
  34. switch (c)
  35. {
  36. case '0':
  37. return 0;
  38. case '1':
  39. return 1;
  40. case '2':
  41. return 2;
  42. case '3':
  43. return 3;
  44. case '4':
  45. return 4;
  46. case '5':
  47. return 5;
  48. case '6':
  49. return 6;
  50. case '7':
  51. return 7;
  52. case '8':
  53. return 8;
  54. case '9':
  55. return 9;
  56. }
  57. throw new Exception("c不是一个数字字符");
  58. }
  59. private enum TokenType
  60. {
  61. LBrace, RBrace,
  62. LBracket, RBracket,
  63. Colon, Comma,
  64. True, False,
  65. Null, String,
  66. Number
  67. }
  68. private struct Token
  69. {
  70. public TokenType type;
  71. public dynamic value;
  72. }
  73. private enum NumberState
  74. {
  75. Start, MinusRead, ZeroRead, InInt, DotRead, InFraction, ERead, InExponent, End
  76. }
  77. private static Token GetToken(string json, ref int i)
  78. {
  79. while (i < json.Length && char.IsWhiteSpace(json[i]))
  80. i++;
  81. Token token = new Token();
  82. string sub = json.Substring(i);
  83. if (json[i] == '{')
  84. {
  85. token.type = TokenType.LBrace;
  86. i = i + 1;
  87. }
  88. else if (json[i] == '}')
  89. {
  90. token.type = TokenType.RBrace;
  91. i = i + 1;
  92. }
  93. else if (json[i] == '[')
  94. {
  95. token.type = TokenType.LBracket;
  96. i = i + 1;
  97. }
  98. else if (json[i] == ']')
  99. {
  100. token.type = TokenType.RBracket;
  101. i = i + 1;
  102. }
  103. else if (json[i] == ':')
  104. {
  105. token.type = TokenType.Colon;
  106. i = i + 1;
  107. }
  108. else if (json[i] == ',')
  109. {
  110. token.type = TokenType.Comma;
  111. i = i + 1;
  112. }
  113. else if (sub.StartsWith("true"))
  114. {
  115. token.type = TokenType.True;
  116. i = i + 4;
  117. }
  118. else if (sub.StartsWith("false"))
  119. {
  120. token.type = TokenType.False;
  121. i = i + 4;
  122. }
  123. else if (sub.StartsWith("null"))
  124. {
  125. token.type = TokenType.Null;
  126. i = i + 4;
  127. }
  128. else if (json[i] == '"') //string
  129. {
  130. StringBuilder sb = new StringBuilder();
  131. i++;
  132. while (i < json.Length)
  133. {
  134. int len = 0;
  135. while (i + len < json.Length && json[i + len] != '"' && json[i + len] != '\\')
  136. len++;
  137. sb.Append(json.Substring(i, len));
  138. if (i + len >= json.Length)
  139. {
  140. throw new Exception("未找到匹配的引号");
  141. }
  142. else if (json[i + len] == '"')
  143. {
  144. i += len + 1;
  145. break;
  146. }
  147. i += len + 1;
  148. switch (json[i])
  149. {
  150. case '"':
  151. sb.Append('"');
  152. i++;
  153. break;
  154. case '\\':
  155. sb.Append('\\');
  156. i++;
  157. break;
  158. case '/':
  159. sb.Append('/');
  160. i++;
  161. break;
  162. case 'b':
  163. sb.Append('\b');
  164. i++;
  165. break;
  166. case 'f':
  167. sb.Append('\f');
  168. i++;
  169. break;
  170. case 'n':
  171. sb.Append('\n');
  172. i++;
  173. break;
  174. case 'r':
  175. sb.Append('\r');
  176. i++;
  177. break;
  178. case 't':
  179. sb.Append('\t');
  180. i++;
  181. break;
  182. case 'u':
  183. string hex = json.Substring(i + 1, 4);
  184. ushort code = Convert.ToUInt16(hex, 16);
  185. char c = (char)code;
  186. sb.Append(c);
  187. i += 5;
  188. break;
  189. }
  190. }
  191. token.value = sb.ToString();
  192. token.type = TokenType.String;
  193. }
  194. else if (json[i] == '-' || char.IsNumber(json[i])) //nubmer
  195. {
  196. dynamic value = 0;
  197. bool isMinus = false;
  198. int frac = 10;
  199. bool isExponentNegative = false;
  200. int exponent = 0;
  201. NumberState state = NumberState.Start;
  202. while (state != NumberState.End)
  203. {
  204. switch (state)
  205. {
  206. case NumberState.Start:
  207. if (json[i] == '-')
  208. {
  209. isMinus = true;
  210. state = NumberState.MinusRead;
  211. }
  212. else if (json[i] == '0')
  213. {
  214. state = NumberState.ZeroRead;
  215. }
  216. else if (char.IsNumber(json[i]))
  217. {
  218. value = CharToInt(json[i]);
  219. state = NumberState.InInt;
  220. }
  221. else
  222. {
  223. throw new Exception("不能识别的字符‘" + json[i] + "’");
  224. }
  225. i++;
  226. break;
  227. case NumberState.MinusRead:
  228. if (json[i] == '0')
  229. {
  230. state = NumberState.ZeroRead;
  231. }
  232. else if (char.IsNumber(json[i]))
  233. {
  234. value = CharToInt(json[i]);
  235. state = NumberState.InInt;
  236. }
  237. else
  238. {
  239. throw new Exception("不能识别的字符‘" + json[i] + "’");
  240. }
  241. i++;
  242. break;
  243. case NumberState.InInt:
  244. if (char.IsNumber(json[i]))
  245. {
  246. value *= 10;
  247. value += CharToInt(json[i]);
  248. }
  249. else if (json[i] == '.')
  250. {
  251. state = NumberState.DotRead;
  252. }
  253. else if (json[i] == 'e' || json[i] == 'E')
  254. {
  255. state = NumberState.ERead;
  256. }
  257. else
  258. {
  259. i--;
  260. state = NumberState.End;
  261. }
  262. i++;
  263. break;
  264. case NumberState.ZeroRead:
  265. if (json[i] == '.')
  266. {
  267. state = NumberState.DotRead;
  268. }
  269. else if (json[i] == 'e' || json[i] == 'E')
  270. {
  271. state = NumberState.ERead;
  272. }
  273. else
  274. {
  275. i--;
  276. state = NumberState.End;
  277. }
  278. i++;
  279. break;
  280. case NumberState.DotRead:
  281. if (char.IsNumber(json[i]))
  282. {
  283. value += (double)CharToInt(json[i]) / frac;
  284. frac *= 10;
  285. state = NumberState.InFraction;
  286. }
  287. else
  288. {
  289. throw new Exception("不能识别的字符‘" + json[i] + "’");
  290. }
  291. i++;
  292. break;
  293. case NumberState.InFraction:
  294. if (char.IsNumber(json[i]))
  295. {
  296. value += (double)CharToInt(json[i]) / frac;
  297. frac *= 10;
  298. }
  299. else if (json[i] == 'e' || json[i] == 'E')
  300. {
  301. state = NumberState.ERead;
  302. }
  303. else
  304. {
  305. i--;
  306. state = NumberState.End;
  307. }
  308. i++;
  309. break;
  310. case NumberState.ERead:
  311. if (char.IsNumber(json[i]))
  312. {
  313. exponent = CharToInt(json[i]);
  314. state = NumberState.InExponent;
  315. }
  316. else if (json[i] == '+')
  317. {
  318. state = NumberState.InExponent;
  319. }
  320. else if (json[i] == '-')
  321. {
  322. isExponentNegative = true;
  323. state = NumberState.InExponent;
  324. }
  325. else
  326. {
  327. throw new Exception("不能识别的字符‘" + json[i] + "’");
  328. }
  329. i++;
  330. break;
  331. case NumberState.InExponent:
  332. if (char.IsNumber(json[i]))
  333. {
  334. exponent *= 10;
  335. exponent = CharToInt(json[i]);
  336. }
  337. else
  338. {
  339. i--;
  340. state = NumberState.End;
  341. }
  342. i++;
  343. break;
  344. }
  345. if (i >= json.Length)
  346. state = NumberState.End;
  347. }
  348. if (exponent != 0)
  349. {
  350. if (isExponentNegative)
  351. {
  352. value = value * Math.Pow(10, -exponent);
  353. }
  354. else
  355. {
  356. value = value * Math.Pow(10, exponent);
  357. }
  358. }
  359. if (isMinus)
  360. token.value = -value;
  361. else
  362. token.value = value;
  363. token.type = TokenType.Number;
  364. }
  365. else
  366. throw new Exception("不能识别的字符‘" + json[i] + "’");
  367. while (i < json.Length && char.IsWhiteSpace(json[i]))
  368. i++;
  369. return token;
  370. }
  371. private static dynamic Value(string json, ref int i)
  372. {
  373. Token token = GetToken(json, ref i);
  374. if (token.type == TokenType.Null)
  375. return null;
  376. else if (token.type == TokenType.False)
  377. return false;
  378. else if (token.type == TokenType.True)
  379. return true;
  380. else if (token.type == TokenType.Number)
  381. return token.value;
  382. else if (token.type == TokenType.String)
  383. return token.value;
  384. else if (token.type == TokenType.LBrace)
  385. {
  386. Dictionary<string, dynamic> member = new Dictionary<string, dynamic>();
  387. bool first = true;
  388. while (true)
  389. {
  390. if (first)
  391. {
  392. int tmpi = i;
  393. token = GetToken(json, ref i);
  394. if (token.type == TokenType.RBrace)
  395. break;
  396. else
  397. i = tmpi;
  398. first = false;
  399. }
  400. token = GetToken(json, ref i);
  401. if (token.type != TokenType.String)
  402. {
  403. throw new Exception("出乎意料的Token: " + token.type);
  404. }
  405. string name = token.value;
  406. token = GetToken(json, ref i);
  407. if (token.type != TokenType.Colon)
  408. {
  409. throw new Exception("出乎意料的Token: " + token.type);
  410. }
  411. dynamic val = Value(json, ref i);
  412. member[name] = val;
  413. token = GetToken(json, ref i);
  414. if (token.type == TokenType.Comma)
  415. continue;
  416. else if (token.type == TokenType.RBrace)
  417. break;
  418. else
  419. throw new Exception("出乎意料的Token: " + token.type);
  420. }
  421. return new JsonObject(member);
  422. }
  423. else if (token.type == TokenType.LBracket)
  424. {
  425. List<dynamic> list = new List<dynamic>();
  426. bool first = true;
  427. while (true)
  428. {
  429. if (first)
  430. {
  431. int tmpi = i;
  432. token = GetToken(json, ref i);
  433. if (token.type == TokenType.RBracket)
  434. break;
  435. else
  436. i = tmpi;
  437. first = false;
  438. }
  439. list.Add(Value(json, ref i));
  440. token = GetToken(json, ref i);
  441. if (token.type == TokenType.Comma)
  442. continue;
  443. else if (token.type == TokenType.RBracket)
  444. break;
  445. else
  446. throw new Exception("出乎意料的Token: " + token.type);
  447. }
  448. return list.ToArray();
  449. }
  450. else
  451. {
  452. throw new Exception("出乎意料的Token: " + token.type);
  453. }
  454. }
  455. public static dynamic Parse(string json)
  456. {
  457. int i = 0;
  458. return Value(json, ref i);
  459. }
  460. }
  461. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注