JsonFormatWriter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 SILVERLIGHT || COMPACT_FRAMEWORK
  93. static readonly Encoding Encoding = new UTF8Encoding(false);
  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. //Tracks the writer depth and the array element count at that depth.
  154. private readonly List<int> _counter;
  155. //True if the top-level of the writer is an array as opposed to a single message.
  156. private bool _isArray;
  157. /// <summary>
  158. /// Constructs a JsonFormatWriter, use the ToString() member to extract the final Json on completion.
  159. /// </summary>
  160. protected JsonFormatWriter()
  161. {
  162. _counter = new List<int>();
  163. }
  164. /// <summary>
  165. /// Constructs a JsonFormatWriter, use ToString() to extract the final output
  166. /// </summary>
  167. public static JsonFormatWriter CreateInstance()
  168. {
  169. return new JsonTextWriter(null);
  170. }
  171. /// <summary>
  172. /// Constructs a JsonFormatWriter to output to the given text writer
  173. /// </summary>
  174. public static JsonFormatWriter CreateInstance(TextWriter output)
  175. {
  176. return new JsonTextWriter(output);
  177. }
  178. /// <summary>
  179. /// Constructs a JsonFormatWriter to output to the given stream
  180. /// </summary>
  181. public static JsonFormatWriter CreateInstance(Stream output)
  182. {
  183. return new JsonStreamWriter(output);
  184. }
  185. /// <summary> Write to the output stream </summary>
  186. protected void WriteToOutput(string format, params object[] args)
  187. {
  188. WriteToOutput(String.Format(format, args));
  189. }
  190. /// <summary> Write to the output stream </summary>
  191. protected void WriteToOutput(string text)
  192. {
  193. WriteToOutput(text.ToCharArray(), 0, text.Length);
  194. }
  195. /// <summary> Write to the output stream </summary>
  196. protected abstract void WriteToOutput(char ch);
  197. /// <summary> Write to the output stream </summary>
  198. protected abstract void WriteToOutput(char[] chars, int offset, int len);
  199. /// <summary> Sets the output formatting to use Environment.NewLine with 4-character indentions </summary>
  200. public JsonFormatWriter Formatted()
  201. {
  202. NewLine = FrameworkPortability.NewLine;
  203. Indent = " ";
  204. Whitespace = " ";
  205. return this;
  206. }
  207. /// <summary> Gets or sets the characters to use for the new-line, default = empty </summary>
  208. public string NewLine { get; set; }
  209. /// <summary> Gets or sets the text to use for indenting, default = empty </summary>
  210. public string Indent { get; set; }
  211. /// <summary> Gets or sets the whitespace to use to separate the text, default = empty </summary>
  212. public string Whitespace { get; set; }
  213. private void Seperator()
  214. {
  215. if (_counter.Count == 0)
  216. {
  217. throw new InvalidOperationException("Mismatched open/close in Json writer.");
  218. }
  219. int index = _counter.Count - 1;
  220. if (_counter[index] > 0)
  221. {
  222. WriteToOutput(',');
  223. }
  224. WriteLine(String.Empty);
  225. _counter[index] = _counter[index] + 1;
  226. }
  227. private void WriteLine(string content)
  228. {
  229. if (!String.IsNullOrEmpty(NewLine))
  230. {
  231. WriteToOutput(NewLine);
  232. for (int i = 1; i < _counter.Count; i++)
  233. {
  234. WriteToOutput(Indent);
  235. }
  236. }
  237. else if (!String.IsNullOrEmpty(Whitespace))
  238. {
  239. WriteToOutput(Whitespace);
  240. }
  241. WriteToOutput(content);
  242. }
  243. private void WriteName(string field)
  244. {
  245. Seperator();
  246. if (!String.IsNullOrEmpty(field))
  247. {
  248. WriteToOutput('"');
  249. WriteToOutput(field);
  250. WriteToOutput('"');
  251. WriteToOutput(':');
  252. if (!String.IsNullOrEmpty(Whitespace))
  253. {
  254. WriteToOutput(Whitespace);
  255. }
  256. }
  257. }
  258. private void EncodeText(string value)
  259. {
  260. char[] text = value.ToCharArray();
  261. int len = text.Length;
  262. int pos = 0;
  263. while (pos < len)
  264. {
  265. int next = pos;
  266. while (next < len && text[next] >= 32 && text[next] < 127 && text[next] != '\\' && text[next] != '/' &&
  267. text[next] != '"')
  268. {
  269. next++;
  270. }
  271. WriteToOutput(text, pos, next - pos);
  272. if (next < len)
  273. {
  274. switch (text[next])
  275. {
  276. case '"':
  277. WriteToOutput(@"\""");
  278. break;
  279. case '\\':
  280. WriteToOutput(@"\\");
  281. break;
  282. //odd at best to escape '/', most Json implementations don't, but it is defined in the rfc-4627
  283. case '/':
  284. WriteToOutput(@"\/");
  285. break;
  286. case '\b':
  287. WriteToOutput(@"\b");
  288. break;
  289. case '\f':
  290. WriteToOutput(@"\f");
  291. break;
  292. case '\n':
  293. WriteToOutput(@"\n");
  294. break;
  295. case '\r':
  296. WriteToOutput(@"\r");
  297. break;
  298. case '\t':
  299. WriteToOutput(@"\t");
  300. break;
  301. default:
  302. WriteToOutput(@"\u{0:x4}", (int) text[next]);
  303. break;
  304. }
  305. next++;
  306. }
  307. pos = next;
  308. }
  309. }
  310. /// <summary>
  311. /// Writes a String value
  312. /// </summary>
  313. protected override void WriteAsText(string field, string textValue, object typedValue)
  314. {
  315. WriteName(field);
  316. if (typedValue is bool || typedValue is int || typedValue is uint || typedValue is long ||
  317. typedValue is ulong || typedValue is double || typedValue is float)
  318. {
  319. WriteToOutput(textValue);
  320. }
  321. else
  322. {
  323. WriteToOutput('"');
  324. if (typedValue is string)
  325. {
  326. EncodeText(textValue);
  327. }
  328. else
  329. {
  330. WriteToOutput(textValue);
  331. }
  332. WriteToOutput('"');
  333. }
  334. }
  335. /// <summary>
  336. /// Writes a Double value
  337. /// </summary>
  338. protected override void Write(string field, double value)
  339. {
  340. if (double.IsNaN(value) || double.IsNegativeInfinity(value) || double.IsPositiveInfinity(value))
  341. {
  342. throw new InvalidOperationException("This format does not support NaN, Infinity, or -Infinity");
  343. }
  344. base.Write(field, value);
  345. }
  346. /// <summary>
  347. /// Writes a Single value
  348. /// </summary>
  349. protected override void Write(string field, float value)
  350. {
  351. if (float.IsNaN(value) || float.IsNegativeInfinity(value) || float.IsPositiveInfinity(value))
  352. {
  353. throw new InvalidOperationException("This format does not support NaN, Infinity, or -Infinity");
  354. }
  355. base.Write(field, value);
  356. }
  357. // Treat enum as string
  358. protected override void WriteEnum(string field, int number, string name)
  359. {
  360. Write(field, name);
  361. }
  362. /// <summary>
  363. /// Writes an array of field values
  364. /// </summary>
  365. protected override void WriteArray(FieldType type, string field, IEnumerable items)
  366. {
  367. IEnumerator enumerator = items.GetEnumerator();
  368. try
  369. {
  370. if (!enumerator.MoveNext())
  371. {
  372. return;
  373. }
  374. }
  375. finally
  376. {
  377. if (enumerator is IDisposable)
  378. {
  379. ((IDisposable) enumerator).Dispose();
  380. }
  381. }
  382. WriteName(field);
  383. WriteToOutput("[");
  384. _counter.Add(0);
  385. base.WriteArray(type, String.Empty, items);
  386. _counter.RemoveAt(_counter.Count - 1);
  387. WriteLine("]");
  388. }
  389. /// <summary>
  390. /// Writes a message
  391. /// </summary>
  392. protected override void WriteMessageOrGroup(string field, IMessageLite message)
  393. {
  394. WriteName(field);
  395. WriteMessage(message);
  396. }
  397. /// <summary>
  398. /// Writes the message to the the formatted stream.
  399. /// </summary>
  400. public override void WriteMessage(IMessageLite message)
  401. {
  402. WriteMessageStart();
  403. message.WriteTo(this);
  404. WriteMessageEnd();
  405. }
  406. /// <summary>
  407. /// Used to write the root-message preamble, in json this is the left-curly brace '{'.
  408. /// After this call you can call IMessageLite.MergeTo(...) and complete the message with
  409. /// a call to WriteMessageEnd().
  410. /// </summary>
  411. public override void WriteMessageStart()
  412. {
  413. if (_isArray)
  414. {
  415. Seperator();
  416. }
  417. WriteToOutput("{");
  418. _counter.Add(0);
  419. }
  420. /// <summary>
  421. /// Used to complete a root-message previously started with a call to WriteMessageStart()
  422. /// </summary>
  423. public override void WriteMessageEnd()
  424. {
  425. _counter.RemoveAt(_counter.Count - 1);
  426. WriteLine("}");
  427. Flush();
  428. }
  429. /// <summary>
  430. /// Used in streaming arrays of objects to the writer
  431. /// </summary>
  432. /// <example>
  433. /// <code>
  434. /// using(writer.StartArray())
  435. /// foreach(IMessageLite m in messages)
  436. /// writer.WriteMessage(m);
  437. /// </code>
  438. /// </example>
  439. public sealed class JsonArray : IDisposable
  440. {
  441. private JsonFormatWriter _writer;
  442. internal JsonArray(JsonFormatWriter writer)
  443. {
  444. _writer = writer;
  445. _writer.WriteToOutput("[");
  446. _writer._counter.Add(0);
  447. }
  448. /// <summary>
  449. /// Causes the end of the array character to be written.
  450. /// </summary>
  451. private void EndArray()
  452. {
  453. if (_writer != null)
  454. {
  455. _writer._counter.RemoveAt(_writer._counter.Count - 1);
  456. _writer.WriteLine("]");
  457. _writer.Flush();
  458. }
  459. _writer = null;
  460. }
  461. void IDisposable.Dispose()
  462. {
  463. EndArray();
  464. }
  465. }
  466. /// <summary>
  467. /// Used to write an array of messages as the output rather than a single message.
  468. /// </summary>
  469. /// <example>
  470. /// <code>
  471. /// using(writer.StartArray())
  472. /// foreach(IMessageLite m in messages)
  473. /// writer.WriteMessage(m);
  474. /// </code>
  475. /// </example>
  476. public JsonArray StartArray()
  477. {
  478. if (_isArray)
  479. {
  480. Seperator();
  481. }
  482. _isArray = true;
  483. return new JsonArray(this);
  484. }
  485. }
  486. }