stream.md 14.1 KB
Newer Older
miloyip's avatar
miloyip committed
1
# Stream
Milo Yip's avatar
Milo Yip committed
2

3
In RapidJSON, `rapidjson::Stream` 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.
Milo Yip's avatar
Milo Yip committed
4

5 6 7
[TOC]

# Memory Streams {#MemoryStreams}
Milo Yip's avatar
Milo Yip committed
8 9 10

Memory streams store JSON in memory.

11
## StringStream (Input) {#StringStream}
Milo Yip's avatar
Milo Yip committed
12 13 14

`StringStream` is the most basic input stream. It represents a complete, read-only JSON stored in memory. It is defined in `rapidjson/rapidjson.h`.

15
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
16 17 18 19 20 21 22 23 24 25
#include "rapidjson/document.h" // will include "rapidjson/rapidjson.h"

using namespace rapidjson;

// ...
const char json[] = "[1, 2, 3, 4]";
StringStream s(json);

Document d;
d.ParseStream(s);
26
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
27 28 29

Since this is very common usage, `Document::Parse(const char*)` is provided to do exactly the same as above:

30
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
31 32 33 34
// ...
const char json[] = "[1, 2, 3, 4]";
Document d;
d.Parse(json);
35
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
36 37 38

Note that, `StringStream` is a typedef of `GenericStringStream<UTF8<> >`, user may use another encodings to represent the character set of the stream.

39
## StringBuffer (Output) {#StringBuffer}
Milo Yip's avatar
Milo Yip committed
40 41 42

`StringBuffer` is a simple output stream. It allocates a memory buffer for writing the whole JSON. Use `GetString()` to obtain the buffer.

43
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
44
#include "rapidjson/stringbuffer.h"
KLsz's avatar
KLsz committed
45
#include <rapidjson/writer.h>
Milo Yip's avatar
Milo Yip committed
46 47 48 49 50 51

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);

const char* output = buffer.GetString();
52
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
53 54 55

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.

56
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
57 58
StringBuffer buffer1(0, 1024); // Use its allocator, initial size = 1024
StringBuffer buffer2(allocator, 1024);
59
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
60 61 62 63 64

By default, `StringBuffer` will instantiate an internal allocator.

Similarly, `StringBuffer` is a typedef of `GenericStringBuffer<UTF8<> >`.

65
# File Streams {#FileStreams}
Milo Yip's avatar
Milo Yip committed
66 67 68 69 70

When parsing a JSON from file, you may read the whole JSON into memory and use ``StringStream`` above.

However, if the JSON is big, or memory is limited, you can use `FileReadStream`. 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.

71
## FileReadStream (Input) {#FileReadStream}
Milo Yip's avatar
Milo Yip committed
72 73 74

`FileReadStream` reads the file via a `FILE` pointer. And user need to provide a buffer.

75
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
76 77 78
#include "rapidjson/filereadstream.h"
#include <cstdio>

79 80
using namespace rapidjson;

Milo Yip's avatar
Milo Yip committed
81 82 83 84 85 86 87 88 89
FILE* fp = fopen("big.json", "rb"); // non-Windows use "r"

char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));

Document d;
d.ParseStream(is);

fclose(fp);
90
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
91 92 93 94 95

Different from string streams, `FileReadStream` is byte stream. It does not handle encodings. If the file is not UTF-8, the byte stream can be wrapped in a `EncodedInputStream`. It will be discussed very soon.

Apart from reading file, user can also use `FileReadStream` to read `stdin`.

96
## FileWriteStream (Output) {#FileWriteStream}
Milo Yip's avatar
Milo Yip committed
97 98 99

`FileWriteStream` is buffered output stream. Its usage is very similar to `FileReadStream`.

100
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
101
#include "rapidjson/filewritestream.h"
KLsz's avatar
KLsz committed
102
#include <rapidjson/writer.h>
Milo Yip's avatar
Milo Yip committed
103 104
#include <cstdio>

105 106
using namespace rapidjson;

Milo Yip's avatar
Milo Yip committed
107 108 109 110 111 112 113 114 115 116 117 118 119
Document d;
d.Parse(json);
// ...

FILE* fp = fopen("output.json", "wb"); // non-Windows use "w"

char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));

Writer<FileWriteStream> writer(os);
d.Accept(writer);

fclose(fp);
120
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
121 122 123

It can also directs the output to `stdout`.

Milo Yip's avatar
Milo Yip committed
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
# iostream Wrapper {#iostreamWrapper}

Due to users' requests, RapidJSON provided official wrappers for `std::basic_istream` and `std::basic_ostream`. However, please note that the performance will be much lower than the other streams above.

## IStreamWrapper {#IStreamWrapper}

`IStreamWrapper` wraps any class drived from `std::istream`, such as `std::istringstream`, `std::stringstream`, `std::ifstream`, `std::fstream`, into RapidJSON's input stream.

~~~cpp
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <fstream>

using namespace rapidjson;
using namespace std;

ifstream ifs("test.json");
IStreamWrapper isw(ifs);

Document d;
d.ParseStream(isw);
~~~

For classes derived from `std::wistream`, use `WIStreamWrapper`.

## OStreamWrapper {#OStreamWrapper}

Similarly, `OStreamWrapper` wraps any class derived from `std::ostream`, such as `std::ostringstream`, `std::stringstream`, `std::ofstream`, `std::fstream`, into RapidJSON's input stream.

~~~cpp
#include <rapidjson/document.h>
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/writer.h>
#include <fstream>

using namespace rapidjson;
using namespace std;

Document d;
d.Parse(json);

// ...

ofstream ofs("output.json");
OStreamWrapper osw(ofs);

Writer<OStreamWrapper> writer(osw);
d.Accept(writer);
~~~

For classes derived from `std::wostream`, use `WOStreamWrapper`.

176
# Encoded Streams {#EncodedStreams}
Milo Yip's avatar
Milo Yip committed
177 178 179 180 181 182 183 184 185 186 187

Encoded streams do not contain JSON itself, but they wrap byte streams to provide basic encoding/decoding function.

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. `wchar_t` for UTF-16) while reading, and characters into bytes while writing.

Besides, it also need to handle [byte order mark (BOM)](http://en.wikipedia.org/wiki/Byte_order_mark). 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.

If the encoding of stream is known in compile-time, you may use `EncodedInputStream` and `EncodedOutputStream`. 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 `AutoUTFInputStream` and `AutoUTFOutputStream`. These streams are defined in `rapidjson/encodedstream.h`.

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.

188
## EncodedInputStream {#EncodedInputStream}
Milo Yip's avatar
Milo Yip committed
189 190 191

`EncodedInputStream` has two template parameters. The first one is a `Encoding` class, such as `UTF8`, `UTF16LE`, defined in `rapidjson/encodings.h`. The second one is the class of stream to be wrapped.

192
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"   // FileReadStream
#include "rapidjson/encodedstream.h"    // EncodedInputStream
#include <cstdio>

using namespace rapidjson;

FILE* fp = fopen("utf16le.json", "rb"); // non-Windows use "r"

char readBuffer[256];
FileReadStream bis(fp, readBuffer, sizeof(readBuffer));

EncodedInputStream<UTF16LE<>, FileReadStream> eis(bis);  // wraps bis into eis

Document d; // Document is GenericDocument<UTF8<> > 
d.ParseStream<0, UTF16LE<> >(eis);  // Parses UTF-16LE file into UTF-8 in memory

fclose(fp);
211
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
212

213
## EncodedOutputStream {#EncodedOutputStream}
Milo Yip's avatar
Milo Yip committed
214 215 216

`EncodedOutputStream` is similar but it has a `bool putBOM` parameter in the constructor, controlling whether to write BOM into output byte stream.

217
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
218 219
#include "rapidjson/filewritestream.h"  // FileWriteStream
#include "rapidjson/encodedstream.h"    // EncodedOutputStream
KLsz's avatar
KLsz committed
220
#include <rapidjson/writer.h>
Milo Yip's avatar
Milo Yip committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#include <cstdio>

Document d;         // Document is GenericDocument<UTF8<> > 
// ...

FILE* fp = fopen("output_utf32le.json", "wb"); // non-Windows use "w"

char writeBuffer[256];
FileWriteStream bos(fp, writeBuffer, sizeof(writeBuffer));

typedef EncodedOutputStream<UTF32LE<>, FileWriteStream> OutputStream;
OutputStream eos(bos, true);   // Write BOM

Writer<OutputStream, UTF32LE<>, UTF8<>> writer(eos);
d.Accept(writer);   // This generates UTF32-LE file from UTF-8 in memory

fclose(fp);
238
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
239

240
## AutoUTFInputStream {#AutoUTFInputStream}
Milo Yip's avatar
Milo Yip committed
241 242 243 244 245

Sometimes an application may want to handle all supported JSON encoding. `AutoUTFInputStream` 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.

Since the characters (code units) may be 8-bit, 16-bit or 32-bit. `AutoUTFInputStream` requires a character type which can hold at least 32-bit. We may use `unsigned`, as in the template parameter:

246
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"   // FileReadStream
#include "rapidjson/encodedstream.h"    // AutoUTFInputStream
#include <cstdio>

using namespace rapidjson;

FILE* fp = fopen("any.json", "rb"); // non-Windows use "r"

char readBuffer[256];
FileReadStream bis(fp, readBuffer, sizeof(readBuffer));

AutoUTFInputStream<unsigned, FileReadStream> eis(bis);  // wraps bis into eis

Document d;         // Document is GenericDocument<UTF8<> > 
d.ParseStream<0, AutoUTF<unsigned> >(eis); // This parses any UTF file into UTF-8 in memory

fclose(fp);
265
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
266 267 268 269 270

When specifying the encoding of stream, uses `AutoUTF<CharType>` as in `ParseStream()` above.

You can obtain the type of UTF via `UTFType GetType()`. And check whether a BOM is found by `HasBOM()`

271
## AutoUTFOutputStream {#AutoUTFOutputStream}
Milo Yip's avatar
Milo Yip committed
272 273 274

Similarly, to choose encoding for output during runtime, we can use `AutoUTFOutputStream`. This class is not automatic *per se*. You need to specify the UTF type and whether to write BOM in runtime.

275 276 277
~~~~~~~~~~cpp
using namespace rapidjson;

Milo Yip's avatar
Milo Yip committed
278 279 280 281 282 283 284 285 286 287
void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) {
    char writeBuffer[256];
    FileWriteStream bos(fp, writeBuffer, sizeof(writeBuffer));

    typedef AutoUTFOutputStream<unsigned, FileWriteStream> OutputStream;
    OutputStream eos(bos, type, putBOM);
    
    Writer<OutputStream, UTF8<>, AutoUTF<> > writer;
    d.Accept(writer);
}
288
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
289 290 291

`AutoUTFInputStream` and `AutoUTFOutputStream` is more convenient than `EncodedInputStream` and `EncodedOutputStream`. They just incur a little bit runtime overheads.

292
# Custom Stream {#CustomStream}
Milo Yip's avatar
Milo Yip committed
293 294 295 296 297

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.

RapidJSON combines different types using templates. A class containing all required interface can be a stream. The Stream interface is defined in comments of `rapidjson/rapidjson.h`:

298
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
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
concept Stream {
    typename Ch;    //!< Character type of the stream.

    //! Read the current character from stream without moving the read cursor.
    Ch Peek() const;

    //! Read the current character from stream and moving the read cursor to next character.
    Ch Take();

    //! Get the current read cursor.
    //! \return Number of characters read from start.
    size_t Tell();

    //! Begin writing operation at the current read pointer.
    //! \return The begin writer pointer.
    Ch* PutBegin();

    //! Write a character.
    void Put(Ch c);

    //! Flush the buffer.
    void Flush();

    //! End the writing operation.
    //! \param begin The begin write pointer returned by PutBegin().
    //! \return Number of characters written.
    size_t PutEnd(Ch* begin);
}
327
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
328 329 330 331 332

For input stream, they must implement `Peek()`, `Take()` and `Tell()`.
For output stream, they must implement `Put()` and `Flush()`. 
There are two special interface, `PutBegin()` and `PutEnd()`, which are only for *in situ* 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.

333
## Example: istream wrapper {#ExampleIStreamWrapper}
Milo Yip's avatar
Milo Yip committed
334

Milo Yip's avatar
Milo Yip committed
335
The following example is a simple wrapper of `std::istream`, which only implements 3 functions.
Milo Yip's avatar
Milo Yip committed
336

337
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
338
class MyIStreamWrapper {
Milo Yip's avatar
Milo Yip committed
339 340 341
public:
    typedef char Ch;

Milo Yip's avatar
Milo Yip committed
342
    MyIStreamWrapper(std::istream& is) : is_(is) {
Milo Yip's avatar
Milo Yip committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    }

    Ch Peek() const { // 1
        int c = is_.peek();
        return c == std::char_traits<char>::eof() ? '\0' : (Ch)c;
    }

    Ch Take() { // 2
        int c = is_.get();
        return c == std::char_traits<char>::eof() ? '\0' : (Ch)c;
    }

    size_t Tell() const { return (size_t)is_.tellg(); } // 3

    Ch* PutBegin() { assert(false); return 0; }
    void Put(Ch) { assert(false); }
    void Flush() { assert(false); }
    size_t PutEnd(Ch*) { assert(false); return 0; }

private:
Milo Yip's avatar
Milo Yip committed
363 364
    MyIStreamWrapper(const MyIStreamWrapper&);
    MyIStreamWrapper& operator=(const MyIStreamWrapper&);
Milo Yip's avatar
Milo Yip committed
365 366 367

    std::istream& is_;
};
368
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
369 370 371

User can use it to wrap instances of `std::stringstream`, `std::ifstream`.

372
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
373 374
const char* json = "[1,2,3,4]";
std::stringstream ss(json);
Milo Yip's avatar
Milo Yip committed
375
MyIStreamWrapper is(ss);
Milo Yip's avatar
Milo Yip committed
376 377

Document d;
378
d.ParseStream(is);
379
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
380 381 382

Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library.

383
## Example: ostream wrapper {#ExampleOStreamWrapper}
Milo Yip's avatar
Milo Yip committed
384

Milo Yip's avatar
Milo Yip committed
385
The following example is a simple wrapper of `std::istream`, which only implements 2 functions.
Milo Yip's avatar
Milo Yip committed
386

387
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
388
class MyOStreamWrapper {
Milo Yip's avatar
Milo Yip committed
389 390 391
public:
    typedef char Ch;

Milo Yip's avatar
Milo Yip committed
392
    MyOStreamWrapper(std::ostream& os) : os_(os) {
Milo Yip's avatar
Milo Yip committed
393 394 395 396 397 398 399 400 401 402 403 404
    }

    Ch Peek() const { assert(false); return '\0'; }
    Ch Take() { assert(false); return '\0'; }
    size_t Tell() const {  }

    Ch* PutBegin() { assert(false); return 0; }
    void Put(Ch c) { os_.put(c); }                  // 1
    void Flush() { os_.flush(); }                   // 2
    size_t PutEnd(Ch*) { assert(false); return 0; }

private:
Milo Yip's avatar
Milo Yip committed
405 406
    MyOStreamWrapper(const MyOStreamWrapper&);
    MyOStreamWrapper& operator=(const MyOStreamWrapper&);
Milo Yip's avatar
Milo Yip committed
407 408 409

    std::ostream& os_;
};
410
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
411 412 413

User can use it to wrap instances of `std::stringstream`, `std::ofstream`.

414
~~~~~~~~~~cpp
Milo Yip's avatar
Milo Yip committed
415 416 417 418
Document d;
// ...

std::stringstream ss;
Milo Yip's avatar
Milo Yip committed
419
MyOStreamWrapper os(ss);
Milo Yip's avatar
Milo Yip committed
420

Milo Yip's avatar
Milo Yip committed
421
Writer<MyOStreamWrapper> writer(os);
Milo Yip's avatar
Milo Yip committed
422
d.Accept(writer);
423
~~~~~~~~~~
Milo Yip's avatar
Milo Yip committed
424 425 426

Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library.

427
# Summary {#Summary}
Milo Yip's avatar
Milo Yip committed
428 429

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.