File: json_pointers.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 (108 lines) | stat: -rw-r--r-- 3,016 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
/**
 * @file
 *
 * @brief Demonstrates how to resolve JSON pointers against the current document
 *
 */

#include <iostream>

#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/internal/json_pointer.hpp>
#include <valijson/internal/json_reference.hpp>
#include <valijson/utils/rapidjson_utils.hpp>

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

template<typename AdapterType>
std::string maybeResolveRef(const AdapterType &value, const AdapterType &root)
{
    if (!value.isObject()) {
        // Not an object, therefore not a JSON reference
        return "";
    }

    const auto &object = value.getObject();
    const auto itr = object.find("$ref");
    if (itr == object.end()) {
        // Object does not contain $ref property
        return "";
    }

    const AdapterType maybeRef = itr->second;
    if (!maybeRef.isString()) {
        return "[$ref did not contain a string value]";
    }

    // Attempt to extract a JSON pointer
    const std::string ref = maybeRef.getString();
    const auto maybePointer = valijson::internal::json_reference::getJsonReferencePointer(ref);
    if (!maybePointer) {
        return "[$ref did not contain valid JSON pointer]";
    }

    const auto refAdapter = valijson::internal::json_pointer::resolveJsonPointer(root, *maybePointer);
    if (!refAdapter.maybeString()) {
        return "[$ref did not point to a string value]";
    }

    return refAdapter.asString();
}

template<typename AdapterType>
void iterateJsonObject(const AdapterType &adapter)
{
    if (!adapter.maybeObject()) {
        cout << "Not an object." << endl;
        return;
    }

    cout << "Object members:" << endl;

    // JSON objects are an unordered collection of key-value pairs,
    // so the members of the object may be printed in an order that is
    // different to that in the source JSON document.
    for (auto member : adapter.asObject()) {
        // The key is a std::string that can be accessed using 'first'
        cout << "  " << member.first << ": ";

        // The value is just another Adapter, and can be accessed using 'second'
        const AdapterType &value = member.second;
        if (value.maybeString()) {
            cout << value.asString();
        } else {
            cout << maybeResolveRef(value, adapter);
        }

        cout << endl;
    }
}

void usingJsonCppWithTemplateFn(const char *filename)
{
    rapidjson::Document document;
    if (!valijson::utils::loadDocument(filename, document)) {
        return;
    }

    valijson::adapters::RapidJsonAdapter adapter(document);
    iterateJsonObject(adapter);
}

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

    // Load the document using jsoncpp and iterate over array using function template
    cout << "-- Resolving JSON pointers using RapidJSON --" << endl;
    usingJsonCppWithTemplateFn(argv[1]);
    cout << endl;

    return 0;
}