Sha256: 2f936b2fdb6855c9c72be0389b77c81b78e49976e15a7c13a53652a91483408f

Contents?: true

Size: 1.57 KB

Versions: 8

Compression:

Stored size: 1.57 KB

Contents

#include "rapidjson/document.h"
#include "rapidjson/filewritestream.h"
#include <rapidjson/prettywriter.h>

#include <algorithm>
#include <iostream>

using namespace rapidjson;
using namespace std;

static void printIt(const Value &doc) {
    char writeBuffer[65536];
    FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
    PrettyWriter<FileWriteStream> writer(os);
    doc.Accept(writer);
    cout << endl;
}

struct NameComparator {
    bool operator()(const Value::Member &lhs, const Value::Member &rhs) const {
        return (strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0);
    }
};

int main() {
    Document d(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);
    d.AddMember("alpha", Value(kArrayType).Move(), allocator);

    printIt(d);

/*
{
    "zeta": false,
    "gama": "test string",
    "delta": 123,
    "alpha": []
}
*/

// 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.
#if __cplusplus >= 201103L || (!defined(__GLIBCXX__) && (!defined(_MSC_VER) || _MSC_VER >= 1900))
    std::sort(d.MemberBegin(), d.MemberEnd(), NameComparator());

    printIt(d);

/*
{
  "alpha": [],
  "delta": 123,
  "gama": "test string",
  "zeta": false
}
*/
#endif
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rj_schema-1.0.5 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-1.0.4 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-1.0.3 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-1.0.2 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-1.0.1 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-1.0.0 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-0.2.6 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp
rj_schema-0.2.5 ext/rj_schema/rapidjson/example/sortkeys/sortkeys.cpp