JsonWriter.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Text;
  6. namespace LitJson
  7. {
  8. public class JsonWriter
  9. {
  10. public JsonWriter()
  11. {
  12. this.inst_string_builder = new StringBuilder();
  13. this.writer = new StringWriter(this.inst_string_builder);
  14. this.Init();
  15. }
  16. public JsonWriter(StringBuilder sb) : this(new StringWriter(sb))
  17. {
  18. }
  19. public JsonWriter(TextWriter writer)
  20. {
  21. if (writer == null)
  22. {
  23. throw new ArgumentNullException("writer");
  24. }
  25. this.writer = writer;
  26. this.Init();
  27. }
  28. public int IndentValue
  29. {
  30. get
  31. {
  32. return this.indent_value;
  33. }
  34. set
  35. {
  36. this.indentation = this.indentation / this.indent_value * value;
  37. this.indent_value = value;
  38. }
  39. }
  40. public bool PrettyPrint
  41. {
  42. get
  43. {
  44. return this.pretty_print;
  45. }
  46. set
  47. {
  48. this.pretty_print = value;
  49. }
  50. }
  51. public TextWriter TextWriter
  52. {
  53. get
  54. {
  55. return this.writer;
  56. }
  57. }
  58. public bool Validate
  59. {
  60. get
  61. {
  62. return this.validate;
  63. }
  64. set
  65. {
  66. this.validate = value;
  67. }
  68. }
  69. private void DoValidation(Condition cond)
  70. {
  71. if (!this.context.ExpectingValue)
  72. {
  73. this.context.Count++;
  74. }
  75. if (!this.validate)
  76. {
  77. return;
  78. }
  79. if (this.has_reached_end)
  80. {
  81. throw new JsonException("A complete JSON symbol has already been written");
  82. }
  83. switch (cond)
  84. {
  85. case Condition.InArray:
  86. if (!this.context.InArray)
  87. {
  88. throw new JsonException("Can't close an array here");
  89. }
  90. break;
  91. case Condition.InObject:
  92. if (!this.context.InObject || this.context.ExpectingValue)
  93. {
  94. throw new JsonException("Can't close an object here");
  95. }
  96. break;
  97. case Condition.NotAProperty:
  98. if (this.context.InObject && !this.context.ExpectingValue)
  99. {
  100. throw new JsonException("Expected a property");
  101. }
  102. break;
  103. case Condition.Property:
  104. if (!this.context.InObject || this.context.ExpectingValue)
  105. {
  106. throw new JsonException("Can't add a property here");
  107. }
  108. break;
  109. case Condition.Value:
  110. if (!this.context.InArray && (!this.context.InObject || !this.context.ExpectingValue))
  111. {
  112. throw new JsonException("Can't add a value here");
  113. }
  114. break;
  115. }
  116. }
  117. private void Init()
  118. {
  119. this.has_reached_end = false;
  120. this.hex_seq = new char[4];
  121. this.indentation = 0;
  122. this.indent_value = 4;
  123. this.pretty_print = false;
  124. this.validate = true;
  125. this.ctx_stack = new Stack<WriterContext>();
  126. this.context = new WriterContext();
  127. this.ctx_stack.Push(this.context);
  128. }
  129. private static void IntToHex(int n, char[] hex)
  130. {
  131. for (int i = 0; i < 4; i++)
  132. {
  133. int num = n % 16;
  134. if (num < 10)
  135. {
  136. hex[3 - i] = (char)(48 + num);
  137. }
  138. else
  139. {
  140. hex[3 - i] = (char)(65 + (num - 10));
  141. }
  142. n >>= 4;
  143. }
  144. }
  145. private void Indent()
  146. {
  147. if (this.pretty_print)
  148. {
  149. this.indentation += this.indent_value;
  150. }
  151. }
  152. private void Put(string str)
  153. {
  154. if (this.pretty_print && !this.context.ExpectingValue)
  155. {
  156. for (int i = 0; i < this.indentation; i++)
  157. {
  158. this.writer.Write(' ');
  159. }
  160. }
  161. this.writer.Write(str);
  162. }
  163. private void PutNewline()
  164. {
  165. this.PutNewline(true);
  166. }
  167. private void PutNewline(bool add_comma)
  168. {
  169. if (add_comma && !this.context.ExpectingValue && this.context.Count > 1)
  170. {
  171. this.writer.Write(',');
  172. }
  173. if (this.pretty_print && !this.context.ExpectingValue)
  174. {
  175. this.writer.Write('\n');
  176. }
  177. }
  178. private void PutString(string str)
  179. {
  180. this.Put(string.Empty);
  181. this.writer.Write('"');
  182. int length = str.Length;
  183. for (int i = 0; i < length; i++)
  184. {
  185. char c = str[i];
  186. switch (c)
  187. {
  188. case '\b':
  189. this.writer.Write("\\b");
  190. break;
  191. case '\t':
  192. this.writer.Write("\\t");
  193. break;
  194. case '\n':
  195. this.writer.Write("\\n");
  196. break;
  197. default:
  198. if (c != '"' && c != '\\')
  199. {
  200. if (str[i] >= ' ' && str[i] <= '~')
  201. {
  202. this.writer.Write(str[i]);
  203. }
  204. else
  205. {
  206. JsonWriter.IntToHex((int)str[i], this.hex_seq);
  207. this.writer.Write("\\u");
  208. this.writer.Write(this.hex_seq);
  209. }
  210. }
  211. else
  212. {
  213. this.writer.Write('\\');
  214. this.writer.Write(str[i]);
  215. }
  216. break;
  217. case '\f':
  218. this.writer.Write("\\f");
  219. break;
  220. case '\r':
  221. this.writer.Write("\\r");
  222. break;
  223. }
  224. }
  225. this.writer.Write('"');
  226. }
  227. private void Unindent()
  228. {
  229. if (this.pretty_print)
  230. {
  231. this.indentation -= this.indent_value;
  232. }
  233. }
  234. public override string ToString()
  235. {
  236. if (this.inst_string_builder == null)
  237. {
  238. return string.Empty;
  239. }
  240. return this.inst_string_builder.ToString();
  241. }
  242. public void Reset()
  243. {
  244. this.has_reached_end = false;
  245. this.ctx_stack.Clear();
  246. this.context = new WriterContext();
  247. this.ctx_stack.Push(this.context);
  248. if (this.inst_string_builder != null)
  249. {
  250. this.inst_string_builder.Remove(0, this.inst_string_builder.Length);
  251. }
  252. }
  253. public void Write(bool boolean)
  254. {
  255. this.DoValidation(Condition.Value);
  256. this.PutNewline();
  257. this.Put((!boolean) ? "false" : "true");
  258. this.context.ExpectingValue = false;
  259. }
  260. public void Write(decimal number)
  261. {
  262. this.DoValidation(Condition.Value);
  263. this.PutNewline();
  264. this.Put(Convert.ToString(number, JsonWriter.number_format));
  265. this.context.ExpectingValue = false;
  266. }
  267. public void Write(double number)
  268. {
  269. this.DoValidation(Condition.Value);
  270. this.PutNewline();
  271. string text = Convert.ToString(number, JsonWriter.number_format);
  272. this.Put(text);
  273. if (text.IndexOf('.') == -1 && text.IndexOf('E') == -1)
  274. {
  275. this.writer.Write(".0");
  276. }
  277. this.context.ExpectingValue = false;
  278. }
  279. public void Write(int number)
  280. {
  281. this.DoValidation(Condition.Value);
  282. this.PutNewline();
  283. this.Put(Convert.ToString(number, JsonWriter.number_format));
  284. this.context.ExpectingValue = false;
  285. }
  286. public void Write(long number)
  287. {
  288. this.DoValidation(Condition.Value);
  289. this.PutNewline();
  290. this.Put(Convert.ToString(number, JsonWriter.number_format));
  291. this.context.ExpectingValue = false;
  292. }
  293. public void Write(string str)
  294. {
  295. this.DoValidation(Condition.Value);
  296. this.PutNewline();
  297. if (str == null)
  298. {
  299. this.Put("null");
  300. }
  301. else
  302. {
  303. this.PutString(str);
  304. }
  305. this.context.ExpectingValue = false;
  306. }
  307. public void Write(ulong number)
  308. {
  309. this.DoValidation(Condition.Value);
  310. this.PutNewline();
  311. this.Put(Convert.ToString(number, JsonWriter.number_format));
  312. this.context.ExpectingValue = false;
  313. }
  314. public void WriteArrayEnd()
  315. {
  316. this.DoValidation(Condition.InArray);
  317. this.PutNewline(false);
  318. this.ctx_stack.Pop();
  319. if (this.ctx_stack.Count == 1)
  320. {
  321. this.has_reached_end = true;
  322. }
  323. else
  324. {
  325. this.context = this.ctx_stack.Peek();
  326. this.context.ExpectingValue = false;
  327. }
  328. this.Unindent();
  329. this.Put("]");
  330. }
  331. public void WriteArrayStart()
  332. {
  333. this.DoValidation(Condition.NotAProperty);
  334. this.PutNewline();
  335. this.Put("[");
  336. this.context = new WriterContext();
  337. this.context.InArray = true;
  338. this.ctx_stack.Push(this.context);
  339. this.Indent();
  340. }
  341. public void WriteObjectEnd()
  342. {
  343. this.DoValidation(Condition.InObject);
  344. this.PutNewline(false);
  345. this.ctx_stack.Pop();
  346. if (this.ctx_stack.Count == 1)
  347. {
  348. this.has_reached_end = true;
  349. }
  350. else
  351. {
  352. this.context = this.ctx_stack.Peek();
  353. this.context.ExpectingValue = false;
  354. }
  355. this.Unindent();
  356. this.Put("}");
  357. }
  358. public void WriteObjectStart()
  359. {
  360. this.DoValidation(Condition.NotAProperty);
  361. this.PutNewline();
  362. this.Put("{");
  363. this.context = new WriterContext();
  364. this.context.InObject = true;
  365. this.ctx_stack.Push(this.context);
  366. this.Indent();
  367. }
  368. public void WritePropertyName(string property_name)
  369. {
  370. this.DoValidation(Condition.Property);
  371. this.PutNewline();
  372. this.PutString(property_name);
  373. if (this.pretty_print)
  374. {
  375. if (property_name.Length > this.context.Padding)
  376. {
  377. this.context.Padding = property_name.Length;
  378. }
  379. for (int i = this.context.Padding - property_name.Length; i >= 0; i--)
  380. {
  381. this.writer.Write(' ');
  382. }
  383. this.writer.Write(": ");
  384. }
  385. else
  386. {
  387. this.writer.Write(':');
  388. }
  389. this.context.ExpectingValue = true;
  390. }
  391. private static NumberFormatInfo number_format = NumberFormatInfo.InvariantInfo;
  392. private WriterContext context;
  393. private Stack<WriterContext> ctx_stack;
  394. private bool has_reached_end;
  395. private char[] hex_seq;
  396. private int indentation;
  397. private int indent_value;
  398. private StringBuilder inst_string_builder;
  399. private bool pretty_print;
  400. private bool validate;
  401. private TextWriter writer;
  402. }
  403. }