JsonExport.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using FastReport.Table;
  6. using FastReport.Utils;
  7. namespace FastReport.Export.Json
  8. {
  9. /// <summary>
  10. /// Represents the JSON export filter.
  11. /// </summary>
  12. public partial class JsonExport : ExportBase
  13. {
  14. #region Fields
  15. Encoding encoding;
  16. int indent;
  17. string indention = " ";
  18. int pageNo = 0;
  19. bool isCommaNeed = false;
  20. #endregion
  21. #region Private Methods
  22. void write(Stream target, string data)
  23. {
  24. if (string.IsNullOrEmpty(data))
  25. return;
  26. if (data.StartsWith("{") && isCommaNeed)
  27. {
  28. var trailedBytes = encoding.GetBytes("," + Environment.NewLine);
  29. long newPosition = target.Length - encoding.GetBytes(Environment.NewLine).Length;
  30. if (newPosition >= 0)
  31. {
  32. target.Position = newPosition;
  33. target.Write(trailedBytes, 0, trailedBytes.Length);
  34. }
  35. }
  36. isCommaNeed = false;
  37. if (data.EndsWith("}"))
  38. isCommaNeed = true;
  39. data = appendIndention(data);
  40. byte[] bytes = encoding.GetBytes(data + Environment.NewLine);
  41. target.Write(bytes, 0, bytes.Length);
  42. }
  43. string appendIndention(string str)
  44. {
  45. for (int i = 0; i < indent; i++)
  46. str = indention + str;
  47. return str;
  48. }
  49. string comma(int i, int length)
  50. {
  51. return i < length - 1 ? "," : "";
  52. }
  53. string wrap(string s)
  54. {
  55. if (string.IsNullOrEmpty(s))
  56. return "\"\"";
  57. char c = '\0';
  58. int i;
  59. int len = s.Length;
  60. StringBuilder sb = new StringBuilder(len + 4);
  61. string t;
  62. for (i = 0; i < len; i += 1)
  63. {
  64. c = s[i];
  65. switch (c)
  66. {
  67. case '\\':
  68. case '"':
  69. sb.Append('\\');
  70. sb.Append(c);
  71. break;
  72. case '/':
  73. sb.Append('\\');
  74. sb.Append(c);
  75. break;
  76. case '\b':
  77. sb.Append("\\b");
  78. break;
  79. case '\t':
  80. sb.Append("\\t");
  81. break;
  82. case '\n':
  83. sb.Append("\\n");
  84. break;
  85. case '\f':
  86. sb.Append("\\f");
  87. break;
  88. case '\r':
  89. sb.Append("\\r");
  90. break;
  91. default:
  92. if (c < ' ')
  93. {
  94. t = "000" + String.Format("X", c);
  95. sb.Append("\\u" + t.Substring(t.Length - 4));
  96. }
  97. else
  98. {
  99. sb.Append(c);
  100. }
  101. break;
  102. }
  103. }
  104. return "\"" + sb.ToString() + "\"";
  105. }
  106. void exportObj(Base c, List<string> items)
  107. {
  108. if (c is ReportComponentBase && (c as ReportComponentBase).Exportable)
  109. {
  110. ReportComponentBase obj = c as ReportComponentBase;
  111. //if (FDataOnly && (obj.Parent == null || !(obj.Parent is DataBand)))
  112. //continue;
  113. //if (obj is TableCell)
  114. //continue;
  115. if (obj is TextObject)
  116. {
  117. TextObject text = obj as TextObject;
  118. if (!string.IsNullOrEmpty(text.Text))
  119. items.Add("{\"TextObject\":" + wrap(text.Text) + "}");
  120. }
  121. else if (obj is PictureObject)
  122. {
  123. PictureObject pic = obj as PictureObject;
  124. if (!string.IsNullOrEmpty(pic.ImageLocation))
  125. items.Add("{\"PictureObject\":" + wrap(pic.ImageLocation) + "}");
  126. }
  127. else if (obj is RichObject)
  128. {
  129. RichObject rich = obj as RichObject;
  130. if (!string.IsNullOrEmpty(rich.Text))
  131. items.Add("{\"RichObject\":" + wrap(rich.Text) + "}");
  132. }
  133. else if (obj is HtmlObject)
  134. {
  135. HtmlObject html = obj as HtmlObject;
  136. if (!string.IsNullOrEmpty(html.Text))
  137. items.Add("{\"HtmlObject\":" + wrap(html.Text) + "}");
  138. }
  139. else if (obj is TableBase)
  140. {
  141. StringBuilder tableData = new StringBuilder();
  142. tableData.Append("{\"TableObject\":[");
  143. indent++;
  144. TableBase table = obj as TableBase;
  145. for (int i = 0; i < table.RowCount; i++)
  146. {
  147. string row = "[";
  148. for (int j = 0; j < table.ColumnCount; j++)
  149. {
  150. TableCell cell = table[j, i];
  151. row += wrap(cell.Text) + comma(j, table.ColumnCount);
  152. }
  153. row += "]" + comma(i, table.RowCount);
  154. tableData.Append(Environment.NewLine + appendIndention(row));
  155. }
  156. indent--;
  157. tableData.Append(Environment.NewLine + appendIndention("]}"));
  158. items.Add(tableData.ToString());
  159. }
  160. }
  161. }
  162. #endregion
  163. #region Protected Methods
  164. /// <inheritdoc/>
  165. protected override void Start()
  166. {
  167. base.Start();
  168. pageNo = 0;
  169. write(Stream, "{");
  170. indent++;
  171. write(Stream, "\"report\": [");
  172. indent++;
  173. }
  174. /// <inheritdoc/>
  175. protected override void Finish()
  176. {
  177. indent--;
  178. write(Stream, "]");
  179. indent--;
  180. write(Stream, "}");
  181. }
  182. /// <inheritdoc/>
  183. protected override string GetFileFilter()
  184. {
  185. return new MyRes("FileFilters").Get("JsonFile");
  186. }
  187. /// <inheritdoc/>
  188. protected override void ExportPageBegin(ReportPage page)
  189. {
  190. base.ExportPageBegin(page);
  191. write(Stream, "{");
  192. indent++;
  193. write(Stream, "\"page\":\"" + (pageNo + 1) + "\",");
  194. write(Stream, "\"items\":[");
  195. indent++;
  196. }
  197. /// <inheritdoc/>
  198. protected override void ExportPageEnd(ReportPage page)
  199. {
  200. base.ExportPageEnd(page);
  201. indent--;
  202. write(Stream, "]");
  203. indent--;
  204. write(Stream, "}" + comma(pageNo, Pages.Length));
  205. pageNo++;
  206. }
  207. /// <inheritdoc/>
  208. protected override void ExportBand(BandBase band)
  209. {
  210. base.ExportBand(band);
  211. List<string> items = new List<string>();
  212. exportObj(band, items);
  213. foreach (Base c in band.ForEachAllConvectedObjects(this))
  214. exportObj(c, items);
  215. for (int i = 0; i < items.Count; i++)
  216. {
  217. string item = items[i];
  218. if (string.IsNullOrEmpty(item))
  219. continue;
  220. write(Stream, item + comma(i, items.Count));
  221. }
  222. }
  223. #endregion
  224. /// <summary>
  225. /// Initializes a new instance of the <see cref="JsonExport"/> class.
  226. /// </summary>
  227. public JsonExport()
  228. {
  229. encoding = Encoding.Unicode;
  230. }
  231. }
  232. }