rapidjson/example/sortkeys/sortkeys.cpp

67 lines
1.3 KiB
C++
Raw Normal View History

2018-12-04 22:40:40 +01:00
#include "rapidjson/document.h"
#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;
void printIt(const Value &doc)
2018-12-04 22:40:40 +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);
cout << endl;
2018-12-04 22:40:40 +01:00
}
struct NameComparator
2018-12-04 22:40:40 +01:00
{
bool
operator()(const GenericMember<UTF8<>, MemoryPoolAllocator<>> &lhs,
const GenericMember<UTF8<>, MemoryPoolAllocator<>> &rhs) const
{
return (strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0);
2018-12-04 22:40:40 +01:00
}
};
int main()
{
Document d = Document(kObjectType);
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);
Value a(kArrayType);
d.AddMember("alpha", a, allocator);
printIt(d);
/**
{
"zeta": false,
"gama": "test string",
"delta": 123,
"alpha": []
}
**/
std::sort(d.MemberBegin(), d.MemberEnd(), NameComparator());
2018-12-04 22:40:40 +01:00
printIt(d);
/**
{
"alpha": [],
"delta": 123,
"gama": "test string",
"zeta": false
}
**/
return 0;
}