config file name
How do I specify, which file should be used for reading/writing?
The manual is only showing of how to create a Json::Value object
Value root;
how to get data from it
string encoding = root.get("encoding", "UTF-8").asString();
and, probably, how to write new data in it, which I couldn't find yet.
What I need is a library, which would create new cfg files, with the folder/name.cfg structure, based on two variable values, in case if there isn't already a cfg file at this path. Then, it should fill the cfg file with some data (not from cin), which would be accessed later. Is it possible to use jsoncpp for this task?
Json::Value class definition has very good doxygen comments. So, it's better to use its header as a reference.
About your question - you can get data from any Json::Value object depending on its type. For example:
Json::Value root; // already parsed
for (Json::Value::const_iterator it = root.begin(); it != root.end(); ++it) {
const Json::Value &property = *it;
std::cout << "Property " << it.name() << std::endl;
switch (property.type()) {
case Json::intValue:
std::cout << "-> INT value: " << property.asInt64() << std::endl;
break;
case Json::stringValue:
std::cout << "-> STRING value: " << property.asString() << std::endl;
break;
case Json::arrayValue:
std::cout << "-> ARRAY value" << std::endl;
for (unsigned int i = 0; i < property.size(); ++i) {
const Json::Value &elem = property[i];
// Process Value again in the same way
}
break;
case Json::objectValue:
std::cout << "-> OBJECT value" << std::endl;
for (Json::Value::const_iterator propIt = property.begin(); propIt != property.end(); ++propIt) {
// Again print name of the object property and its value
}
break;
default:
// and other types processing
}
}
In order to create a JSON file you need to create a tree of Json::Value objects and then dump the root:
Json::Value rootNode(Json::objectValue);
rootNode["some_string"] = "string value here";
rootNode["some_int"] = 42;
Json::Value arrayNode(Json::arrayValue);
arrayNode[0] = 1;
arrayNode[1] = 3;
rootNode["some_array"] = arrayNode;
rootNode["another_array"] = Json::Value(Json::arrayValue);
rootNode["another_array"][0] = 17;
rootNode["some_object"]["object_property"] = "and its value";
std::ofstream outFile("output.json", std::ios_base::out | std::ios_base::trunc);
outFile << root << std::endl;
Json::Value root; // already parsed
But, how? How do I parse specific file into the root object?
But, how? How do I parse specific file into the root object?
// Let's take a file stream as an input example
std::ifstream inFile("input.json");
if (!inFile.is_open()) {
std::cerr << "Unable to open input JSON file: " << strerror(errno) << std::endl;
return;
}
// Configure stream reader/parser and process the input stream
Json::CharReaderBuilder builder;
Json::Value root;
std::string parseError;
builder["collectComments"] = false;
if (!Json::parseFromStream(builder, inFile, &root, &parseErr)) {
std::cerr << "Can't parse JSON: " << parseErr << std::endl;
return;
}
// Some `root` processing
for (Json::Value::const_iterator it = root.begin(); it != root.end(); ++it) {
// ...
}
Maybe @cdunn2001 can show the recommended way of the library usage.
If you don't mind the default read/write options, just use a stream.
Json::Value root;
std::ifstream istr("input.json");
istr >> root;
# modify root
# ...
cout << root;
For most users, that should be fine.
Why does the library sorts parameters by alphabet and not by the order, in which they were added to the object? https://i.imgur.com/NnlbKPj.png
Maybe we should use an OrderedDict backend. But that wasn't available when Baptiste first wrong jsoncpp, and changing it now could break some users.
That would be great, cause it's a bit hard to scroll through the file, while inspecting and editing parameters manually.
Notice how the parameter number 192 is at the line 14347 of a >700Kb configuration file: https://i.imgur.com/oiV48KV.png
Is there a way to get a number of the array values?
http://jsoncpp.sourceforge.net/class_json_1_1_value.html#4ca8ee6c48a34ca6c2f131956bab5e05
size()