Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
prettyauto.cpp
Go to the documentation of this file.
1
// JSON pretty formatting example
2
// This example can handle UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE.
3
// The input firstly convert to UTF8, and then write to the original encoding with pretty formatting.
4
5
#include "
rapidjson/reader.h
"
6
#include "
rapidjson/prettywriter.h
"
7
#include "
rapidjson/filereadstream.h
"
8
#include "
rapidjson/filewritestream.h
"
9
#include "
rapidjson/encodedstream.h
"
// NEW
10
#include "
rapidjson/error/en.h
"
11
#ifdef _WIN32
12
#include <fcntl.h>
13
#include <
io.h
>
14
#endif
15
16
using namespace
rapidjson
;
17
18
int
main
(
int
,
char
*[]) {
19
#ifdef _WIN32
20
// Prevent Windows converting between CR+LF and LF
21
_setmode(_fileno(stdin), _O_BINARY);
// NEW
22
_setmode(_fileno(stdout), _O_BINARY);
// NEW
23
#endif
24
25
// Prepare reader and input stream.
26
//Reader reader;
27
GenericReader<AutoUTF<unsigned>
,
UTF8<>
> reader;
// CHANGED
28
char
readBuffer[65536];
29
FileReadStream
is(stdin, readBuffer,
sizeof
(readBuffer));
30
AutoUTFInputStream<unsigned, FileReadStream>
eis(is);
// NEW
31
32
// Prepare writer and output stream.
33
char
writeBuffer[65536];
34
FileWriteStream
os(stdout, writeBuffer,
sizeof
(writeBuffer));
35
36
#if 1
37
// Use the same Encoding of the input. Also use BOM according to input.
38
typedef
AutoUTFOutputStream<unsigned, FileWriteStream>
OutputStream;
// NEW
39
OutputStream eos(os, eis.
GetType
(), eis.
HasBOM
());
// NEW
40
PrettyWriter<OutputStream, UTF8<>
,
AutoUTF<unsigned>
> writer(eos);
// CHANGED
41
#else
42
// You may also use static bound encoding type, such as output to UTF-16LE with BOM
43
typedef
EncodedOutputStream<UTF16LE<>
,
FileWriteStream
> OutputStream;
// NEW
44
OutputStream eos(os,
true
);
// NEW
45
PrettyWriter<OutputStream, UTF8<>
,
UTF16LE<>
> writer(eos);
// CHANGED
46
#endif
47
48
// JSON reader parse from the input stream and let writer generate the output.
49
//if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
50
if
(!reader.
Parse
<
kParseValidateEncodingFlag
>(eis, writer)) {
// CHANGED
51
fprintf(stderr,
"\nError(%u): %s\n"
,
static_cast<
unsigned
>
(reader.
GetErrorOffset
()),
GetParseError_En
(reader.
GetParseErrorCode
()));
52
return
1;
53
}
54
55
return
0;
56
}
main
int main()
Definition
archivertest.cpp:283
AutoUTFInputStream
Input stream wrapper with dynamically bound encoding and automatic encoding detection.
Definition
encodedstream.h:135
AutoUTFInputStream::HasBOM
bool HasBOM() const
Definition
encodedstream.h:154
AutoUTFInputStream::GetType
UTFType GetType() const
Definition
encodedstream.h:153
AutoUTFOutputStream
Output stream wrapper with dynamically bound encoding and automatic encoding detection.
Definition
encodedstream.h:233
EncodedOutputStream
Output byte stream wrapper with statically bound encoding.
Definition
encodedstream.h:100
FileReadStream
File byte stream for input using fread().
Definition
filereadstream.h:34
FileWriteStream
Wrapper of C file stream for output using fwrite().
Definition
filewritestream.h:32
GenericReader
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition
reader.h:537
GenericReader::Parse
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition
reader.h:557
GenericReader::GetParseErrorCode
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition
reader.h:683
GenericReader::GetErrorOffset
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition
reader.h:686
PrettyWriter
Writer with indentation and spacing.
Definition
prettywriter.h:48
en.h
encodedstream.h
filereadstream.h
filewritestream.h
GetParseError_En
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition
en.h:36
io.h
rapidjson
main RapidJSON namespace
prettywriter.h
reader.h
kParseValidateEncodingFlag
@ kParseValidateEncodingFlag
Validate encoding of JSON strings.
Definition
reader.h:148
AutoUTF
Dynamically select encoding according to stream's runtime-specified UTF encoding type.
Definition
encodings.h:615
UTF16LE
UTF-16 little endian encoding.
Definition
encodings.h:342
UTF8
UTF-8 encoding.
Definition
encodings.h:96
external
rapidjson
example
prettyauto
prettyauto.cpp
Generated on
for Electroneum by
1.17.0