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
|
/**
* @file
*
* @brief Demonstrates iteration over an array using template functions
*
*/
#include <iostream>
#include <json/json.h>
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/utils/jsoncpp_utils.hpp>
using std::cerr;
using std::cout;
using std::endl;
template<class AdapterType>
void iterateJsonArray(const AdapterType &adapter)
{
if (!adapter.isArray()) {
cout << "Not an array." << endl;
return;
}
cout << "Array values:" << endl;
int index = 0;
for (auto value : adapter.getArray()) {
cout << " " << index++ << ": ";
if (value.maybeString()) {
cout << value.asString();
}
cout << endl;
}
}
void usingJsonCppWithTemplateFn(const char *filename)
{
Json::Value value;
if (!valijson::utils::loadDocument(filename, value)) {
return;
}
valijson::adapters::JsonCppAdapter adapter(value);
iterateJsonArray(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 << "-- Array iteration using jsoncpp via template function --" << endl;
usingJsonCppWithTemplateFn(argv[1]);
cout << endl;
return 0;
}
|