File: array_iteration_basics.cpp

package info (click to toggle)
valijson 1.0.3%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,756 kB
  • sloc: cpp: 19,769; sh: 134; makefile: 24
file content (120 lines) | stat: -rw-r--r-- 3,264 bytes parent folder | download | duplicates (3)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * @file
 *
 * @brief Demonstrates iteration over an array, and how to use type check functions
 */

#include <iostream>

// jsoncpp
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/utils/jsoncpp_utils.hpp>

// RapidJSON
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp>

using std::cerr;
using std::cout;
using std::endl;
using std::runtime_error;

// The first example uses RapidJson to load a JSON document. If the document
// contains an array, this function will print any array values that have a
// valid string representation.
void usingRapidJson(const char *filename)
{
    using valijson::adapters::RapidJsonAdapter;

    rapidjson::Document document;
    if (!valijson::utils::loadDocument(filename, document)) {
        return;
    }

    const RapidJsonAdapter adapter(document);
    if (!adapter.isArray()) {
        cout << "Not an array." << endl;
        return;
    }

    cout << "Array values:" << endl;
    int index = 0;

    const RapidJsonAdapter::Array array = adapter.asArray();
    for (auto &&item : array) {
        cout << "  " << index++ << ": ";

        // maybeString is a loose type check
        if (item.maybeString()) {
            // If a value may be a string, we are allowed to get a string
            // representation of the value using asString
            cout << item.asString();
        }

        cout << endl;
    }
}

// The second example uses JsonCpp to perform the same task, but unlike the
// RapidJson example, we see how to use strict type checks and exception
// handling.
void usingJsonCpp(const char *filename)
{
    using valijson::adapters::JsonCppAdapter;

    Json::Value value;
    if (!valijson::utils::loadDocument(filename, value)) {
        return;
    }

    const JsonCppAdapter adapter(value);
    if (!adapter.isArray()) {
        cout << "Not an array." << endl;
        return;
    }

    cout << "Array values:" << endl;
    int index = 0;

    // If a value is not an array, then calling getArray will cause a runtime
    // exception to be raised.
    const JsonCppAdapter::Array array = adapter.getArray();
    for (auto &&item : array) {
        cout << "  " << index++ << ": ";

        // isString is another strict type check. Valijson uses the convention
        // that strict type check functions are prefixed with 'is'.
        if (!item.isString()) {
            cout << "Not a string. ";
        }

        try {
            // Also by convention, functions prefixed with 'get' will raise a
            // runtime exception if the item is not of the correct type.
            cout << item.getString() << endl;
        } catch (const runtime_error &e) {
            cout << "Caught exception: " << e.what() << endl;
        }
    }
}

int main(int argc, char **argv)
{
    if (argc != 2) {
        cerr << "Usage: " << endl;
        cerr << "  " << argv[0] << " <filename>" << endl;
        return 1;
    }

    // Load the document using rapidjson
    cout << "-- Iteration using RapidJSON --" << endl;
    usingRapidJson(argv[1]);
    cout << endl;

    // Load the document using jsoncpp
    cout << "-- Iteration using jsoncpp --" << endl;
    usingJsonCpp(argv[1]);
    cout << endl;

    return 0;
}