Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
capitalize.cpp
Go to the documentation of this file.
1// JSON condenser example
2
3// This example parses JSON from stdin with validation,
4// and re-output the JSON content to stdout with all string capitalized, and without whitespace.
5
6#include "rapidjson/reader.h"
7#include "rapidjson/writer.h"
10#include "rapidjson/error/en.h"
11#include <vector>
12#include <cctype>
13
14using namespace rapidjson;
15
16template<typename OutputHandler>
18 CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {}
19
20 bool Null() { return out_.Null(); }
21 bool Bool(bool b) { return out_.Bool(b); }
22 bool Int(int i) { return out_.Int(i); }
23 bool Uint(unsigned u) { return out_.Uint(u); }
24 bool Int64(int64_t i) { return out_.Int64(i); }
25 bool Uint64(uint64_t u) { return out_.Uint64(u); }
26 bool Double(double d) { return out_.Double(d); }
27 bool RawNumber(const char* str, SizeType length, bool copy) { return out_.RawNumber(str, length, copy); }
28 bool String(const char* str, SizeType length, bool) {
29 buffer_.clear();
30 for (SizeType i = 0; i < length; i++)
31 buffer_.push_back(static_cast<char>(std::toupper(str[i])));
32 return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string
33 }
34 bool StartObject() { return out_.StartObject(); }
35 bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); }
36 bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); }
37 bool StartArray() { return out_.StartArray(); }
38 bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); }
39
40 OutputHandler& out_;
41 std::vector<char> buffer_;
42
43private:
45 CapitalizeFilter& operator=(const CapitalizeFilter&);
46};
47
48int main(int, char*[]) {
49 // Prepare JSON reader and input stream.
50 Reader reader;
51 char readBuffer[65536];
52 FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
53
54 // Prepare JSON writer and output stream.
55 char writeBuffer[65536];
56 FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
58
59 // JSON reader parse from the input stream and let writer generate the output.
61 if (!reader.Parse(is, filter)) {
62 fprintf(stderr, "\nError(%u): %s\n", static_cast<unsigned>(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode()));
63 return 1;
64 }
65
66 return 0;
67}
File byte stream for input using fread().
Wrapper of C file stream for output using fwrite().
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition reader.h:557
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition reader.h:683
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition reader.h:686
JSON writer.
Definition writer.h:89
os_t os
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition en.h:36
main RapidJSON namespace
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:384
signed __int64 int64_t
Definition stdint.h:135
unsigned __int64 uint64_t
Definition stdint.h:136
bool Uint(unsigned u)
bool Double(double d)
bool EndArray(SizeType elementCount)
bool RawNumber(const char *str, SizeType length, bool copy)
bool Int64(int64_t i)
bool Int(int i)
bool Uint64(uint64_t u)
std::vector< char > buffer_
OutputHandler & out_
bool Bool(bool b)
bool String(const char *str, SizeType length, bool)
bool Key(const char *str, SizeType length, bool copy)
bool EndObject(SizeType memberCount)
CapitalizeFilter(OutputHandler &out)
CK_ULONG d