JsonReader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. TableAddRow (parse_table, ParserToken.Value);
  182. TableAddCol (parse_table, ParserToken.Value, '"',
  183. (int) ParserToken.String);
  184. TableAddCol (parse_table, ParserToken.Value, '[',
  185. (int) ParserToken.Array);
  186. TableAddCol (parse_table, ParserToken.Value, '{',
  187. (int) ParserToken.Object);
  188. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.Number,
  189. (int) ParserToken.Number);
  190. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.True,
  191. (int) ParserToken.True);
  192. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.False,
  193. (int) ParserToken.False);
  194. TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.Null,
  195. (int) ParserToken.Null);
  196. TableAddRow (parse_table, ParserToken.ValueRest);
  197. TableAddCol (parse_table, ParserToken.ValueRest, ',',
  198. ',',
  199. (int) ParserToken.Value,
  200. (int) ParserToken.ValueRest);
  201. TableAddCol (parse_table, ParserToken.ValueRest, ']',
  202. (int) ParserToken.Epsilon);
  203. return parse_table;
  204. }
  205. private static void TableAddCol (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken row, int col,
  206. params int[] symbols)
  207. {
  208. parse_table[(int) row].Add (col, symbols);
  209. }
  210. private static void TableAddRow (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken rule)
  211. {
  212. parse_table.Add ((int) rule, new Dictionary<int, int[]> ());
  213. }
  214. #endregion
  215. #region Private Methods
  216. private void ProcessNumber (string number)
  217. {
  218. if (number.IndexOf ('.') != -1 ||
  219. number.IndexOf ('e') != -1 ||
  220. number.IndexOf ('E') != -1) {
  221. double n_double;
  222. if (double.TryParse (number, NumberStyles.Any, CultureInfo.InvariantCulture, out n_double)) {
  223. token = JsonToken.Double;
  224. token_value = n_double;
  225. return;
  226. }
  227. }
  228. int n_int32;
  229. if (int.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int32)) {
  230. token = JsonToken.Int;
  231. token_value = n_int32;
  232. return;
  233. }
  234. long n_int64;
  235. if (long.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int64)) {
  236. token = JsonToken.Long;
  237. token_value = n_int64;
  238. return;
  239. }
  240. ulong n_uint64;
  241. if (ulong.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_uint64))
  242. {
  243. token = JsonToken.Long;
  244. token_value = n_uint64;
  245. return;
  246. }
  247. // Shouldn't happen, but just in case, return something
  248. token = JsonToken.Int;
  249. token_value = 0;
  250. }
  251. private void ProcessSymbol ()
  252. {
  253. if (current_symbol == '[') {
  254. token = JsonToken.ArrayStart;
  255. parser_return = true;
  256. } else if (current_symbol == ']') {
  257. token = JsonToken.ArrayEnd;
  258. parser_return = true;
  259. } else if (current_symbol == '{') {
  260. token = JsonToken.ObjectStart;
  261. parser_return = true;
  262. } else if (current_symbol == '}') {
  263. token = JsonToken.ObjectEnd;
  264. parser_return = true;
  265. } else if (current_symbol == '"') {
  266. if (parser_in_string) {
  267. parser_in_string = false;
  268. parser_return = true;
  269. } else {
  270. if (token == JsonToken.None)
  271. token = JsonToken.String;
  272. parser_in_string = true;
  273. }
  274. } else if (current_symbol == (int) ParserToken.CharSeq) {
  275. token_value = lexer.StringValue;
  276. } else if (current_symbol == (int) ParserToken.False) {
  277. token = JsonToken.Boolean;
  278. token_value = false;
  279. parser_return = true;
  280. } else if (current_symbol == (int) ParserToken.Null) {
  281. token = JsonToken.Null;
  282. parser_return = true;
  283. } else if (current_symbol == (int) ParserToken.Number) {
  284. ProcessNumber (lexer.StringValue);
  285. parser_return = true;
  286. } else if (current_symbol == (int) ParserToken.Pair) {
  287. token = JsonToken.PropertyName;
  288. } else if (current_symbol == (int) ParserToken.True) {
  289. token = JsonToken.Boolean;
  290. token_value = true;
  291. parser_return = true;
  292. }
  293. }
  294. private bool ReadToken ()
  295. {
  296. if (end_of_input)
  297. return false;
  298. lexer.NextToken ();
  299. if (lexer.EndOfInput) {
  300. Close ();
  301. return false;
  302. }
  303. current_input = lexer.Token;
  304. return true;
  305. }
  306. #endregion
  307. public void Close ()
  308. {
  309. if (end_of_input)
  310. return;
  311. end_of_input = true;
  312. end_of_json = true;
  313. if (reader_is_owned)
  314. {
  315. using(reader){}
  316. }
  317. reader = null;
  318. }
  319. public bool Read ()
  320. {
  321. if (end_of_input)
  322. return false;
  323. if (end_of_json) {
  324. end_of_json = false;
  325. automaton_stack.Clear ();
  326. automaton_stack.Push ((int) ParserToken.End);
  327. automaton_stack.Push ((int) ParserToken.Text);
  328. }
  329. parser_in_string = false;
  330. parser_return = false;
  331. token = JsonToken.None;
  332. token_value = null;
  333. if (! read_started) {
  334. read_started = true;
  335. if (! ReadToken ())
  336. return false;
  337. }
  338. int[] entry_symbols;
  339. while (true) {
  340. if (parser_return) {
  341. if (automaton_stack.Peek () == (int) ParserToken.End)
  342. end_of_json = true;
  343. return true;
  344. }
  345. current_symbol = automaton_stack.Pop ();
  346. ProcessSymbol ();
  347. if (current_symbol == current_input) {
  348. if (! ReadToken ()) {
  349. if (automaton_stack.Peek () != (int) ParserToken.End)
  350. throw new JsonException (
  351. "Input doesn't evaluate to proper JSON text");
  352. if (parser_return)
  353. return true;
  354. return false;
  355. }
  356. continue;
  357. }
  358. try {
  359. entry_symbols =
  360. parse_table[current_symbol][current_input];
  361. } catch (KeyNotFoundException e) {
  362. throw new JsonException ((ParserToken) current_input, e);
  363. }
  364. if (entry_symbols[0] == (int) ParserToken.Epsilon)
  365. continue;
  366. for (int i = entry_symbols.Length - 1; i >= 0; i--)
  367. automaton_stack.Push (entry_symbols[i]);
  368. }
  369. }
  370. }
  371. }