Last active
August 29, 2015 14:05
-
-
Save longbuilder/53566f5f70eea2b94547 to your computer and use it in GitHub Desktop.
boost demos
This file contains hidden or 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 <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