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 
14 using namespace rapidjson;
15 
16 template<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 
43 private:
45  CapitalizeFilter& operator=(const CapitalizeFilter&);
46 };
47 
48 int 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));
57  Writer<FileWriteStream> writer(os);
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 }
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:558
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
JSON writer.
Definition: fwd.h:95
bool Int(int i)
Definition: capitalize.cpp:22
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition: reader.h:684
bool String(const char *str, SizeType length, bool)
Definition: capitalize.cpp:28
Wrapper of C file stream for input using fread().
bool EndArray(SizeType elementCount)
Definition: capitalize.cpp:38
bool Uint64(uint64_t u)
Definition: capitalize.cpp:25
bool Uint(unsigned u)
Definition: capitalize.cpp:23
bool Key(const char *str, SizeType length, bool copy)
Definition: capitalize.cpp:35
bool Double(double d)
Definition: capitalize.cpp:26
unsigned __int64 uint64_t
Definition: stdint.h:136
CapitalizeFilter(OutputHandler &out)
Definition: capitalize.cpp:18
int main(int, char *[])
Definition: capitalize.cpp:48
main RapidJSON namespace
bool EndObject(SizeType memberCount)
Definition: capitalize.cpp:36
File byte stream for input using fread().
std::vector< char > buffer_
Definition: capitalize.cpp:41
signed __int64 int64_t
Definition: stdint.h:135
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: reader.h:687
bool RawNumber(const char *str, SizeType length, bool copy)
Definition: capitalize.cpp:27
bool Bool(bool b)
Definition: capitalize.cpp:21
bool Int64(int64_t i)
Definition: capitalize.cpp:24
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition: en.h:36
OutputHandler & out_
Definition: capitalize.cpp:40


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:08