]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rapidjson/example/capitalize/capitalize.cpp
1 // JSON condenser example
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.
6 #include "rapidjson/reader.h"
7 #include "rapidjson/writer.h"
8 #include "rapidjson/filereadstream.h"
9 #include "rapidjson/filewritestream.h"
10 #include "rapidjson/error/en.h"
14 using namespace rapidjson
;
16 template<typename OutputHandler
>
17 struct CapitalizeFilter
{
18 CapitalizeFilter(OutputHandler
& out
) : out_(out
), buffer_() {}
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) {
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
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
); }
41 std::vector
<char> buffer_
;
44 CapitalizeFilter(const CapitalizeFilter
&);
45 CapitalizeFilter
& operator=(const CapitalizeFilter
&);
48 int main(int, char*[]) {
49 // Prepare JSON reader and input stream.
51 char readBuffer
[65536];
52 FileReadStream
is(stdin
, readBuffer
, sizeof(readBuffer
));
54 // Prepare JSON writer and output stream.
55 char writeBuffer
[65536];
56 FileWriteStream
os(stdout
, writeBuffer
, sizeof(writeBuffer
));
57 Writer
<FileWriteStream
> writer(os
);
59 // JSON reader parse from the input stream and let writer generate the output.
60 CapitalizeFilter
<Writer
<FileWriteStream
> > filter(writer
);
61 if (!reader
.Parse(is
, filter
)) {
62 fprintf(stderr
, "\nError(%u): %s\n", static_cast<unsigned>(reader
.GetErrorOffset()), GetParseError_En(reader
.GetParseErrorCode()));