Created
October 10, 2015 07:42
-
-
Save felixdv/185dcb2b0f5035f8eeac to your computer and use it in GitHub Desktop.
Testing boost::property_tree with a json config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This fails | |
# See http://stackoverflow.com/questions/18552427/boost-read-json-and-c11 | |
g++ -std=c++11 ptree_test.cpp -o ptree_test | |
# This works | |
g++ ptree_test.cpp -o ptree_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"global": { | |
"server_name": "Main Housten Server", | |
"interfaces": [ | |
{ | |
"name": "st1_cam1", | |
"enabled": true, | |
"type": "ueye", | |
"description": "Camera Device 1 - HD Station Left Front Door Y555", | |
"config": { | |
"camera_id": 1, | |
"hardware_trigger": 1, | |
"param_file": "/usr/lib64/ueye/param_camera1.par" | |
} | |
}, | |
{ | |
"name": "st1_scan", | |
"enabled": true, | |
"type": "tcp_listener", | |
"description": "Barcode Scanner - Station Left Front Door Y555", | |
"config": { | |
"listen_addr": "192.168.201.200", | |
"listen_port": 6000, | |
"EOM_char": "\n" | |
} | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/property_tree/ptree.hpp> | |
#include <boost/property_tree/json_parser.hpp> | |
#include <boost/foreach.hpp> | |
#include <string> | |
#include <set> | |
#include <exception> | |
#include <iostream> | |
namespace pt = boost::property_tree; | |
int main(int argc, char* argv[]) | |
{ | |
pt::ptree tree; | |
pt::read_json("./config.json", tree); | |
std::cout << "Global settings: " << std::endl; | |
std::cout << "Server name: " + tree.get<std::string>("global.server_name") << std::endl; | |
BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("global.interfaces")) { | |
std::cout << "Interface found:" << std::endl; | |
std::cout << "============================" << std::endl; | |
BOOST_FOREACH(pt::ptree::value_type &k, v.second) { | |
if (k.first == "config") { | |
std::cout << "Settings:" << std::endl; | |
BOOST_FOREACH(pt::ptree::value_type &g, k.second) { | |
std::cout << "> " + g.first + ": " + g.second.data() << std::endl; | |
} | |
} else { | |
std::cout << k.first + ": " + k.second.data() << std::endl; | |
} | |
} | |
std::cout << std::endl << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment