2018-12-04 22:40:40 +01:00
|
|
|
#include "rapidjson/document.h"
|
2018-12-05 08:24:59 +01:00
|
|
|
#include "rapidjson/filewritestream.h"
|
2018-12-04 22:40:40 +01:00
|
|
|
#include <rapidjson/prettywriter.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace rapidjson;
|
|
|
|
using namespace std;
|
|
|
|
|
2019-02-06 19:59:09 +08:00
|
|
|
static void printIt(const Value &doc) {
|
2018-12-05 08:24:59 +01:00
|
|
|
char writeBuffer[65536];
|
|
|
|
FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
|
|
|
|
PrettyWriter<FileWriteStream> writer(os);
|
2018-12-04 22:40:40 +01:00
|
|
|
doc.Accept(writer);
|
2018-12-05 08:24:59 +01:00
|
|
|
cout << endl;
|
2018-12-04 22:40:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 19:59:09 +08:00
|
|
|
struct NameComparator {
|
2019-02-08 11:39:25 +08:00
|
|
|
bool operator()(const Value::Member &lhs, const Value::Member &rhs) const {
|
2018-12-05 08:24:59 +01:00
|
|
|
return (strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0);
|
2018-12-04 22:40:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-06 19:59:09 +08:00
|
|
|
int main() {
|
2019-02-06 20:35:20 +08:00
|
|
|
Document d(kObjectType);
|
2018-12-04 22:40:40 +01:00
|
|
|
Document::AllocatorType &allocator = d.GetAllocator();
|
|
|
|
|
|
|
|
d.AddMember("zeta", Value().SetBool(false), allocator);
|
|
|
|
d.AddMember("gama", Value().SetString("test string", allocator), allocator);
|
|
|
|
d.AddMember("delta", Value().SetInt(123), allocator);
|
2019-02-06 19:59:09 +08:00
|
|
|
d.AddMember("alpha", Value(kArrayType).Move(), allocator);
|
2018-12-04 22:40:40 +01:00
|
|
|
|
|
|
|
printIt(d);
|
|
|
|
|
2019-02-06 19:59:09 +08:00
|
|
|
/*
|
2018-12-04 22:40:40 +01:00
|
|
|
{
|
|
|
|
"zeta": false,
|
|
|
|
"gama": "test string",
|
|
|
|
"delta": 123,
|
|
|
|
"alpha": []
|
|
|
|
}
|
2019-02-06 19:59:09 +08:00
|
|
|
*/
|
2018-12-04 22:40:40 +01:00
|
|
|
|
2019-02-11 14:14:35 +08:00
|
|
|
// C++11 supports std::move() of Value so it always have no problem for std::sort().
|
|
|
|
// Some C++03 implementations of std::sort() requires copy constructor which causes compilation error.
|
|
|
|
// Needs a sorting function only depends on std::swap() instead.
|
2019-04-28 00:51:20 +02:00
|
|
|
#if __cplusplus >= 201103L || (!defined(__GLIBCXX__) && (!defined(_MSC_VER) || _MSC_VER >= 1900))
|
2018-12-05 08:24:59 +01:00
|
|
|
std::sort(d.MemberBegin(), d.MemberEnd(), NameComparator());
|
2019-02-11 14:14:35 +08:00
|
|
|
|
2018-12-04 22:40:40 +01:00
|
|
|
printIt(d);
|
2019-02-06 19:59:09 +08:00
|
|
|
|
|
|
|
/*
|
2018-12-04 22:40:40 +01:00
|
|
|
{
|
|
|
|
"alpha": [],
|
|
|
|
"delta": 123,
|
|
|
|
"gama": "test string",
|
|
|
|
"zeta": false
|
|
|
|
}
|
2019-02-06 19:59:09 +08:00
|
|
|
*/
|
2019-04-28 00:51:20 +02:00
|
|
|
#endif
|
2018-12-04 22:40:40 +01:00
|
|
|
}
|