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

github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormiloyip@gmail.com <miloyip@gmail.com@c5894555-1306-4e8d-425f-1f6f381ee07c>2011-12-03 15:14:39 +0400
committermiloyip@gmail.com <miloyip@gmail.com@c5894555-1306-4e8d-425f-1f6f381ee07c>2011-12-03 15:14:39 +0400
commitbf8fcd19c189df01012b316b5dd7b550f513eab4 (patch)
tree00a2bec4f1b8a665f4ca524ab7527c4dc1f620b8 /example
parent7c914a9d4cb39de8e54fbe6c07a7d91072d9ef15 (diff)
Added prettyauto example, which can handle UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE
Fixed a bug for using Reader with AutoUTFInputStream git-svn-id: https://rapidjson.googlecode.com/svn/trunk@50 c5894555-1306-4e8d-425f-1f6f381ee07c
Diffstat (limited to 'example')
-rw-r--r--example/pretty/pretty.cpp1
-rw-r--r--example/prettyauto/prettyauto.cpp55
2 files changed, 56 insertions, 0 deletions
diff --git a/example/pretty/pretty.cpp b/example/pretty/pretty.cpp
index a48f24f9..950d6366 100644
--- a/example/pretty/pretty.cpp
+++ b/example/pretty/pretty.cpp
@@ -1,4 +1,5 @@
// JSON pretty formatting example
+// This example can only handle UTF-8. For handling other encodings, see prettyauto example.
#include "rapidjson/reader.h"
#include "rapidjson/prettywriter.h"
diff --git a/example/prettyauto/prettyauto.cpp b/example/prettyauto/prettyauto.cpp
new file mode 100644
index 00000000..bb970cd9
--- /dev/null
+++ b/example/prettyauto/prettyauto.cpp
@@ -0,0 +1,55 @@
+// JSON pretty formatting example
+// This example can handle UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE.
+// The input firstly convert to UTF8, and then write to the original encoding with pretty formatting.
+
+#include "rapidjson/reader.h"
+#include "rapidjson/prettywriter.h"
+#include "rapidjson/filereadstream.h"
+#include "rapidjson/filewritestream.h"
+#include "rapidjson/encodedstream.h" // NEW
+#ifdef _WIN32
+#include <fcntl.h>
+#include <io.h>
+#endif
+
+using namespace rapidjson;
+
+int main(int argc, char* argv[]) {
+#ifdef _WIN32
+ // Prevent Windows converting between CR+LF and LF
+ _setmode(_fileno(stdin), _O_BINARY); // NEW
+ _setmode(_fileno(stdout), _O_BINARY); // NEW
+#endif
+
+ // Prepare reader and input stream.
+ //Reader reader;
+ GenericReader<AutoUTF<unsigned>, UTF8<> > reader; // CHANGED
+ char readBuffer[65536];
+ FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
+ AutoUTFInputStream<unsigned, FileReadStream> eis(is); // NEW
+
+ // Prepare writer and output stream.
+ char writeBuffer[65536];
+ FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
+
+#if 1
+ // Use the same Encoding of the input. Also use BOM according to input.
+ typedef AutoUTFOutputStream<unsigned, FileWriteStream> OutputStream; // NEW
+ OutputStream eos(os, eis.GetType(), eis.HasBOM()); // NEW
+ PrettyWriter<OutputStream, UTF8<>, AutoUTF<unsigned> > writer(eos); // CHANGED
+#else
+ // You may also use static bound encoding type, such as output to UTF-16LE with BOM
+ typedef EncodedOutputStream<UTF16LE<>,FileWriteStream> OutputStream; // NEW
+ OutputStream eos(os, true); // NEW
+ PrettyWriter<OutputStream, UTF8<>, UTF16LE<> > writer(eos); // CHANGED
+#endif
+
+ // JSON reader parse from the input stream and let writer generate the output.
+ //if (!reader.Parse<kParseValidateEncodingFlag>(is, writer)) {
+ if (!reader.Parse<kParseValidateEncodingFlag>(eis, writer)) { // CHANGED
+ fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), reader.GetParseError());
+ return 1;
+ }
+
+ return 0;
+}