Welcome to mirror list, hosted at ThFree Co, Russian Federation.

md_doc_stream.html - github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e43cbd7492ac53337287394384f7eb6f28413fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!-- HTML header for doxygen 1.8.7-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.7"/>
<title>RapidJSON: Stream</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygenextra.css" rel="stylesheet" type="text/css"/>
</head>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-63929386-1', 'auto');
  ga('send', 'pageview');
</script>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="topbanner"><a href="https://github.com/miloyip/rapidjson" title="RapidJSON GitHub"><i class="githublogo"></i></a></div>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.7 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('md_doc_stream.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(11)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(12)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Stream </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#MemoryStreams">Memory Streams</a><ul><li class="level2"><a href="#StringStream">StringStream (Input)</a></li>
<li class="level2"><a href="#StringBuffer">StringBuffer (Output)</a></li>
</ul>
</li>
<li class="level1"><a href="#FileStreams">File Streams</a><ul><li class="level2"><a href="#FileReadStream">FileReadStream (Input)</a></li>
<li class="level2"><a href="#FileWriteStream">FileWriteStream (Output)</a></li>
</ul>
</li>
<li class="level1"><a href="#EncodedStreams">Encoded Streams</a><ul><li class="level2"><a href="#EncodedInputStream">EncodedInputStream</a></li>
<li class="level2"><a href="#EncodedOutputStream">EncodedOutputStream</a></li>
<li class="level2"><a href="#AutoUTFInputStream">AutoUTFInputStream</a></li>
<li class="level2"><a href="#AutoUTFOutputStream">AutoUTFOutputStream</a></li>
</ul>
</li>
<li class="level1"><a href="#CustomStream">Custom Stream</a><ul><li class="level2"><a href="#ExampleIStreamWrapper">Example: istream wrapper</a></li>
<li class="level2"><a href="#ExampleOStreamWrapper">Example: ostream wrapper</a></li>
</ul>
</li>
<li class="level1"><a href="#Summary">Summary</a></li>
</ul>
</div>
<div class="textblock"><p>In RapidJSON, <code><a class="el" href="classrapidjson_1_1_stream.html" title="Concept for reading and writing characters. ">rapidjson::Stream</a></code> is a concept for reading/writing JSON. Here we first show how to use streams provided. And then see how to create a custom stream.</p>
<h1><a class="anchor" id="MemoryStreams"></a>
Memory Streams</h1>
<p>Memory streams store JSON in memory.</p>
<h2><a class="anchor" id="StringStream"></a>
StringStream (Input)</h2>
<p><code>StringStream</code> is the most basic input stream. It represents a complete, read-only JSON stored in memory. It is defined in <code><a class="el" href="rapidjson_8h.html" title="common definitions and configuration ">rapidjson/rapidjson.h</a></code>.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="document_8h.html">rapidjson/document.h</a>&quot;</span> <span class="comment">// will include &quot;rapidjson/rapidjson.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>rapidjson;</div>
<div class="line"></div>
<div class="line"><span class="comment">// ...</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> json[] = <span class="stringliteral">&quot;[1, 2, 3, 4]&quot;</span>;</div>
<div class="line"><a class="code" href="structrapidjson_1_1_generic_string_stream.html">StringStream</a> s(json);</div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_generic_document.html">Document</a> d;</div>
<div class="line">d.<a class="code" href="classrapidjson_1_1_generic_document.html#a3ae97682cf04685c7db9d89ebc399b85">ParseStream</a>(s);</div>
</div><!-- fragment --><p>Since this is very common usage, <code>Document::Parse(const char*)</code> is provided to do exactly the same as above:</p>
<div class="fragment"><div class="line"><span class="comment">// ...</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> json[] = <span class="stringliteral">&quot;[1, 2, 3, 4]&quot;</span>;</div>
<div class="line"><a class="code" href="namespacerapidjson.html#a660c934c2959121babf799b6cb206659">Document</a> d;</div>
<div class="line">d.Parse(json);</div>
</div><!-- fragment --><p>Note that, <code>StringStream</code> is a typedef of <code>GenericStringStream&lt;UTF8&lt;&gt; &gt;</code>, user may use another encodings to represent the character set of the stream.</p>
<h2><a class="anchor" id="StringBuffer"></a>
StringBuffer (Output)</h2>
<p><code>StringBuffer</code> is a simple output stream. It allocates a memory buffer for writing the whole JSON. Use <code>GetString()</code> to obtain the buffer.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;rapidjson/stringbuffer.h&quot;</span></div>
<div class="line"></div>
<div class="line"><a class="code" href="namespacerapidjson.html#a51a6c35028b76e354bbb9e25d7125641">StringBuffer</a> buffer;</div>
<div class="line">Writer&lt;StringBuffer&gt; writer(buffer);</div>
<div class="line">d.Accept(writer);</div>
<div class="line"></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* output = buffer.GetString();</div>
</div><!-- fragment --><p>When the buffer is full, it will increases the capacity automatically. The default capacity is 256 characters (256 bytes for UTF8, 512 bytes for UTF16, etc.). User can provide an allocator and a initial capacity.</p>
<div class="fragment"><div class="line"><a class="code" href="namespacerapidjson.html#a51a6c35028b76e354bbb9e25d7125641">StringBuffer</a> buffer1(0, 1024); <span class="comment">// Use its allocator, initial size = 1024</span></div>
<div class="line"><a class="code" href="namespacerapidjson.html#a51a6c35028b76e354bbb9e25d7125641">StringBuffer</a> buffer2(allocator, 1024);</div>
</div><!-- fragment --><p>By default, <code>StringBuffer</code> will instantiate an internal allocator.</p>
<p>Similarly, <code>StringBuffer</code> is a typedef of <code>GenericStringBuffer&lt;UTF8&lt;&gt; &gt;</code>.</p>
<h1><a class="anchor" id="FileStreams"></a>
File Streams</h1>
<p>When parsing a JSON from file, you may read the whole JSON into memory and use <code>StringStream</code> above.</p>
<p>However, if the JSON is big, or memory is limited, you can use <code>FileReadStream</code>. It only read a part of JSON from file into buffer, and then let the part be parsed. If it runs out of characters in the buffer, it will read the next part from file.</p>
<h2><a class="anchor" id="FileReadStream"></a>
FileReadStream (Input)</h2>
<p><code>FileReadStream</code> reads the file via a <code>FILE</code> pointer. And user need to provide a buffer.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;rapidjson/filereadstream.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>rapidjson;</div>
<div class="line"></div>
<div class="line">FILE* fp = fopen(<span class="stringliteral">&quot;big.json&quot;</span>, <span class="stringliteral">&quot;rb&quot;</span>); <span class="comment">// non-Windows use &quot;r&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">char</span> readBuffer[65536];</div>
<div class="line"><a class="code" href="classrapidjson_1_1_file_read_stream.html">FileReadStream</a> is(fp, readBuffer, <span class="keyword">sizeof</span>(readBuffer));</div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_generic_document.html">Document</a> d;</div>
<div class="line">d.<a class="code" href="classrapidjson_1_1_generic_document.html#a3ae97682cf04685c7db9d89ebc399b85">ParseStream</a>(is);</div>
<div class="line"></div>
<div class="line">fclose(fp);</div>
</div><!-- fragment --><p>Different from string streams, <code>FileReadStream</code> is byte stream. It does not handle encodings. If the file is not UTF-8, the byte stream can be wrapped in a <code>EncodedInputStream</code>. It will be discussed very soon.</p>
<p>Apart from reading file, user can also use <code>FileReadStream</code> to read <code>stdin</code>.</p>
<h2><a class="anchor" id="FileWriteStream"></a>
FileWriteStream (Output)</h2>
<p><code>FileWriteStream</code> is buffered output stream. Its usage is very similar to <code>FileReadStream</code>.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;rapidjson/filewritestream.h&quot;</span></div>
<div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>rapidjson;</div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_generic_document.html">Document</a> d;</div>
<div class="line">d.<a class="code" href="classrapidjson_1_1_generic_document.html#aea842b533a858c9a3861451ad9e8642c">Parse</a>(json);</div>
<div class="line"><span class="comment">// ...</span></div>
<div class="line"></div>
<div class="line">FILE* fp = fopen(<span class="stringliteral">&quot;output.json&quot;</span>, <span class="stringliteral">&quot;wb&quot;</span>); <span class="comment">// non-Windows use &quot;w&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">char</span> writeBuffer[65536];</div>
<div class="line"><a class="code" href="classrapidjson_1_1_file_write_stream.html">FileWriteStream</a> os(fp, writeBuffer, <span class="keyword">sizeof</span>(writeBuffer));</div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_writer.html">Writer&lt;FileWriteStream&gt;</a> writer(os);</div>
<div class="line">d.Accept(writer);</div>
<div class="line"></div>
<div class="line">fclose(fp);</div>
</div><!-- fragment --><p>It can also directs the output to <code>stdout</code>.</p>
<h1><a class="anchor" id="EncodedStreams"></a>
Encoded Streams</h1>
<p>Encoded streams do not contain JSON itself, but they wrap byte streams to provide basic encoding/decoding function.</p>
<p>As mentioned above, UTF-8 byte streams can be read directly. However, UTF-16 and UTF-32 have endian issue. To handle endian correctly, it needs to convert bytes into characters (e.g. <code>wchar_t</code> for UTF-16) while reading, and characters into bytes while writing.</p>
<p>Besides, it also need to handle <a href="http://en.wikipedia.org/wiki/Byte_order_mark">byte order mark (BOM)</a>. When reading from a byte stream, it is needed to detect or just consume the BOM if exists. When writing to a byte stream, it can optionally write BOM.</p>
<p>If the encoding of stream is known in compile-time, you may use <code>EncodedInputStream</code> and <code>EncodedOutputStream</code>. If the stream can be UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE JSON, and it is only known in runtime, you may use <code>AutoUTFInputStream</code> and <code>AutoUTFOutputStream</code>. These streams are defined in <code><a class="el" href="encodedstream_8h_source.html">rapidjson/encodedstream.h</a></code>.</p>
<p>Note that, these encoded streams can be applied to streams other than file. For example, you may have a file in memory, or a custom byte stream, be wrapped in encoded streams.</p>
<h2><a class="anchor" id="EncodedInputStream"></a>
EncodedInputStream</h2>
<p><code>EncodedInputStream</code> has two template parameters. The first one is a <code>Encoding</code> class, such as <code>UTF8</code>, <code>UTF16LE</code>, defined in <code><a class="el" href="encodings_8h_source.html">rapidjson/encodings.h</a></code>. The second one is the class of stream to be wrapped.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="document_8h.html">rapidjson/document.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;rapidjson/filereadstream.h&quot;</span>   <span class="comment">// FileReadStream</span></div>
<div class="line"><span class="preprocessor">#include &quot;rapidjson/encodedstream.h&quot;</span>    <span class="comment">// EncodedInputStream</span></div>
<div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>rapidjson;</div>
<div class="line"></div>
<div class="line">FILE* fp = fopen(<span class="stringliteral">&quot;utf16le.json&quot;</span>, <span class="stringliteral">&quot;rb&quot;</span>); <span class="comment">// non-Windows use &quot;r&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">char</span> readBuffer[256];</div>
<div class="line"><a class="code" href="classrapidjson_1_1_file_read_stream.html">FileReadStream</a> bis(fp, readBuffer, <span class="keyword">sizeof</span>(readBuffer));</div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_encoded_input_stream.html">EncodedInputStream&lt;UTF16LE&lt;&gt;</a>, <a class="code" href="classrapidjson_1_1_file_read_stream.html">FileReadStream</a>&gt; eis(bis);  <span class="comment">// wraps bis into eis</span></div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_generic_document.html">Document</a> d; <span class="comment">// Document is GenericDocument&lt;UTF8&lt;&gt; &gt; </span></div>
<div class="line">d.<a class="code" href="classrapidjson_1_1_generic_document.html#a3ae97682cf04685c7db9d89ebc399b85">ParseStream</a>&lt;0, <a class="code" href="structrapidjson_1_1_u_t_f16_l_e.html">UTF16LE&lt;&gt;</a> &gt;(eis);  <span class="comment">// Parses UTF-16LE file into UTF-8 in memory</span></div>
<div class="line"></div>
<div class="line">fclose(fp);</div>
</div><!-- fragment --><h2><a class="anchor" id="EncodedOutputStream"></a>
EncodedOutputStream</h2>
<p><code>EncodedOutputStream</code> is similar but it has a <code>bool putBOM</code> parameter in the constructor, controlling whether to write BOM into output byte stream.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;rapidjson/filewritestream.h&quot;</span>  <span class="comment">// FileWriteStream</span></div>
<div class="line"><span class="preprocessor">#include &quot;rapidjson/encodedstream.h&quot;</span>    <span class="comment">// EncodedOutputStream</span></div>
<div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div>
<div class="line"></div>
<div class="line"><a class="code" href="namespacerapidjson.html#a660c934c2959121babf799b6cb206659">Document</a> d;         <span class="comment">// Document is GenericDocument&lt;UTF8&lt;&gt; &gt; </span></div>
<div class="line"><span class="comment">// ...</span></div>
<div class="line"></div>
<div class="line">FILE* fp = fopen(<span class="stringliteral">&quot;output_utf32le.json&quot;</span>, <span class="stringliteral">&quot;wb&quot;</span>); <span class="comment">// non-Windows use &quot;w&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">char</span> writeBuffer[256];</div>
<div class="line">FileWriteStream bos(fp, writeBuffer, <span class="keyword">sizeof</span>(writeBuffer));</div>
<div class="line"></div>
<div class="line"><span class="keyword">typedef</span> EncodedOutputStream&lt;UTF32LE&lt;&gt;, FileWriteStream&gt; OutputStream;</div>
<div class="line">OutputStream eos(bos, <span class="keyword">true</span>);   <span class="comment">// Write BOM</span></div>
<div class="line"></div>
<div class="line">Writer&lt;OutputStream, UTF32LE&lt;&gt;, UTF8&lt;&gt;&gt; writer(eos);</div>
<div class="line">d.Accept(writer);   <span class="comment">// This generates UTF32-LE file from UTF-8 in memory</span></div>
<div class="line"></div>
<div class="line">fclose(fp);</div>
</div><!-- fragment --><h2><a class="anchor" id="AutoUTFInputStream"></a>
AutoUTFInputStream</h2>
<p>Sometimes an application may want to handle all supported JSON encoding. <code>AutoUTFInputStream</code> will detection encoding by BOM first. If BOM is unavailable, it will use characteristics of valid JSON to make detection. If neither method success, it falls back to the UTF type provided in constructor.</p>
<p>Since the characters (code units) may be 8-bit, 16-bit or 32-bit. <code>AutoUTFInputStream</code> requires a character type which can hold at least 32-bit. We may use <code>unsigned</code>, as in the template parameter:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="document_8h.html">rapidjson/document.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;rapidjson/filereadstream.h&quot;</span>   <span class="comment">// FileReadStream</span></div>
<div class="line"><span class="preprocessor">#include &quot;rapidjson/encodedstream.h&quot;</span>    <span class="comment">// AutoUTFInputStream</span></div>
<div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">using namespace </span>rapidjson;</div>
<div class="line"></div>
<div class="line">FILE* fp = fopen(<span class="stringliteral">&quot;any.json&quot;</span>, <span class="stringliteral">&quot;rb&quot;</span>); <span class="comment">// non-Windows use &quot;r&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">char</span> readBuffer[256];</div>
<div class="line"><a class="code" href="classrapidjson_1_1_file_read_stream.html">FileReadStream</a> bis(fp, readBuffer, <span class="keyword">sizeof</span>(readBuffer));</div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_auto_u_t_f_input_stream.html">AutoUTFInputStream&lt;unsigned, FileReadStream&gt;</a> eis(bis);  <span class="comment">// wraps bis into eis</span></div>
<div class="line"></div>
<div class="line"><a class="code" href="classrapidjson_1_1_generic_document.html">Document</a> d;         <span class="comment">// Document is GenericDocument&lt;UTF8&lt;&gt; &gt; </span></div>
<div class="line">d.<a class="code" href="classrapidjson_1_1_generic_document.html#a3ae97682cf04685c7db9d89ebc399b85">ParseStream</a>&lt;0, <a class="code" href="structrapidjson_1_1_auto_u_t_f.html">AutoUTF&lt;unsigned&gt;</a> &gt;(eis); <span class="comment">// This parses any UTF file into UTF-8 in memory</span></div>
<div class="line"></div>
<div class="line">fclose(fp);</div>
</div><!-- fragment --><p>When specifying the encoding of stream, uses <code>AutoUTF&lt;CharType&gt;</code> as in <code>ParseStream()</code> above.</p>
<p>You can obtain the type of UTF via <code>UTFType GetType()</code>. And check whether a BOM is found by <code>HasBOM()</code></p>
<h2><a class="anchor" id="AutoUTFOutputStream"></a>
AutoUTFOutputStream</h2>
<p>Similarly, to choose encoding for output during runtime, we can use <code>AutoUTFOutputStream</code>. This class is not automatic <em>per se</em>. You need to specify the UTF type and whether to write BOM in runtime.</p>
<div class="fragment"><div class="line"><span class="keyword">using namespace </span>rapidjson;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> WriteJSONFile(FILE* fp, <a class="code" href="namespacerapidjson.html#a4aacabc0f8cea1cd628f466d890773eb">UTFType</a> type, <span class="keywordtype">bool</span> putBOM, <span class="keyword">const</span> <a class="code" href="classrapidjson_1_1_generic_document.html">Document</a>&amp; d) {</div>
<div class="line">    <span class="keywordtype">char</span> writeBuffer[256];</div>
<div class="line">    <a class="code" href="classrapidjson_1_1_file_write_stream.html">FileWriteStream</a> bos(fp, writeBuffer, <span class="keyword">sizeof</span>(writeBuffer));</div>
<div class="line"></div>
<div class="line">    <span class="keyword">typedef</span> <a class="code" href="classrapidjson_1_1_auto_u_t_f_output_stream.html">AutoUTFOutputStream&lt;unsigned, FileWriteStream&gt;</a> OutputStream;</div>
<div class="line">    OutputStream eos(bos, type, putBOM);</div>
<div class="line"></div>
<div class="line">    <a class="code" href="classrapidjson_1_1_writer.html">Writer&lt;OutputStream, UTF8&lt;&gt;</a>, <a class="code" href="structrapidjson_1_1_auto_u_t_f.html">AutoUTF&lt;&gt;</a> &gt; writer;</div>
<div class="line">    d.<a class="code" href="classrapidjson_1_1_generic_value.html#aeef0506cd1f8d703db4833c503f0657e">Accept</a>(writer);</div>
<div class="line">}</div>
</div><!-- fragment --><p><code>AutoUTFInputStream</code> and <code>AutoUTFOutputStream</code> is more convenient than <code>EncodedInputStream</code> and <code>EncodedOutputStream</code>. They just incur a little bit runtime overheads.</p>
<h1><a class="anchor" id="CustomStream"></a>
Custom Stream</h1>
<p>In addition to memory/file streams, user can create their own stream classes which fits RapidJSON's API. For example, you may create network stream, stream from compressed file, etc.</p>
<p>RapidJSON combines different types using templates. A class containing all required interface can be a stream. The Stream interface is defined in comments of <code><a class="el" href="rapidjson_8h.html" title="common definitions and configuration ">rapidjson/rapidjson.h</a></code>:</p>
<div class="fragment"><div class="line">concept Stream {</div>
<div class="line">    <span class="keyword">typename</span> Ch;    <span class="comment">//!&lt; Character type of the stream.</span></div>
<div class="line"><span class="comment"></span><span class="comment"></span></div>
<div class="line"><span class="comment">    //! Read the current character from stream without moving the read cursor.</span></div>
<div class="line"><span class="comment"></span>    Ch Peek() <span class="keyword">const</span>;</div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">    //! Read the current character from stream and moving the read cursor to next character.</span></div>
<div class="line"><span class="comment"></span>    Ch Take();</div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">    //! Get the current read cursor.</span></div>
<div class="line"><span class="comment">    //! \return Number of characters read from start.</span></div>
<div class="line"><span class="comment"></span>    <span class="keywordtype">size_t</span> Tell();</div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">    //! Begin writing operation at the current read pointer.</span></div>
<div class="line"><span class="comment">    //! \return The begin writer pointer.</span></div>
<div class="line"><span class="comment"></span>    Ch* PutBegin();</div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">    //! Write a character.</span></div>
<div class="line"><span class="comment"></span>    <span class="keywordtype">void</span> Put(Ch c);</div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">    //! Flush the buffer.</span></div>
<div class="line"><span class="comment"></span>    <span class="keywordtype">void</span> Flush();</div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">    //! End the writing operation.</span></div>
<div class="line"><span class="comment">    //! \param begin The begin write pointer returned by PutBegin().</span></div>
<div class="line"><span class="comment">    //! \return Number of characters written.</span></div>
<div class="line"><span class="comment"></span>    <span class="keywordtype">size_t</span> PutEnd(Ch* begin);</div>
<div class="line">}</div>
</div><!-- fragment --><p>For input stream, they must implement <code>Peek()</code>, <code>Take()</code> and <code>Tell()</code>. For output stream, they must implement <code>Put()</code> and <code>Flush()</code>. There are two special interface, <code>PutBegin()</code> and <code>PutEnd()</code>, which are only for <em>in situ</em> parsing. Normal streams do not implement them. However, if the interface is not needed for a particular stream, it is still need to a dummy implementation, otherwise will generate compilation error.</p>
<h2><a class="anchor" id="ExampleIStreamWrapper"></a>
Example: istream wrapper</h2>
<p>The following example is a wrapper of <code>std::istream</code>, which only implements 3 functions.</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>IStreamWrapper {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    <span class="keyword">typedef</span> <span class="keywordtype">char</span> Ch;</div>
<div class="line"></div>
<div class="line">    IStreamWrapper(std::istream&amp; is) : is_(is) {</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    Ch Peek()<span class="keyword"> const </span>{ <span class="comment">// 1</span></div>
<div class="line">        <span class="keywordtype">int</span> c = is_.peek();</div>
<div class="line">        <span class="keywordflow">return</span> c == std::char_traits&lt;char&gt;::eof() ? <span class="charliteral">&#39;\0&#39;</span> : (Ch)c;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    Ch Take() { <span class="comment">// 2</span></div>
<div class="line">        <span class="keywordtype">int</span> c = is_.get();</div>
<div class="line">        <span class="keywordflow">return</span> c == std::char_traits&lt;char&gt;::eof() ? <span class="charliteral">&#39;\0&#39;</span> : (Ch)c;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordtype">size_t</span> Tell()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> (<span class="keywordtype">size_t</span>)is_.tellg(); } <span class="comment">// 3</span></div>
<div class="line"></div>
<div class="line">    Ch* PutBegin() { assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> 0; }</div>
<div class="line">    <span class="keywordtype">void</span> Put(Ch) { assert(<span class="keyword">false</span>); }</div>
<div class="line">    <span class="keywordtype">void</span> Flush() { assert(<span class="keyword">false</span>); }</div>
<div class="line">    <span class="keywordtype">size_t</span> PutEnd(Ch*) { assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> 0; }</div>
<div class="line"></div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line">    IStreamWrapper(<span class="keyword">const</span> IStreamWrapper&amp;);</div>
<div class="line">    IStreamWrapper&amp; operator=(<span class="keyword">const</span> IStreamWrapper&amp;);</div>
<div class="line"></div>
<div class="line">    std::istream&amp; is_;</div>
<div class="line">};</div>
</div><!-- fragment --><p>User can use it to wrap instances of <code>std::stringstream</code>, <code>std::ifstream</code>.</p>
<div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>* json = <span class="stringliteral">&quot;[1,2,3,4]&quot;</span>;</div>
<div class="line">std::stringstream ss(json);</div>
<div class="line">IStreamWrapper is(ss);</div>
<div class="line"></div>
<div class="line"><a class="code" href="namespacerapidjson.html#a660c934c2959121babf799b6cb206659">Document</a> d;</div>
<div class="line">d.<a class="code" href="classrapidjson_1_1_generic_document.html#a3ae97682cf04685c7db9d89ebc399b85">ParseStream</a>(is);</div>
</div><!-- fragment --><p>Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library.</p>
<h2><a class="anchor" id="ExampleOStreamWrapper"></a>
Example: ostream wrapper</h2>
<p>The following example is a wrapper of <code>std::istream</code>, which only implements 2 functions.</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>OStreamWrapper {</div>
<div class="line"><span class="keyword">public</span>:</div>
<div class="line">    <span class="keyword">typedef</span> <span class="keywordtype">char</span> Ch;</div>
<div class="line"></div>
<div class="line">    OStreamWrapper(std::ostream&amp; os) : os_(os) {</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    Ch Peek()<span class="keyword"> const </span>{ assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> <span class="charliteral">&#39;\0&#39;</span>; }</div>
<div class="line">    Ch Take() { assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> <span class="charliteral">&#39;\0&#39;</span>; }</div>
<div class="line">    <span class="keywordtype">size_t</span> Tell()<span class="keyword"> const </span>{  }</div>
<div class="line"></div>
<div class="line">    Ch* PutBegin() { assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> 0; }</div>
<div class="line">    <span class="keywordtype">void</span> Put(Ch c) { os_.put(c); }                  <span class="comment">// 1</span></div>
<div class="line">    <span class="keywordtype">void</span> Flush() { os_.flush(); }                   <span class="comment">// 2</span></div>
<div class="line">    <span class="keywordtype">size_t</span> PutEnd(Ch*) { assert(<span class="keyword">false</span>); <span class="keywordflow">return</span> 0; }</div>
<div class="line"></div>
<div class="line"><span class="keyword">private</span>:</div>
<div class="line">    OStreamWrapper(<span class="keyword">const</span> OStreamWrapper&amp;);</div>
<div class="line">    OStreamWrapper&amp; operator=(<span class="keyword">const</span> OStreamWrapper&amp;);</div>
<div class="line"></div>
<div class="line">    std::ostream&amp; os_;</div>
<div class="line">};</div>
</div><!-- fragment --><p>User can use it to wrap instances of <code>std::stringstream</code>, <code>std::ofstream</code>.</p>
<div class="fragment"><div class="line"><a class="code" href="namespacerapidjson.html#a660c934c2959121babf799b6cb206659">Document</a> d;</div>
<div class="line"><span class="comment">// ...</span></div>
<div class="line"></div>
<div class="line">std::stringstream ss;</div>
<div class="line">OSStreamWrapper os(ss);</div>
<div class="line"></div>
<div class="line">Writer&lt;OStreamWrapper&gt; writer(os);</div>
<div class="line">d.Accept(writer);</div>
</div><!-- fragment --><p>Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library.</p>
<h1><a class="anchor" id="Summary"></a>
Summary</h1>
<p>This section describes stream classes available in RapidJSON. Memory streams are simple. File stream can reduce the memory required during JSON parsing and generation, if the JSON is stored in file system. Encoded streams converts between byte streams and character streams. Finally, user may create custom streams using a simple interface. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.7-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
  </ul>
</div>
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = 'rapidjson-doc';
    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
	    var dt = document.createElement('div');
	    dt.id = "disqus_thread";
	    (document.getElementsByClassName('contents')[0]).appendChild(dt);
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
</body>
</html>