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:
authorMilo Yip <miloyip@gmail.com>2014-07-12 23:51:18 +0400
committerMilo Yip <miloyip@gmail.com>2014-07-12 23:51:18 +0400
commitc203f682e9a2b063e3dd34d3e52c184cf14e13d9 (patch)
tree73b00505f157bd9a97bd4b4454cc0f622726b06c /example
parenta73ed78fd6d8c6cc1e565e0085b6e4a8711183b7 (diff)
Add capitalize example
Diffstat (limited to 'example')
-rw-r--r--example/capitalize/capitalize.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/example/capitalize/capitalize.cpp b/example/capitalize/capitalize.cpp
new file mode 100644
index 00000000..25bef3a2
--- /dev/null
+++ b/example/capitalize/capitalize.cpp
@@ -0,0 +1,61 @@
+// JSON condenser exmaple
+
+// This example parses JSON from stdin with validation,
+// and re-output the JSON content to stdout with all string capitalized, and without whitespace.
+
+#include "rapidjson/reader.h"
+#include "rapidjson/writer.h"
+#include "rapidjson/filereadstream.h"
+#include "rapidjson/filewritestream.h"
+#include "rapidjson/error/en.h"
+#include <vector>
+#include <cctype>
+
+using namespace rapidjson;
+
+template<typename OutputHandler>
+struct CapitalizeFilter {
+ CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {}
+
+ bool Null() { return out_.Null(); }
+ bool Bool(bool b) { return out_.Bool(b); }
+ bool Int(int i) { return out_.Int(i); }
+ bool Uint(unsigned u) { return out_.Uint(u); }
+ bool Int64(int64_t i) { return out_.Int64(i); }
+ bool Uint64(uint64_t u) { return out_.Uint64(u); }
+ bool Double(double d) { return out_.Double(d); }
+ bool String(const char* str, SizeType length, bool) {
+ buffer_.clear();
+ for (SizeType i = 0; i < length; i++)
+ buffer_.push_back(std::toupper(str[i]));
+ return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string
+ }
+ bool StartObject() { return out_.StartObject(); }
+ bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); }
+ bool StartArray() { return out_.StartArray(); }
+ bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); }
+
+ OutputHandler& out_;
+ std::vector<char> buffer_;
+};
+
+int main(int, char*[]) {
+ // Prepare JSON reader and input stream.
+ Reader reader;
+ char readBuffer[65536];
+ FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
+
+ // Prepare JSON writer and output stream.
+ char writeBuffer[65536];
+ FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
+ Writer<FileWriteStream> writer(os);
+
+ // JSON reader parse from the input stream and let writer generate the output.
+ CapitalizeFilter<Writer<FileWriteStream> > filter(writer);
+ if (!reader.Parse(is, filter)) {
+ fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode()));
+ return 1;
+ }
+
+ return 0;
+}