Skip to content

Instantly share code, notes, and snippets.

@longbuilder
Last active August 29, 2015 14:05
Show Gist options
  • Save longbuilder/53566f5f70eea2b94547 to your computer and use it in GitHub Desktop.
Save longbuilder/53566f5f70eea2b94547 to your computer and use it in GitHub Desktop.
boost demos
#include <sstream>
#include <string>
#include <boost/property_tree/json_parser.hpp>
int parseJson(const std::string &s)
{
using boost::property_tree::ptree;
ptree pt;
std::stringstream ss(s);
try{
read_json(ss, pt);
int uid = pt.get<int>("uid");
std::string name = pt.get<std::string>("name");
}
catch(std::exception &e)
{
return -1;
}
return 0;
}
int writeJson(const std::string &s)
{
using boost::property_tree::ptree;
ptree pt;
std::stringstream ss;
try{
pt.put<int>("uid", 123);
pt.put<std::string>("name", "Tom");
boost::property_tree::write_json(ss, pt);
s = ss.str();
}
catch(std::exception &e)
{
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment