From c9060b4a5c29a0d9fc69573695e600add61b75fc Mon Sep 17 00:00:00 2001 From: seky Date: Tue, 4 Dec 2018 22:40:40 +0100 Subject: added example for sorting keys --- example/CMakeLists.txt | 1 + example/sortkeys/sortkeys.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 example/sortkeys/sortkeys.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ff541993..9f53c9aa 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -21,6 +21,7 @@ set(EXAMPLES simplereader simplepullreader simplewriter + sortkeys tutorial) include_directories("../include/") diff --git a/example/sortkeys/sortkeys.cpp b/example/sortkeys/sortkeys.cpp new file mode 100644 index 00000000..fb45d4aa --- /dev/null +++ b/example/sortkeys/sortkeys.cpp @@ -0,0 +1,70 @@ +#define RAPIDJSON_HAS_STDSTRING 1 +#include "rapidjson/document.h" +#include +#include + +#include +#include + +using namespace rapidjson; +using namespace std; + +void printIt(Document &doc) +{ + string output; + StringBuffer buffer; + PrettyWriter writer(buffer); + doc.Accept(writer); + + output = buffer.GetString(); + cout << output << endl; +} + +struct ValueNameComparator +{ + bool + operator()(const GenericMember, MemoryPoolAllocator<>> &lhs, + const GenericMember, MemoryPoolAllocator<>> &rhs) const + { + string lhss = string(lhs.name.GetString()); + string rhss = string(rhs.name.GetString()); + return lhss < rhss; + } +}; + +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(), ValueNameComparator()); + + printIt(d); + /** +{ + "alpha": [], + "delta": 123, + "gama": "test string", + "zeta": false +} +**/ + return 0; +} -- cgit v1.2.3 From d0188462d909d221bcd55c2a64ca5943b931dc08 Mon Sep 17 00:00:00 2001 From: seky Date: Wed, 5 Dec 2018 08:24:59 +0100 Subject: removed std::string and receiving const Value in printIt --- example/sortkeys/sortkeys.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/example/sortkeys/sortkeys.cpp b/example/sortkeys/sortkeys.cpp index fb45d4aa..85e08074 100644 --- a/example/sortkeys/sortkeys.cpp +++ b/example/sortkeys/sortkeys.cpp @@ -1,7 +1,6 @@ -#define RAPIDJSON_HAS_STDSTRING 1 #include "rapidjson/document.h" +#include "rapidjson/filewritestream.h" #include -#include #include #include @@ -9,26 +8,23 @@ using namespace rapidjson; using namespace std; -void printIt(Document &doc) +void printIt(const Value &doc) { - string output; - StringBuffer buffer; - PrettyWriter writer(buffer); + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + PrettyWriter writer(os); doc.Accept(writer); - output = buffer.GetString(); - cout << output << endl; + cout << endl; } -struct ValueNameComparator +struct NameComparator { bool operator()(const GenericMember, MemoryPoolAllocator<>> &lhs, const GenericMember, MemoryPoolAllocator<>> &rhs) const { - string lhss = string(lhs.name.GetString()); - string rhss = string(rhs.name.GetString()); - return lhss < rhss; + return (strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0); } }; @@ -55,7 +51,7 @@ int main() } **/ - std::sort(d.MemberBegin(), d.MemberEnd(), ValueNameComparator()); + std::sort(d.MemberBegin(), d.MemberEnd(), NameComparator()); printIt(d); /** -- cgit v1.2.3