JsonFormatWriter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using Google.ProtocolBuffers.Descriptors;
  7. namespace Google.ProtocolBuffers.Serialization
  8. {
  9. /// <summary>
  10. /// JsonFormatWriter is a .NET 2.0 friendly json formatter for proto buffer messages. For .NET 3.5
  11. /// you may also use the XmlFormatWriter with an XmlWriter created by the
  12. /// <see cref="System.Runtime.Serialization.Json.JsonReaderWriterFactory">JsonReaderWriterFactory</see>.
  13. /// </summary>
  14. public abstract class JsonFormatWriter : AbstractTextWriter
  15. {
  16. #region buffering implementations
  17. private class JsonTextWriter : JsonFormatWriter
  18. {
  19. private readonly char[] _buffer;
  20. private TextWriter _output;
  21. private int _bufferPos;
  22. public JsonTextWriter(TextWriter output)
  23. {
  24. _buffer = new char[4096];
  25. _bufferPos = 0;
  26. _output = output;
  27. _counter.Add(0);
  28. }
  29. /// <summary>
  30. /// Returns the output of TextWriter.ToString() where TextWriter is the ctor argument.
  31. /// </summary>
  32. public override string ToString()
  33. {
  34. Flush();
  35. if (_output != null)
  36. {
  37. return _output.ToString();
  38. }
  39. return new String(_buffer, 0, _bufferPos);
  40. }
  41. protected override void WriteToOutput(char[] chars, int offset, int len)
  42. {
  43. if (_bufferPos + len >= _buffer.Length)
  44. {
  45. if (_output == null)
  46. {
  47. _output = new StringWriter(new StringBuilder(_buffer.Length*2 + len));
  48. }
  49. Flush();
  50. }
  51. if (len < _buffer.Length)
  52. {
  53. if (len <= 12)
  54. {
  55. int stop = offset + len;
  56. for (int i = offset; i < stop; i++)
  57. {
  58. _buffer[_bufferPos++] = chars[i];
  59. }
  60. }
  61. else
  62. {
  63. Buffer.BlockCopy(chars, offset << 1, _buffer, _bufferPos << 1, len << 1);
  64. _bufferPos += len;
  65. }
  66. }
  67. else
  68. {
  69. _output.Write(chars, offset, len);
  70. }
  71. }
  72. protected override void WriteToOutput(char ch)
  73. {
  74. if (_bufferPos >= _buffer.Length)
  75. {
  76. Flush();
  77. }
  78. _buffer[_bufferPos++] = ch;
  79. }
  80. public override void Flush()
  81. {
  82. if (_bufferPos > 0 && _output != null)
  83. {
  84. _output.Write(_buffer, 0, _bufferPos);
  85. _bufferPos = 0;
  86. }
  87. base.Flush();
  88. }
  89. }
  90. private class JsonStreamWriter : JsonFormatWriter
  91. {
  92. #if SILVERLIGHT2 || COMPACT_FRAMEWORK_35
  93. static readonly Encoding Encoding = Encoding.UTF8;
  94. #else
  95. private static readonly Encoding Encoding = Encoding.ASCII;
  96. #endif
  97. private readonly byte[] _buffer;
  98. private Stream _output;
  99. private int _bufferPos;
  100. public JsonStreamWriter(Stream output)
  101. {
  102. _buffer = new byte[8192];
  103. _bufferPos = 0;
  104. _output = output;
  105. _counter.Add(0);
  106. }
  107. protected override void WriteToOutput(char[] chars, int offset, int len)
  108. {
  109. if (_bufferPos + len >= _buffer.Length)
  110. {
  111. Flush();
  112. }
  113. if (len < _buffer.Length)
  114. {
  115. if (len <= 12)
  116. {
  117. int stop = offset + len;
  118. for (int i = offset; i < stop; i++)
  119. {
  120. _buffer[_bufferPos++] = (byte) chars[i];
  121. }
  122. }
  123. else
  124. {
  125. _bufferPos += Encoding.GetBytes(chars, offset, len, _buffer, _bufferPos);
  126. }
  127. }
  128. else
  129. {
  130. byte[] temp = Encoding.GetBytes(chars, offset, len);
  131. _output.Write(temp, 0, temp.Length);
  132. }
  133. }
  134. protected override void WriteToOutput(char ch)
  135. {
  136. if (_bufferPos >= _buffer.Length)
  137. {
  138. Flush();
  139. }
  140. _buffer[_bufferPos++] = (byte) ch;
  141. }
  142. public override void Flush()
  143. {
  144. if (_bufferPos > 0 && _output != null)
  145. {
  146. _output.Write(_buffer, 0, _bufferPos);
  147. _bufferPos = 0;
  148. }
  149. base.Flush();
  150. }
  151. }
  152. #endregion
  153. private readonly List<int> _counter;
  154. private bool _isArray;
  155. /// <summary>
  156. /// Constructs a JsonFormatWriter, use the ToString() member to extract the final Json on completion.
  157. /// </summary>
  158. protected JsonFormatWriter()
  159. {
  160. _counter = new List<int>();
  161. }
  162. /// <summary>
  163. /// Constructs a JsonFormatWriter, use ToString() to extract the final output
  164. /// </summary>
  165. public static JsonFormatWriter CreateInstance()
  166. {
  167. return new JsonTextWriter(null);
  168. }
  169. /// <summary>
  170. /// Constructs a JsonFormatWriter to output to the given text writer
  171. /// </summary>
  172. public static JsonFormatWriter CreateInstance(TextWriter output)
  173. {
  174. return new JsonTextWriter(output);
  175. }
  176. /// <summary>
  177. /// Constructs a JsonFormatWriter to output to the given stream
  178. /// </summary>
  179. public static JsonFormatWriter CreateInstance(Stream output)
  180. {
  181. return new JsonStreamWriter(output);
  182. }
  183. /// <summary> Write to the output stream </summary>
  184. protected void WriteToOutput(string format, params object[] args)
  185. {
  186. WriteToOutput(String.Format(format, args));
  187. }
  188. /// <summary> Write to the output stream </summary>
  189. protected void WriteToOutput(string text)
  190. {
  191. WriteToOutput(text.ToCharArray(), 0, text.Length);
  192. }
  193. /// <summary> Write to the output stream </summary>
  194. protected abstract void WriteToOutput(char ch);
  195. /// <summary> Write to the output stream </summary>
  196. protected abstract void WriteToOutput(char[] chars, int offset, int len);
  197. /// <summary> Sets the output formatting to use Environment.NewLine with 4-character indentions </summary>
  198. public JsonFormatWriter Formatted()
  199. {
  200. NewLine = Environment.NewLine;
  201. Indent = " ";
  202. Whitespace = " ";
  203. return this;
  204. }
  205. /// <summary> Gets or sets the characters to use for the new-line, default = empty </summary>
  206. public string NewLine { get; set; }
  207. /// <summary> Gets or sets the text to use for indenting, default = empty </summary>
  208. public string Indent { get; set; }
  209. /// <summary> Gets or sets the whitespace to use to separate the text, default = empty </summary>
  210. public string Whitespace { get; set; }
  211. /// <summary>
  212. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  213. /// </summary>
  214. protected override void Dispose(bool disposing)
  215. {
  216. if (disposing && _counter.Count == 1)
  217. {
  218. EndMessage();
  219. }
  220. base.Dispose(disposing);
  221. }
  222. private void Seperator()
  223. {
  224. if (_counter.Count == 0)
  225. {
  226. throw new InvalidOperationException("Missmatched open/close in Json writer.");
  227. }
  228. int index = _counter.Count - 1;
  229. if (_counter[index] > 0)
  230. {
  231. WriteToOutput(',');
  232. }
  233. WriteLine(String.Empty);
  234. _counter[index] = _counter[index] + 1;
  235. }
  236. private void WriteLine(string content)
  237. {
  238. if (!String.IsNullOrEmpty(NewLine))
  239. {
  240. WriteToOutput(NewLine);
  241. for (int i = 1; i < _counter.Count; i++)
  242. {
  243. WriteToOutput(Indent);
  244. }
  245. }
  246. else if (!String.IsNullOrEmpty(Whitespace))
  247. {
  248. WriteToOutput(Whitespace);
  249. }
  250. WriteToOutput(content);
  251. }
  252. private void WriteName(string field)
  253. {
  254. Seperator();
  255. if (!String.IsNullOrEmpty(field))
  256. {
  257. WriteToOutput('"');
  258. WriteToOutput(field);
  259. WriteToOutput('"');
  260. WriteToOutput(':');
  261. if (!String.IsNullOrEmpty(Whitespace))
  262. {
  263. WriteToOutput(Whitespace);
  264. }
  265. }
  266. }
  267. private void EncodeText(string value)
  268. {
  269. char[] text = value.ToCharArray();
  270. int len = text.Length;
  271. int pos = 0;
  272. while (pos < len)
  273. {
  274. int next = pos;
  275. while (next < len && text[next] >= 32 && text[next] < 127 && text[next] != '\\' && text[next] != '/' &&
  276. text[next] != '"')
  277. {
  278. next++;
  279. }
  280. WriteToOutput(text, pos, next - pos);
  281. if (next < len)
  282. {
  283. switch (text[next])
  284. {
  285. case '"':
  286. WriteToOutput(@"\""");
  287. break;
  288. case '\\':
  289. WriteToOutput(@"\\");
  290. break;
  291. //odd at best to escape '/', most Json implementations don't, but it is defined in the rfc-4627
  292. case '/':
  293. WriteToOutput(@"\/");
  294. break;
  295. case '\b':
  296. WriteToOutput(@"\b");
  297. break;
  298. case '\f':
  299. WriteToOutput(@"\f");
  300. break;
  301. case '\n':
  302. WriteToOutput(@"\n");
  303. break;
  304. case '\r':
  305. WriteToOutput(@"\r");
  306. break;
  307. case '\t':
  308. WriteToOutput(@"\t");
  309. break;
  310. default:
  311. WriteToOutput(@"\u{0:x4}", (int) text[next]);
  312. break;
  313. }
  314. next++;
  315. }
  316. pos = next;
  317. }
  318. }
  319. /// <summary>
  320. /// Writes a String value
  321. /// </summary>
  322. protected override void WriteAsText(string field, string textValue, object typedValue)
  323. {
  324. WriteName(field);
  325. if (typedValue is bool || typedValue is int || typedValue is uint || typedValue is long ||
  326. typedValue is ulong || typedValue is double || typedValue is float)
  327. {
  328. WriteToOutput(textValue);
  329. }
  330. else
  331. {
  332. WriteToOutput('"');
  333. if (typedValue is string)
  334. {
  335. EncodeText(textValue);
  336. }
  337. else
  338. {
  339. WriteToOutput(textValue);
  340. }
  341. WriteToOutput('"');
  342. }
  343. }
  344. /// <summary>
  345. /// Writes a Double value
  346. /// </summary>
  347. protected override void Write(string field, double value)
  348. {
  349. if (double.IsNaN(value) || double.IsNegativeInfinity(value) || double.IsPositiveInfinity(value))
  350. {
  351. throw new InvalidOperationException("This format does not support NaN, Infinity, or -Infinity");
  352. }
  353. base.Write(field, value);
  354. }
  355. /// <summary>
  356. /// Writes a Single value
  357. /// </summary>
  358. protected override void Write(string field, float value)
  359. {
  360. if (float.IsNaN(value) || float.IsNegativeInfinity(value) || float.IsPositiveInfinity(value))
  361. {
  362. throw new InvalidOperationException("This format does not support NaN, Infinity, or -Infinity");
  363. }
  364. base.Write(field, value);
  365. }
  366. // Treat enum as string
  367. protected override void WriteEnum(string field, int number, string name)
  368. {
  369. Write(field, name);
  370. }
  371. /// <summary>
  372. /// Writes an array of field values
  373. /// </summary>
  374. protected override void WriteArray(FieldType type, string field, IEnumerable items)
  375. {
  376. IEnumerator enumerator = items.GetEnumerator();
  377. try
  378. {
  379. if (!enumerator.MoveNext())
  380. {
  381. return;
  382. }
  383. }
  384. finally
  385. {
  386. if (enumerator is IDisposable)
  387. {
  388. ((IDisposable) enumerator).Dispose();
  389. }
  390. }
  391. WriteName(field);
  392. WriteToOutput("[");
  393. _counter.Add(0);
  394. base.WriteArray(type, String.Empty, items);
  395. _counter.RemoveAt(_counter.Count - 1);
  396. WriteLine("]");
  397. }
  398. /// <summary>
  399. /// Writes a message
  400. /// </summary>
  401. protected override void WriteMessageOrGroup(string field, IMessageLite message)
  402. {
  403. WriteName(field);
  404. WriteMessage(message);
  405. }
  406. /// <summary>
  407. /// Writes the message to the the formatted stream.
  408. /// </summary>
  409. public override void WriteMessage(IMessageLite message)
  410. {
  411. StartMessage();
  412. message.WriteTo(this);
  413. EndMessage();
  414. }
  415. /// <summary>
  416. /// Used to write the root-message preamble, in json this is the left-curly brace '{'.
  417. /// After this call you can call IMessageLite.MergeTo(...) and complete the message with
  418. /// a call to EndMessage().
  419. /// </summary>
  420. public override void StartMessage()
  421. {
  422. if (_isArray)
  423. {
  424. Seperator();
  425. }
  426. WriteToOutput("{");
  427. _counter.Add(0);
  428. }
  429. /// <summary>
  430. /// Used to complete a root-message previously started with a call to StartMessage()
  431. /// </summary>
  432. public override void EndMessage()
  433. {
  434. _counter.RemoveAt(_counter.Count - 1);
  435. WriteLine("}");
  436. Flush();
  437. }
  438. /// <summary>
  439. /// Used in streaming arrays of objects to the writer
  440. /// </summary>
  441. /// <example>
  442. /// <code>
  443. /// using(writer.StartArray())
  444. /// foreach(IMessageLite m in messages)
  445. /// writer.WriteMessage(m);
  446. /// </code>
  447. /// </example>
  448. public sealed class JsonArray : IDisposable
  449. {
  450. private JsonFormatWriter _writer;
  451. internal JsonArray(JsonFormatWriter writer)
  452. {
  453. _writer = writer;
  454. _writer.WriteToOutput("[");
  455. _writer._counter.Add(0);
  456. }
  457. /// <summary>
  458. /// Causes the end of the array character to be written.
  459. /// </summary>
  460. private void EndArray()
  461. {
  462. if (_writer != null)
  463. {
  464. _writer._counter.RemoveAt(_writer._counter.Count - 1);
  465. _writer.WriteLine("]");
  466. _writer.Flush();
  467. }
  468. _writer = null;
  469. }
  470. void IDisposable.Dispose()
  471. {
  472. EndArray();
  473. }
  474. }
  475. /// <summary>
  476. /// Used to write an array of messages as the output rather than a single message.
  477. /// </summary>
  478. /// <example>
  479. /// <code>
  480. /// using(writer.StartArray())
  481. /// foreach(IMessageLite m in messages)
  482. /// writer.WriteMessage(m);
  483. /// </code>
  484. /// </example>
  485. public JsonArray StartArray()
  486. {
  487. if (_isArray)
  488. {
  489. Seperator();
  490. }
  491. _isArray = true;
  492. return new JsonArray(this);
  493. }
  494. }
  495. }