JsonReader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #region Header
  2. /**
  3. * JsonReader.cs
  4. * Stream-like access to JSON text.
  5. *
  6. * The authors disclaim copyright to this source code. For more details, see
  7. * the COPYING file included with this distribution.
  8. **/
  9. #endregion
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Text;
  15. namespace LitJson
  16. {
  17. public enum JsonToken
  18. {
  19. None,
  20. ObjectStart,
  21. PropertyName,
  22. ObjectEnd,
  23. ArrayStart,
  24. ArrayEnd,
  25. Int,
  26. Long,
  27. Double,
  28. String,
  29. Boolean,
  30. Null
  31. }
  32. public class JsonReader
  33. {
  34. #region Fields
  35. private static readonly IDictionary<int, IDictionary<int, int[]>> parse_table;
  36. private Stack<int> automaton_stack;
  37. private int current_input;
  38. private int current_symbol;
  39. private bool end_of_json;
  40. private bool end_of_input;
  41. private Lexer lexer;
  42. private bool parser_in_string;
  43. private bool parser_return;
  44. private bool read_started;
  45. private TextReader reader;
  46. private bool reader_is_owned;
  47. private bool skip_non_members;
  48. private object token_value;
  49. private JsonToken token;
  50. #endregion
  51. #region Public Properties
  52. public bool AllowComments {
  53. get { return lexer.AllowComments; }
  54. set { lexer.AllowComments = value; }
  55. }
  56. public bool AllowSingleQuotedStrings {
  57. get { return lexer.AllowSingleQuotedStrings; }
  58. set { lexer.AllowSingleQuotedStrings = value; }
  59. }
  60. public bool SkipNonMembers {
  61. get { return skip_non_members; }
  62. set { skip_non_members = value; }
  63. }
  64. public bool EndOfInput {
  65. get { return end_of_input; }
  66. }
  67. public bool EndOfJson {
  68. get { return end_of_json; }
  69. }
  70. public JsonToken Token {
  71. get { return token; }
  72. }
  73. public object Value {
  74. get { return token_value; }
  75. }
  76. #endregion
  77. #region Constructors
  78. static JsonReader ()
  79. {
  80. parse_table = PopulateParseTable ();
  81. }
  82. public JsonReader (string json_text) :
  83. this (new StringReader (json_text), true)
  84. {
  85. }
  86. public JsonReader (TextReader reader) :
  87. this (reader, false)
  88. {
  89. }
  90. private JsonReader (TextReader reader, bool owned)
  91. {
  92. if (reader == null)
  93. throw new ArgumentNullException ("reader");
  94. parser_in_string = false;
  95. parser_return = false;
  96. read_started = false;
  97. automaton_stack = new Stack<int> ();
  98. automaton_stack.Push ((int) ParserToken.End);
  99. automaton_stack.Push ((int) ParserToken.Text);
  100. lexer = new Lexer (reader);
  101. end_of_input = false;
  102. end_of_json = false;
  103. skip_non_members = true;
  104. this.reader = reader;
  105. reader_is_owned = owned;
  106. }
  107. #endregion
  108. #region Static Methods
  109. private static IDictionary<int, IDictionary<int, int[]>> PopulateParseTable ()
  110. {
  111. // See section A.2. of the manual for details
  112. IDictionary<int, IDictionary<int, int[]>> parse_table = new Dictionary<int, IDictionary<int, int[]>> ();
  113. TableAddRow (parse_table, ParserToken.Array);
  114. TableAddCol (parse_table, ParserToken.Array, '[',
  115. '[',
  116. (int) ParserToken.ArrayPrime);
  117. TableAddRow (parse_table, ParserToken.ArrayPrime);
  118. TableAddCol (parse_table, ParserToken.ArrayPrime, '"',
  119. (int) ParserToken.Value,
  120. (int) ParserToken.ValueRest,
  121. ']');
  122. TableAddCol (parse_table, ParserToken.ArrayPrime, '[',
  123. (int) ParserToken.Value,
  124. (int) ParserToken.ValueRest,
  125. ']');
  126. TableAddCol (parse_table, ParserToken.ArrayPrime, ']',
  127. ']');
  128. TableAddCol (parse_table, ParserToken.ArrayPrime, '{',
  129. (int) ParserToken.Value,
  130. (int) ParserToken.ValueRest,
  131. ']');
  132. TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.Number,
  133. (int) ParserToken.Value,
  134. (int) ParserToken.ValueRest,
  135. ']');
  136. TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.True,
  137. (int) ParserToken.Value,
  138. (int) ParserToken.ValueRest,
  139. ']');
  140. TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.False,
  141. (int) ParserToken.Value,
  142. (int) ParserToken.ValueRest,
  143. ']');
  144. TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.Null,
  145. (int) ParserToken.Value,
  146. (int) ParserToken.ValueRest,
  147. ']');
  148. TableAddRow (parse_table, ParserToken.Object);
  149. TableAddCol (parse_table, ParserToken.Object, '{',
  150. '{',
  151. (int) ParserToken.ObjectPrime);
  152. TableAddRow (parse_table, ParserToken.ObjectPrime);
  153. TableAddCol (parse_table, ParserToken.ObjectPrime, '"',
  154. (int) ParserToken.Pair,
  155. (int) ParserToken.PairRest,
  156. '}');
  157. TableAddCol (parse_table, ParserToken.ObjectPrime, '}',
  158. '}');
  159. TableAddRow (parse_table, ParserToken.Pair);
  160. TableAddCol (parse_table, ParserToken.Pair, '"',
  161. (int) ParserToken.String,
  162. ':',
  163. (int) ParserToken.Value);
  164. TableAddRow (parse_table, ParserToken.PairRest);
  165. TableAddCol (parse_table, ParserToken.PairRest, ',',
  166. ',',
  167. (int) ParserToken.Pair,
  168. (int) ParserToken.PairRest);
  169. TableAddCol (parse_table, ParserToken.PairRest, '}',
  170. (int) ParserToken.Epsilon);
  171. TableAddRow (parse_table, ParserToken.String);
  172. TableAddCol (parse_table, ParserToken.String, '"',
  173. '"',
  174. (int) ParserToken.CharSeq,
  175. '"');
  176. TableAddRow (parse_table, ParserToken.Text);
  177. TableAddCol (parse_table, ParserToken.Text, '[',
  178. (int) ParserToken.Array);
  179. TableAddCol (parse_table, ParserToken.Text, '{',
  180. (int) ParserToken.Object);
  181. TableAddCol(parse_table, ParserToken.Text, (int)ParserToken.Number,
  182. (int)ParserToken.Number);
  183. TableAddCol(parse_table, ParserToken.Text, (int)ParserToken.True,
  184. (int)ParserToken.True);
  185. TableAddCol(parse_table, ParserToken.Text, (int)ParserToken.False,
  186. (int)ParserToken.False);
  187. TableAddCol(parse_table, ParserToken.Text, '"',
  188. (int)ParserToken.String);
  189. TableAddRow (parse_table, ParserToken.Value);
  190. TableAddCol (parse_table, ParserToken.Value, '"',
  191. (int) ParserToken.String);
  192. TableAddCol (parse_table, ParserToken.Value, '[',
  193. (int) ParserToken.Array);
  194. TableAddCol (parse_table, ParserToken.Value, '{',
  195. (int) ParserToken.Object);
  196. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.Number,
  197. (int) ParserToken.Number);
  198. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.True,
  199. (int) ParserToken.True);
  200. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.False,
  201. (int) ParserToken.False);
  202. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.Null,
  203. (int) ParserToken.Null);
  204. TableAddRow (parse_table, ParserToken.ValueRest);
  205. TableAddCol (parse_table, ParserToken.ValueRest, ',',
  206. ',',
  207. (int) ParserToken.Value,
  208. (int) ParserToken.ValueRest);
  209. TableAddCol (parse_table, ParserToken.ValueRest, ']',
  210. (int) ParserToken.Epsilon);
  211. return parse_table;
  212. }
  213. private static void TableAddCol (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken row, int col,
  214. params int[] symbols)
  215. {
  216. parse_table[(int) row].Add (col, symbols);
  217. }
  218. private static void TableAddRow (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken rule)
  219. {
  220. parse_table.Add ((int) rule, new Dictionary<int, int[]> ());
  221. }
  222. #endregion
  223. #region Private Methods
  224. private void ProcessNumber (string number)
  225. {
  226. if (number.IndexOf ('.') != -1 ||
  227. number.IndexOf ('e') != -1 ||
  228. number.IndexOf ('E') != -1) {
  229. double n_double;
  230. if (double.TryParse (number, NumberStyles.Any, CultureInfo.InvariantCulture, out n_double)) {
  231. token = JsonToken.Double;
  232. token_value = n_double;
  233. return;
  234. }
  235. }
  236. int n_int32;
  237. if (int.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int32)) {
  238. token = JsonToken.Int;
  239. token_value = n_int32;
  240. return;
  241. }
  242. long n_int64;
  243. if (long.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int64)) {
  244. token = JsonToken.Long;
  245. token_value = n_int64;
  246. return;
  247. }
  248. ulong n_uint64;
  249. if (ulong.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_uint64))
  250. {
  251. token = JsonToken.Long;
  252. token_value = n_uint64;
  253. return;
  254. }
  255. // Shouldn't happen, but just in case, return something
  256. token = JsonToken.Int;
  257. token_value = 0;
  258. }
  259. private void ProcessSymbol ()
  260. {
  261. if (current_symbol == '[') {
  262. token = JsonToken.ArrayStart;
  263. parser_return = true;
  264. } else if (current_symbol == ']') {
  265. token = JsonToken.ArrayEnd;
  266. parser_return = true;
  267. } else if (current_symbol == '{') {
  268. token = JsonToken.ObjectStart;
  269. parser_return = true;
  270. } else if (current_symbol == '}') {
  271. token = JsonToken.ObjectEnd;
  272. parser_return = true;
  273. } else if (current_symbol == '"') {
  274. if (parser_in_string) {
  275. parser_in_string = false;
  276. parser_return = true;
  277. } else {
  278. if (token == JsonToken.None)
  279. token = JsonToken.String;
  280. parser_in_string = true;
  281. }
  282. } else if (current_symbol == (int) ParserToken.CharSeq) {
  283. token_value = lexer.StringValue;
  284. } else if (current_symbol == (int) ParserToken.False) {
  285. token = JsonToken.Boolean;
  286. token_value = false;
  287. parser_return = true;
  288. } else if (current_symbol == (int) ParserToken.Null) {
  289. token = JsonToken.Null;
  290. parser_return = true;
  291. } else if (current_symbol == (int) ParserToken.Number) {
  292. ProcessNumber (lexer.StringValue);
  293. parser_return = true;
  294. } else if (current_symbol == (int) ParserToken.Pair) {
  295. token = JsonToken.PropertyName;
  296. } else if (current_symbol == (int) ParserToken.True) {
  297. token = JsonToken.Boolean;
  298. token_value = true;
  299. parser_return = true;
  300. }
  301. }
  302. private bool ReadToken ()
  303. {
  304. if (end_of_input)
  305. return false;
  306. lexer.NextToken ();
  307. if (lexer.EndOfInput && automaton_stack.Peek() == (int)ParserToken.End) {
  308. Close ();
  309. return false;
  310. }
  311. current_input = lexer.Token;
  312. return true;
  313. }
  314. #endregion
  315. public void Close ()
  316. {
  317. if (end_of_input)
  318. return;
  319. end_of_input = true;
  320. end_of_json = true;
  321. if (reader_is_owned)
  322. {
  323. using(reader){}
  324. }
  325. reader = null;
  326. }
  327. public bool Read ()
  328. {
  329. if (end_of_input)
  330. return false;
  331. if (end_of_json) {
  332. end_of_json = false;
  333. automaton_stack.Clear ();
  334. automaton_stack.Push ((int) ParserToken.End);
  335. automaton_stack.Push ((int) ParserToken.Text);
  336. }
  337. parser_in_string = false;
  338. parser_return = false;
  339. token = JsonToken.None;
  340. token_value = null;
  341. if (! read_started) {
  342. read_started = true;
  343. if (! ReadToken ())
  344. return false;
  345. }
  346. int[] entry_symbols;
  347. while (true) {
  348. if (parser_return) {
  349. if (automaton_stack.Peek () == (int) ParserToken.End)
  350. end_of_json = true;
  351. return true;
  352. }
  353. current_symbol = automaton_stack.Pop ();
  354. ProcessSymbol ();
  355. if (current_symbol == current_input) {
  356. if (! ReadToken ()) {
  357. if (automaton_stack.Peek () != (int) ParserToken.End)
  358. throw new JsonException (
  359. "Input doesn't evaluate to proper JSON text");
  360. if (parser_return)
  361. return true;
  362. return false;
  363. }
  364. continue;
  365. }
  366. try {
  367. entry_symbols =
  368. parse_table[current_symbol][current_input];
  369. } catch (KeyNotFoundException e) {
  370. throw new JsonException ((ParserToken) current_input, e);
  371. }
  372. if (entry_symbols[0] == (int) ParserToken.Epsilon)
  373. continue;
  374. for (int i = entry_symbols.Length - 1; i >= 0; i--)
  375. automaton_stack.Push (entry_symbols[i]);
  376. }
  377. }
  378. }
  379. }