I am know approaching to boost property tree and saw that it is a good feature of boost libs for c++ programming.
Well, I have one doubt? how to iterate a property tree using iterators or similar?
In reference there is just an example of browsing the tree through:
BOOST_FOREACH
But is there nothing more? Something like an stl-like container? It would be a better solution, speaking about code quality....
Here is what I came up with after much experimentation. I wanted to share it in the community because I couldn't find what I wanted. Everybody seemed to just post the answer from the boost docs, which I found to be insufficient. Anyhow:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <string>
#include <iostream>
using namespace std;
using boost::property_tree::ptree;
string indent(int level) {
string s;
for (int i=0; i<level; i++) s += " ";
return s;
}
void printTree (ptree &pt, int level) {
if (pt.empty()) {
cerr << "\""<< pt.data()<< "\"";
}
else {
if (level) cerr << endl;
cerr << indent(level) << "{" << endl;
for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
cerr << indent(level+1) << "\"" << pos->first << "\": ";
printTree(pos->second, level + 1);
++pos;
if (pos != pt.end()) {
cerr << ",";
}
cerr << endl;
}
cerr << indent(level) << " }";
}
return;
}
int main(int, char*[]) {
// first, make a json file:
string tagfile = "testing2.pt";
ptree pt1;
pt1.put("object1.type","ASCII");
pt1.put("object2.type","INT64");
pt1.put("object3.type","DOUBLE");
pt1.put("object1.value","one");
pt1.put("object2.value","2");
pt1.put("object3.value","3.0");
write_json(tagfile, pt1);
ptree pt;
bool success = true;
try {
read_json(tagfile, pt);
printTree(pt, 0);
cerr << endl;
}catch(const json_parser_error &jpe){
//do error handling
success = false
}
return success;
}
Here is the output:
rcook#rzbeast (blockbuster): a.out
{
"object1":
{
"type": "ASCII",
"value": "one"
},
"object2":
{
"type": "INT64",
"value": "2"
},
"object3":
{
"type": "DOUBLE",
"value": "3.0"
}
}
rcook#rzbeast (blockbuster): cat testing2.pt
{
"object1":
{
"type": "ASCII",
"value": "one"
},
"object2":
{
"type": "INT64",
"value": "2"
},
"object3":
{
"type": "DOUBLE",
"value": "3.0"
}
}
BOOST_FOREACH is just a convenient way for iterating that can be done by iterator, begin() and end()
Your_tree_type::const_iterator end = tree.end();
for (your_tree_type::const_iterator it = tree.begin(); it != end; ++it)
...
And since C++11 it's:
for (auto& it: tree)
...
I ran into this issue recently and found the answers incomplete for my need, so I came up with this short and sweet snippet:
using boost::property_tree::ptree;
void parse_tree(const ptree& pt, std::string key)
{
std::string nkey;
if (!key.empty())
{
// The full-key/value pair for this node is
// key / pt.data()
// So do with it what you need
nkey = key + "."; // More work is involved if you use a different path separator
}
ptree::const_iterator end = pt.end();
for (ptree::const_iterator it = pt.begin(); it != end; ++it)
{
parse_tree(it->second, nkey + it->first);
}
}
Important to note is that any node, except the root node can contain data as well as child nodes. The if (!key.empty()) bit will get the data for all but the root node, we can also start building the path for the looping of the node's children if any.
You'd start the parsing by calling parse_tree(root_node, "") and of course you need to do something inside this function to make it worth doing.
If you are doing some parsing where you don't need the FULL path, simply remove the nkey variable and it's operations, and just pass it->first to the recursive function.
An addition to the answer How to iterate a boost property tree? :
In the C++11 style range based for for (auto node : tree), each node is a std::pair<key_type, property_tree>
Whereas in the manually written iteration
Your_tree_type::const_iterator end = tree.end();
for (your_tree_type::const_iterator it = tree.begin(); it != end; ++it)
...
the iterator it is a pointer to such a pair. It's a tiny difference in usage. For example, to access the key, one would write it->first but node.first.
Posted as a new answer, because my proposed edit to the original answer was rejected with the suggestion to post a new answer.
BFS based print ptree traversal, May be used if we want to do some algorithmic manipulation
int print_ptree_bfs(ptree &tree) {
try {
std::queue<ptree*> treeQ;
std::queue<string> strQ;
ptree* temp;
if (tree.empty())
cout << "\"" << tree.data() << "\"";
treeQ.push(&tree);
//cout << tree.data();
strQ.push(tree.data());
while (!treeQ.empty()) {
temp = treeQ.front();
treeQ.pop();
if (temp == NULL) {
cout << "Some thing is wrong" << std::endl;
break;
}
cout << "----- " << strQ.front() << "----- " << std::endl;
strQ.pop();
for (auto itr = temp->begin(); itr != temp->end(); itr++) {
if (!itr->second.empty()) {
//cout << itr->first << std::endl;
treeQ.push(&itr->second);
strQ.push(itr->first);
} else {
cout<<itr->first << " " << itr->second.data() << std::endl;
}
}
cout << std::endl;
}
} catch (std::exception const& ex) {
cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
Related
I am using the Boost Property Tree for a Project and came across a problem. I'm using it like this:
using Namespace boost::property_tree;
ptree proot;
int myInt = 5;
proot.put("Number", myInt);
write_json("myjson.json", proot);
If I use it like this, the data type that is safed is a string, not an int. An example of what i mean:
{ "Number": "5" } //what i get
{ "Number": 5 } //what i want
Is there a way to change that?
No you cannot change this behavior, as the string value type is pretty muched baked into boost::property_tree. While you could technically use different template type parameters than the default ones, you loose much of the conversion logic that went into that library.
As a somewhat wanky alternative, consider the following.
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace boost::property_tree;
using boost::property_tree::json_parser::create_escapes;
void writeJsonValue(std::ostream& stream, const ptree& pt)
{
const auto raw = pt.get_value<std::string>();
if (raw == "true" || raw == "false") {
stream << raw;
return;
}
if (const auto integral = pt.get_value_optional<int>())
stream << *integral;
else
stream << '"' << create_escapes(raw) << '"';
}
This essentially reverts some predefined loss of type information. You can use this within an modified version of Boost's json output function:
void writeJson(std::ostream& stream, const ptree& pt, int indent = 0)
{
static const auto indentStr = [](int level) { return std::string(4 * level, ' '); };
if (indent > 0 && pt.empty())
writeJsonValue(stream, pt);
else if (indent > 0 && pt.count(std::string()) == pt.size()) {
stream << "[\n";
for (auto it = pt.begin(); it != pt.end(); ++it) {
stream << indentStr(indent + 1);
writeJson(stream, it->second, indent + 1);
if (boost::next(it) != pt.end())
stream << ',';
stream << '\n';
}
stream << indentStr(indent) << ']';
} else {
stream << "{\n";
for (auto it = pt.begin(); it != pt.end(); ++it) {
stream << indentStr(indent + 1);
stream << '"' << create_escapes(it->first) << "\": ";
writeJson(stream, it->second, indent + 1);
if (boost::next(it) != pt.end())
stream << ',';
stream << '\n';
}
stream << indentStr(indent) << '}';
}
}
Call it for your data e.g. as
writeJson(std::cout, proot);
and the output should be
{
"Number": 5
}
I successfully tried similar things and they worked well without any hacks, see my approach here:
Why does Boost property tree write_json save everything as string? Is it possible to change that?
It requires some effort (you have to provide an own JSON-writer and to specialize some parts of the reader) but it's worth the effort in general since you will also observe a performance gain for a lot of scenarios.
I'm new to c++ after learning basic Object Oriented Programming in Java so I'm having a difficult time grasping memory deallocation. The assignment was to create a Weighted Directed Graph...
I'm getting the error: "terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc" when I run certain inputs through my code, and I'm having a difficult time figuring out what is causing it.
I googled the error and found that it was a memory problem, so I attempted to go through my code and try to find any leaks, but I am not sure where they are. Most posts are talking about pointers, which I do not tend to implement because I am unfamiliar with them. Thank you for your time!
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#include <iterator>
#include <map>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
class WDGraph {
private:
map<string,map<string,int>> edges;
vector<string> verts;
list<string> leaves;
list<string> roots;
list<string> selfEdges;
public:
list<string> getRoots() { return roots; }
list<string> getLeaves() { return leaves; }
void addVert(string key) {
verts.push_back(key);
}
void link(string start, string dest, int cost) {
edges[start].insert(make_pair(dest,cost));
if (!containsLeaf(dest) && !containsVert(dest))
leaves.push_back(dest);
if (!containsRoot(start) && !containsVert(start))
roots.push_back(start);
if (start == dest)
selfEdges.push_back(start);
roots.remove(dest);
leaves.remove(start);
}
bool containsVert(string key) {
for (int i=0; i < verts.size(); i++) {
if (key == verts[i]) {
return true;
}
}
return false;
}
bool containsRoot(string key) {
bool found = (find(roots.begin(), roots.end(), key) != roots.end());
return found;
}
bool containsLeaf(string key) {
bool found = (find(leaves.begin(), leaves.end(), key) != leaves.end());
return found;
}
WDGraph() { }
void printWDG() {
cout << "Printing Weighted Directed Graph." << endl;
for (auto itr1 = edges.begin(); itr1 != edges.end(); ++itr1) {
for (auto itr2 = itr1->second.begin(); itr2 != itr1->second.end(); ++itr2) {
if (itr2->first == "null" && containsRoot(itr1->first)) {
cout << "[" << itr1->first << "]";
}
else if (itr2->first != "null")
cout << "[" << itr1->first << " -> ";
cout << itr2->first << ", " << itr2->second << "] ";
}
cout << "" << endl;
}
}
void printNumVerts() {
cout << "Total number of vertices: " << verts.size() << endl;
}
void printRoots() {
int num_roots = 0;
cout << "Vertices with zero inbound edges: " << endl;
for (auto itr = roots.begin(); itr != roots.end(); ++itr) {
cout << "[" << *itr << "]" << endl;
num_roots++;
}
if (num_roots == 0) cout << "None" << endl;
}
void printLeaves() {
int num_leaves = 0;
cout << "Vertices with zero outbound edges:" << endl;
for (auto itr = leaves.begin(); itr != leaves.end(); ++itr) {
if (*itr != "null")
cout << "[" << *itr << "]" << endl;
num_leaves++;
}
if (num_leaves == 0) cout << "None" << endl;
}
void printSelfEdges() {
cout << "Vertices with self edges:" << endl;
for (auto itr = selfEdges.begin(); itr != selfEdges.end(); ++itr) {
cout << "[" << *itr << "]" << endl;
}
}
};
int main() {
WDGraph myWDG;
string filePath;
string line;
int weight;
size_t commaPos;
vector<string> sVector;
ifstream dataFile;
// cout << "Please enter the relative path to an input file." << endl;
// getline (cin, filePath);
// cout << "The file path you entered was " << filePath << endl;
// dataFile.open(filePath);
dataFile.open("input.csv"); //test input
while (getline (dataFile, line)) {
commaPos = line.find(',');
//Parse input file into string vector
while (line.length() >= 1) {
if (line.length() == 1) {
sVector.push_back(line);
break;
}
sVector.push_back(line.substr(0,commaPos));
line = line.substr(commaPos+1);
commaPos = line.find(',');
}
//Create vertices depending on number of parameters
if (sVector.size() == 1) {
if (!myWDG.containsVert(sVector[0])) {
myWDG.addVert(sVector[0]);\
}
myWDG.link(sVector[0], "null", 0);
}
if (sVector.size() == 3) {
if (!myWDG.containsVert(sVector[0])) {
myWDG.addVert(sVector[0]);
}
if (!myWDG.containsVert(sVector[1])) {
myWDG.addVert(sVector[1]);
}
weight = stoi(sVector[2]);
myWDG.link(sVector[0], sVector[1], weight);
}
sVector.clear();
}
myWDG.printWDG();
myWDG.printNumVerts();
myWDG.printRoots();
myWDG.printLeaves();
myWDG.printSelfEdges();
}
When my .csv has simple stuff it works as expected, for example:
a,b,1
c,d,2
e
f,f,3
However, if I have stuff like this I get the error "terminate called after throwing an instance of 'std::bad_alloc':
Hello
World,Hello,3
My,Name,4
Is
Nikki,Hello,3
As mentioned by Z E Nir, your line parsing code fails to consume any input if there is no comma "," in the line. You can of course debug your line parsing code, as debugging is a valuable skill to develop anyway.
However, a possible alternative to debugging consists in finding an existing C++ language construct that does what you want to do, and is part of the language library so it is already debugged.
Quite often, what you want to do is "common stuff", so debugging manual code will take more time than finding the appropriate pre-existing language construct, courtesy of your favorite internet search engine and/or stackoverflow itself. And being able to quickly find the language construct is also a very valuable skill.
In your case, function getline() takes an optional delimiter, which is a newline by default, but you can instead have "," as delimiter and so use getline() again, but to parse a single line. It just takes a string object pretending to be a file stream, that is an std::istringstream object.
So you end up with two nested loops, both using getline():
#include <sstream>
while (getline (dataFile, line)) {
std::istringstream iss{line};
std::string token;
while (getline (iss, token, ',')) {
std::cout << "DEBUG TOKEN LEN=" << token.length() << std::endl;
sVector.push_back(token);
}
// go build myWDG
}
That way, you don't have to mess up with lowly details such as the value of your commaPos variable. And the resulting code is easier to understand for another programmer.
Welcome to Stack Overflow.
Heads up: Sorry for the style, but you really have to learn solving those kind of problem on your own. It's called debugging. I'm experienced programmer and yet, my code never run exactly as I thought it will when testing it in the first time. You need to learn how to use a debugger like gdb or the built in debugger in the Visual C++ environment.
Now about your question:
The following code received the variable line with value Hello. There is no , character in line hence line = line.substr(commaPos + 1); return Hello all the time, and since 'Hello' string holds more then one character, you stuck in an infinte loop.
//Parse input file into string vector
while (line.length() >= 1) {
if (line.length() == 1) {
sVector.push_back(line);
break;
}
sVector.push_back(line.substr(0, commaPos));
line = line.substr(commaPos + 1);
commaPos = line.find(',');
}
The problem isn't stop there. Since each iteration over the infinite loop your program executing: sVector.push_back(line.substr(0, commaPos)); you actually allocates more and more memory, till you system won't give any more to this process. That's where you get the bad_alloc exception.
In other words, your error is not about C++, but about poor programing.
Reconsider your program, and think how you want to handle edge-cases like the Hello.
Oh, and never build objects on the stack. I know some places claim its OK to do this in the main function, but belive me its causing a lot of troubles.
I want to write a parameter server in C++ where I can recursively dump a tree of parameters into a property tree and then write it to a JSON file.
The dump function looks like this:
void Params::dump(string filename) {
// Create a root
pt::ptree root;
// Fill the root with parameters
mapToPt(curParams, root);
// Write to cout
pt::write_json(cout, root);
}
mapToPt is supposed to recursively go through the hierarchy of my parameter server and fill the property tree while doing so:
void Params::mapToPt(boost::shared_ptr<Params> curParams, pt::ptree &root) {
// Fill current root with parameters from curParams ParameterMap
map<string, boost::shared_ptr<Param> >::iterator it;
for ( it = curParams->getParamMap().begin(); it != curParams-getParamMap().end(); it++ ) {
root.put(it->first, it->second->getValue());
cout << "Add Parameter: \n";
cout << "Parameter name: " << it->first << "\n";
cout << "Parameter value: " << it->second->getValue() << "\n";
}
// Recursively go through all children to do the same to them
if(curParams->hasChildren()) { //ERROR LINE
map<string, boost::shared_ptr<Params> >::iterator it;
for ( it = curParams->getChildren().begin(); it != curParams-getChildren().end(); it++ ) {
pt::ptree new_tree;
root.add_child(it->second->getName(), new_tree);
cout << "Add Child: \n";
cout << "Child name: " << it->second->getName() << "\n";
mapToPt(it->second, new_tree);
}
}
}
My problem is that as soon as I go into recursion, errors occur at random lines that can not be the cause of the error. "basic_string::_M_construct null not valid" is the error message. I believe that I might access deleted content and that it might be due to the way I iterate through the property tree children.
Is my way of doing that wrong or is there another way to do it?
Thank you.
Why is mapToPt a member when it also expects a pointer to a Params instance?
Anyhoops, there's quite a bit of confusion.
At a design level, your Params type looks like it cannot decide whether it's a leaf node or not. Moreover, it suffers from "Quasi Classes" design, where getters essentially guarantee that there is no class invariant possible. In such cases, prefer to just have a struct with member fields.
Note, if you fail to return by reference from getParamMap() and getChildren() then you already have Undefined Behaviour in both loops, because the iterators then point into non-existent copies of containers.
You should check this. Also, see my working demo below
At the implementation level, this is causing you problems:
pt::ptree new_tree;
root.add_child(it->second->getName(), new_tree);
add_child inserts a copy of new_tree. Any future modification to new_tree has no effect. Instead, write:
pt::ptree& new_tree = root.add_child(it->second->getName(), {});
Here, new_tree becomes a reference to the actually added tree.
Attempted Fix
The style is still below my expectations. Personally I'd closely review the use of shared_ptr at all in this piece of code.
But this will probably help you along:
Live On Coliru
#include <boost/make_shared.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
#include <map>
namespace pt = boost::property_tree;
struct Param {
std::string getValue() const { return "42"; }
};
struct Params {
using ParamMap = std::map<std::string, boost::shared_ptr<Param> >;
using Children = std::map<std::string, boost::shared_ptr<Params> >;
Params(std::string name = "") : _name(name) {}
std::string getName() const { return _name; }
ParamMap& getParamMap() { return _map; }
ParamMap const& getParamMap() const { return _map; }
bool hasChildren() const { return !_children.empty(); }
Children& getChildren() { return _children; }
Children const& getChildren() const { return _children; }
static void mapToPt(boost::shared_ptr<Params> curParams, pt::ptree &root);
private:
std::string _name;
ParamMap _map;
Children _children;
};
void Params::mapToPt(boost::shared_ptr<Params> curParams, pt::ptree &root) {
// Fill current root with parameters from curParams ParameterMap
std::map<std::string, boost::shared_ptr<Param> >::iterator it;
for (it = curParams->getParamMap().begin(); it != curParams->getParamMap().end(); it++) {
root.put(it->first, it->second->getValue());
//std::cout << "Add Parameter: \n";
//std::cout << "Parameter name: " << it->first << "\n";
//std::cout << "Parameter value: " << it->second->getValue() << "\n";
}
// Recursively go through all children to do the same to them
if (curParams->hasChildren()) {
for (auto it = curParams->getChildren().begin(); it != curParams->getChildren().end(); it++) {
pt::ptree& new_tree = root.add_child(it->second->getName(), {});
//std::cout << "Add Child: \n";
//std::cout << "Child name: " << it->second->getName() << "\n";
mapToPt(it->second, new_tree);
}
}
}
int main() {
auto a = boost::make_shared<Params>("rootparams");
a->getParamMap().emplace("one", boost::make_shared<Param>());
a->getParamMap().emplace("two", boost::make_shared<Param>());
a->getParamMap().emplace("three", boost::make_shared<Param>());
a->getChildren().emplace("child1", boost::make_shared<Params>("child1-name"))
.first->second->getParamMap().emplace("four", boost::make_shared<Param>());
a->getChildren().emplace("child2", boost::make_shared<Params>("child2-name"))
.first->second->getParamMap().emplace("five", boost::make_shared<Param>());
pt::ptree root;
a->mapToPt(a, root);
write_json(std::cout, root);
}
Prints
{
"one": "42",
"three": "42",
"two": "42",
"child1-name": {
"four": "42"
},
"child2-name": {
"five": "42"
}
}
I have a small but complex tree structure. Using, boost property tree as a container i am trying to iterate through the tree and subsequently emit it to the yaml file using yaml-cpp library.
For instance, i have a small nested property tree:
fibonacci:
type: series
entities:
golden_ratio:
ratio: 2.3
function:
power_series: 2
I want my yaml file to look exactly like this.
I wrote a recursive function to iterate through the tree and emit to yaml.
//Member variable
YAML::Emitter m_out
void iterator(const boost::property_tree::ptree& tree, const std::string& key)
{
for (const auto& item: tree)
{
if (item.second.data().empty()) //check if map node
{
m_out << YAML::BeginMap;
m_out << YAML::Key << item.first;
}
else if (!item.second.data().empty()) //else it is key/value pair
{
m_out << YAML::Key << item.first;
m_out << YAML::Value << item.second.data();
}
if (!item.second.empty()) //If the node has child
{
iterator(item.second, item.first);
}
}
}
I am calling the function with a emtpy key as iterator(root, ""). I know that the property tree works as key/value pairs, whereas, Yaml-cpp has node designations. In the code i am just trying to assume type of tree node based on value (no value - Map node, else - key/value node)
Apparently, my emitted yaml file doesn't possess the desired tree structure as presented above since my logic is wrong. I would like to make a recursive function which can iterate through any kind of tree and emit it to yaml file.
Is it possible to iterate tree and subsequently emit to yaml recursively? If yes, i would appreciate some ideas.
So I took your desired YAML and put it through an online converter to get a "reliable" ptree representation (which you interestingly left out of the question).
Then I proceeded to do a simple ptree roundtrip for sanity checks:
Live On Coliru
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
std::istringstream sample_json();
ptree sample_ptree();
int main() {
write_json(std::cout, sample_ptree());
}
std::istringstream sample_json() {
return std::istringstream(R"({
"fibonacci": {
"type": "series",
"entities": {
"golden_ratio": {
"ratio": 2.3
},
"function": {
"power_series": 2
}
}
}
})");
}
ptree sample_ptree() {
ptree pt;
{
auto stream = sample_json();
read_json(stream, pt);
}
return pt;
}
Prints
{
"fibonacci": {
"type": "series",
"entities": {
"golden_ratio": {
"ratio": "2.3"
},
"function": {
"power_series": "2"
}
}
}
}
to_yaml take #1
The simplest is, of course, to read the same JSON, and let yaml-cpp do the conversion:
auto stream = sample_json();
std::cout << YAML::Load(stream) << "\n";
Prints:
{fibonacci: {type: series, entities: {golden_ratio: {ratio: 2.3}, function: {power_series: 2}}}}
to_yaml take #2: pretty print
First off
naming is important. iterator doesn't describe the function, and clashes with the well-known concept from the standard library
the key argument is unused
you only ever BeginMap, how are you expecting a valid tree if you don't have EndMap anywhere in the code?
No global variables please. They make your code brittle (non-deterministic, non-idempotent, non-reentrant, not threadsafe etc.). Just pass that Emitter& as a parameter.
I'd make it MUCH simpler:
void to_yaml(ptree const& node, YAML::Emitter &m_out) {
if (node.empty()) {
m_out << YAML::Value << node.data();
} else {
m_out << YAML::BeginMap;
for (auto const&item : node) {
m_out << YAML::Key << item.first;
to_yaml(item.second, m_out);
}
m_out << YAML::EndMap;
}
}
Now, to have a convenient entry point, add an overload:
std::string to_yaml(ptree const& tree) {
YAML::Emitter out;
to_yaml(tree, out);
return out.c_str();
}
Now you can print the result by doing:
std::cout << to_yaml(sample_ptree()) << "\n";
Prints:
fibonacci:
type: series
entities:
golden_ratio:
ratio: 2.3
function:
power_series: 2
Full Listing
#include <iostream>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;
std::istringstream sample_json();
ptree sample_ptree();
#include "yaml-cpp/yaml.h"
void to_yaml(ptree const& node, YAML::Emitter &m_out) {
if (node.empty()) {
m_out << YAML::Value << node.data();
} else {
m_out << YAML::BeginMap;
for (auto const&item : node) {
m_out << YAML::Key << item.first;
to_yaml(item.second, m_out);
}
m_out << YAML::EndMap;
}
}
std::string to_yaml(ptree const& tree) {
YAML::Emitter out;
to_yaml(tree, out);
return out.c_str();
}
int main() {
write_json(std::cout, sample_ptree());
{
auto stream = sample_json();
std::cout << YAML::Load(stream) << "\n";
}
std::cout << to_yaml(sample_ptree()) << "\n";
}
std::istringstream sample_json() {
return std::istringstream(R"({
"fibonacci": {
"type": "series",
"entities": {
"golden_ratio": {
"ratio": 2.3
},
"function": {
"power_series": 2
}
}
}
})");
}
ptree sample_ptree() {
ptree pt;
{
auto stream = sample_json();
read_json(stream, pt);
}
return pt;
}
I am trying to get the array from my JSON Stinrg defined in the main function. I have used libjson API for this, simple key value is easy to get so I am able to get the value of RootA but how about this array in ChildA. Please let me know
#include <iostream>
#include <libjson/libjson.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char rootA[20];
int childB;
int *childInt;
void ParseJSON(JSONNODE *n) {
if (n == NULL) {
printf("Invalid JSON Node\n");
return;
}
JSONNODE_ITERATOR i = json_begin(n);
while (i != json_end(n)) {
if (*i == NULL) {
printf("Invalid JSON Node\n");
return;
}
// recursively call ourselves to dig deeper into the tree
if (json_type(*i) == JSON_ARRAY || json_type(*i) == JSON_NODE) {
ParseJSON(*i);
}
// get the node name and value as a string
json_char *node_name = json_name(*i);
// find out where to store the values
if (strcmp(node_name, "RootA") == 0) {
json_char *node_value = json_as_string(*i);
strcpy(rootA, node_value);
cout << rootA<<"\n";
json_free(node_value);
} else if (strcmp(node_name, "ChildA") == 0) {
JSONNODE *node_value = json_as_array(*i);
childInt=reinterpret_cast<int *>(&node_value);
cout << childInt[0]<<"\n";
cout << childInt[1]<<"\n";
json_free(node_value);
} else if (strcmp(node_name, "ChildB") == 0) {
childB = json_as_int(*i);
cout << childB;
}
// cleanup and increment the iterator
json_free(node_name);
++i;
}
}
int main(int argc, char **argv) {
char
*json =
"{\"RootA\":\"Value in parent node\",\"ChildNode\":{\"ChildA\":[1,2],\"ChildB\":42}}";
JSONNODE *n = json_parse(json);
ParseJSON(n);
json_delete(n);
return 0;
}
Thanks not-sehe but I got the solution for this
Ok I got it... treat array as a node and iterate over it again as if its a value with blank key. You can see the code part which did it..
if (json_type(*i) == JSON_ARRAY) {
cout << "\n Its a Json Array";
JSONNODE *arrayValue = json_as_array(*i);
JSONNODE_ITERATOR i1 = json_begin(arrayValue);
while (i1 != json_end(arrayValue)) {
cout << "\n In Array Loop ";
cout << json_as_int(*i1);
++i1;
}
}
This is probably not the answer you were looking for, but let me just demonstrate that a library with a slightly more modern interface makes this a lot easier (test.cpp):
#include <sstream>
#include "JSON.hpp"
int main()
{
auto document = JSON::readFrom(std::istringstream(
"{\"RootA\":\"Value in parent node\",\"ChildNode\":{\"ChildA\":[1,2],\"ChildB\":42}}"));
auto childA = as_object(
as_object(document)[L"ChildNode"]
)[L"ChildA"];
std::cout << childA << std::endl;
}
Which prints
[1,2]
It's using my own minimalist implementation of the rfc4627 specs. It's minimalist in interface only, supporting the full syntax and UNICODE.
The API interface is quite limited, but you can already see that working without C-style pointers, with proper dictionary lookups, key comparisons etc. makes it a less tedious and error prone:
// or use each value
for(auto& value : as_array(childA).values)
std::cout << value << std::endl;
// more advanced:
JSON::Value expected = JSON::Object {
{ L"RootA", L"Value in parent node" },
{ L"ChildNode", JSON::Object {
{ L"ChildA", JSON::Array { 1,2 } },
{ L"ChildB", 42 },
} },
};
std::cout << "Check equality: " << std::boolalpha << (document == expected) << std::endl;
std::cout << "Serialized: " << document;
See the full parser implementation (note: it includes serialization too) at github: https://github.com/sehe/spirit-v2-json/tree/q17064905