Improve array_iteration_basics example

This commit is contained in:
Tristan Penman 2021-05-20 22:14:22 +10:00
parent e6909b155c
commit bb2c425104

View File

@ -1,11 +1,9 @@
/**
* @file
*
* @brief Demonstrates iteration over an array and type check functions
*
* @brief Demonstrates iteration over an array, and how to use type check functions
*/
#include <exception>
#include <iostream>
// jsoncpp
@ -24,12 +22,81 @@ 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);
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);
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)
{
@ -51,80 +118,3 @@ int main(int argc, char **argv)
return 0;
}
void usingRapidJson(const char *filename)
{
using valijson::adapters::RapidJsonAdapter;
rapidjson::Document document;
if (!valijson::utils::loadDocument(filename, document)) {
return;
}
RapidJsonAdapter adapter(document);
if (!adapter.isArray()) {
cout << "Not an array." << endl;
return;
}
cout << "Array values:" << endl;
int index = 0;
// We support the old way of doing things...
const RapidJsonAdapter::Array array = adapter.asArray();
for (RapidJsonAdapter::Array::const_iterator itr = array.begin();
itr != array.end(); ++itr) {
cout << " " << index++ << ": ";
// Each element of the array is just another RapidJsonAdapter
const RapidJsonAdapter &value = *itr;
// maybeString is a loose type check
if (value.maybeString()) {
// If a value may be a string, we are allowed to get a string
// representation of the value using asString
cout << value.asString();
}
cout << endl;
}
}
void usingJsonCpp(const char *filename)
{
Json::Value value;
if (!valijson::utils::loadDocument(filename, value)) {
return;
}
valijson::adapters::JsonCppAdapter adapter(value);
// isArray is a strict type check
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.
for (auto value : adapter.getArray()) {
cout << " " << index++ << ": ";
// isString is another strict type check. Valijson uses the convention
// that strict type check functions are prefixed with 'is'.
if (!value.isString()) {
cout << "Not a string. ";
}
try {
// Also by convention, functions prefixed with 'get' will raise a
// runtime exception if the value is not of the correct type.
cout << value.getString() << endl;
} catch (runtime_error &e) {
cout << "Caught exception: " << e.what() << endl;
}
}
}