You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/stream.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Stream
2
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.
3
+
In RapidJSON, `rapidjson::Stream` is a concept for reading/writing JSON. Here we'll first show you how to use provided streams. And then see how to create a custom stream.
4
4
5
5
[TOC]
6
6
@@ -51,7 +51,7 @@ d.Accept(writer);
51
51
const char* output = buffer.GetString();
52
52
~~~~~~~~~~
53
53
54
-
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.
54
+
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 an initial capacity.
55
55
56
56
~~~~~~~~~~cpp
57
57
StringBuffer buffer1(0, 1024); // Use its allocator, initial size = 1024
@@ -89,7 +89,7 @@ d.ParseStream(is);
89
89
fclose(fp);
90
90
~~~~~~~~~~
91
91
92
-
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.
92
+
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`. We will discuss more about this later in this tutorial.
93
93
94
94
Apart from reading file, user can also use `FileReadStream` to read `stdin`.
95
95
@@ -119,11 +119,11 @@ d.Accept(writer);
119
119
fclose(fp);
120
120
~~~~~~~~~~
121
121
122
-
It can also directs the output to `stdout`.
122
+
It can also redirect the output to `stdout`.
123
123
124
124
# iostream Wrapper {#iostreamWrapper}
125
125
126
-
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.
126
+
Due to users' requests, RapidJSON also provides 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.
127
127
128
128
## IStreamWrapper {#IStreamWrapper}
129
129
@@ -181,7 +181,7 @@ As mentioned above, UTF-8 byte streams can be read directly. However, UTF-16 and
181
181
182
182
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.
183
183
184
-
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`.
184
+
If the encoding of stream is known during 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`.
185
185
186
186
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.
// In this case, IsUint()/IsInt64()/IsUint64() also return true.
86
86
assert(document["i"].IsInt());
87
87
printf("i = %d\n", document["i"].GetInt());
88
-
// Alternative (int)document["i"]
88
+
// Alternatively (int)document["i"]
89
89
90
90
assert(document["pi"].IsNumber());
91
91
assert(document["pi"].IsDouble());
@@ -113,7 +113,7 @@ a[2] = 3
113
113
a[3] = 4
114
114
~~~~~~~~~~
115
115
116
-
Note that, RapidJSON does not automatically convert values between JSON types. If a value is a string, it is invalid to call `GetInt()`, for example. In debug mode it will fail an assertion. In release mode, the behavior is undefined.
116
+
Note that, RapidJSON does not automatically convert values between JSON types. For example, if a value is a string, it is invalid to call `GetInt()`. In debug mode it will fail on assertion. In release mode, the behavior is undefined.
117
117
118
118
In the following sections we discuss details about querying individual types.
119
119
@@ -168,9 +168,9 @@ Type of member pi is Number
168
168
Type of member a is Array
169
169
~~~~~~~~~~
170
170
171
-
Note that, when `operator[](const char*)` cannot find the member, it will fail an assertion.
171
+
Note that, when `operator[](const char*)` cannot find the member, it will fail on assertion.
172
172
173
-
If we are unsure whether a member exists, we need to call `HasMember()` before calling `operator[](const char*)`. However, this incurs two lookup. A better way is to call `FindMember()`, which can check the existence of member and obtain its value at once:
173
+
If we are unsure whether a member exists, we need to call `HasMember()` before calling `operator[](const char*)`. However, this incurs two lookup. A better way is to call `FindMember()`, which can check the existence of a member and obtain its value at once:
@@ -221,18 +221,18 @@ When obtaining the numeric values, `GetDouble()` will convert internal integer r
221
221
222
222
## Query String {#QueryString}
223
223
224
-
In addition to `GetString()`, the `Value` class also contains `GetStringLength()`. Here explains why.
224
+
In addition to `GetString()`, the `Value` class also contains `GetStringLength()`. Here explains why:
225
225
226
-
According to RFC 4627, JSON strings can contain Unicode character `U+0000`, which must be escaped as `"\u0000"`. The problem is that, C/C++ often uses null-terminated string, which treats ``\0'` as the terminator symbol.
226
+
According to RFC 4627, JSON strings can contain Unicode character `U+0000`, which must be escaped as `"\u0000"`. The problem is that, C/C++ often uses null-terminated string, which treats `\0` as the terminator symbol.
227
227
228
-
To conform RFC 4627, RapidJSON supports string containing `U+0000`. If you need to handle this, you can use `GetStringLength()` to obtain the correct string length.
228
+
To conform with RFC 4627, RapidJSON supports string containing `U+0000` character. If you need to handle this, you can use `GetStringLength()` to obtain the correct string length.
229
229
230
-
For example, after parsing a the following JSON to `Document d`:
230
+
For example, after parsing the following JSON to `Document d`:
231
231
232
232
~~~~~~~~~~js
233
233
{ "s" : "a\u0000b" }
234
234
~~~~~~~~~~
235
-
The correct length of the value`"a\u0000b"` is 3. But `strlen()` returns 1.
235
+
The correct length of the string`"a\u0000b"` is 3, as returned by `GetStringLength()`. But `strlen()` returns 1.
236
236
237
237
`GetStringLength()` can also improve performance, as user may often need to call `strlen()` for allocating buffer.
238
238
@@ -246,7 +246,7 @@ which accepts the length of string as parameter. This constructor supports stori
246
246
247
247
## Comparing values
248
248
249
-
You can use `==` and `!=` to compare values. Two values are equal if and only if they are have same type and contents. You can also compare values with primitive types. Here is an example.
249
+
You can use `==` and `!=` to compare values. Two values are equal if and only if they have same type and contents. You can also compare values with primitive types. Here is an example:
250
250
251
251
~~~~~~~~~~cpp
252
252
if (document["hello"] == document["n"]) /*...*/; // Compare values
@@ -264,7 +264,7 @@ Note that, currently if an object contains duplicated named member, comparing eq
264
264
There are several ways to create values. After a DOM tree is created and/or modified, it can be saved as JSON again using `Writer`.
265
265
266
266
## Change Value Type {#ChangeValueType}
267
-
When creating a Value or Document by default constructor, its type is Null. To change its type, call `SetXXX()` or assignment operator, for example:
267
+
When creating a `Value` or `Document` by default constructor, its type is Null. To change its type, call `SetXXX()` or assignment operator, for example:
268
268
269
269
~~~~~~~~~~cpp
270
270
Document d; // Null
@@ -285,7 +285,7 @@ Value u(123u); // calls Value(unsigned)
285
285
Value d(1.5); // calls Value(double)
286
286
~~~~~~~~~~
287
287
288
-
To create empty object or array, you may use `SetObject()`/`SetArray()` after default constructor, or using the `Value(Type)` in one shot:
288
+
To create empty object or array, you may use `SetObject()`/`SetArray()` after default constructor, or using the `Value(Type)` in one call:
289
289
290
290
~~~~~~~~~~cpp
291
291
Value o(kObjectType);
@@ -299,7 +299,7 @@ A very special decision during design of RapidJSON is that, assignment of value
299
299
~~~~~~~~~~cpp
300
300
Value a(123);
301
301
Value b(456);
302
-
b = a; // a becomes a Null value, b becomes number 123.
302
+
a = b; // b becomes a Null value, a becomes number 456.
303
303
~~~~~~~~~~
304
304
305
305

@@ -367,7 +367,7 @@ RapidJSON provides two strategies for storing string.
367
367
368
368
Copy-string is always safe because it owns a copy of the data. Const-string can be used for storing a string literal, and for in-situ parsing which will be mentioned in the DOM section.
369
369
370
-
To make memory allocation customizable, RapidJSON requires users to pass an instance of allocator, whenever an operation may require allocation. This design is needed to prevent storing a allocator (or Document) pointer per Value.
370
+
To make memory allocation customizable, RapidJSON requires users to pass an instance of allocator, whenever an operation may require allocation. This design is needed to prevent storing an allocator (or Document) pointer per Value.
371
371
372
372
Therefore, when we assign a copy-string, we call this overloaded `SetString()` with allocator:
0 commit comments