Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sortkeys.cpp « sortkeys « example - github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 85e080740eafee81d959b3465a99c2fa26cb41be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "rapidjson/document.h"
#include "rapidjson/filewritestream.h"
#include <rapidjson/prettywriter.h>

#include <algorithm>
#include <iostream>

using namespace rapidjson;
using namespace std;

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 GenericMember<UTF8<>, MemoryPoolAllocator<>> &lhs,
               const GenericMember<UTF8<>, MemoryPoolAllocator<>> &rhs) const
    {
        return (strcmp(lhs.name.GetString(), rhs.name.GetString()) < 0);
    }
};

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());

    printIt(d);
    /**
{
  "alpha": [],
  "delta": 123,
  "gama": "test string",
  "zeta": false
}
**/
    return 0;
}