std::stringstream ss;
ss << "{ \"values\": \"A\": 1, \"B\": 10 }";
I'd like to make this stream into this format below.
{
"values": [
{ "A": 1, "B": 10 }
...
]
}
Does anybody know how to parse the values of array using c++ and boost ptree?
解决方案
Assume input input.json
Using Boost Spirit V2.x
Here's a simplistic method using Boost Spirit Qi:
#include
#include
#include
#include
#include
namespace {
using Element = std::map<:string int>;
struct Array : std::vector { };
std::istream& operator>>(std::istream& is, Array& into) {
using namespace boost::spirit::qi;
using it = boost::spirit::istream_iterator;
rule s;
rule r, e;
s = '"' >> ~char_('"') >> '"';
r = (s >> ':' >> int_) % ',';
e = '{' >> r >> '}';
return is >> phrase_match('{'
>> lit("\"values\"") >> ':' >> '[' >> (e % ',') >> ']'
>> '}', space, into);
}
}
int main() {
std::ifstream ifs("input.json");
ifs.unsetf(std::ios::skipws);
Array array;
if (ifs >> array) {
std::cout << "Parsed " << array.size() << " elements:\n";
for (auto& e : array) {
std::cout << "\n--------------------\n{ ";
for (auto& kv : e)
std::cout << "\"" << kv.first << "\": " << kv.second << ", ";
std::cout << " }\n";
}
} else {
std::cout << "Parsing failed\n";
}
}
Prints
std::istream& {anonymous}::operator>>(std::istream&, {anonymous}::Array&)
Parsed 13 elements:
--------------------
{ "A": 1, "B": 10, }
--------------------
{ "C": 3, "D": 12, }
--------------------
{ "E": 5, "F": 14, }
--------------------
{ "G": 7, "H": 16, }
--------------------
{ "I": 9, "J": 18, }
--------------------
{ "K": 11, "L": 20, }
--------------------
{ "M": 13, "N": 22, }
--------------------
{ "O": 15, "P": 24, }
--------------------
{ "Q": 17, "R": 26, }
--------------------
{ "S": 19, "T": 28, }
--------------------
{ "U": 21, "V": 30, }
--------------------
{ "W": 23, "X": 32, }
--------------------
{ "Y": 25, "Z": 34, }
Using Spirit X3
Same deal:
namespace {
using Element = std::map<:string int>;
struct Array : std::vector { };
namespace parser {
using namespace boost::spirit::x3;
rule s;
rule r;
rule e;
auto s_def = '"' >> ~char_('"') >> '"';
auto r_def = (s >> ':' >> int_) % ',';
auto e_def = '{' >> r >> '}';
BOOST_SPIRIT_DEFINE(s,r,e)
}
std::istream& operator>>(std::istream& is, Array& into) {
using namespace parser;
boost::spirit::istream_iterator f(is), l;
if (!phrase_parse(f, l, '{'
>> lit("\"values\"") >> ':' >> '[' >> (e % ',') >> ']'
>> '}', space, into))
{
is.setstate(is.rdstate() | std::ios::failbit);
}
return is;
}
}
Same output with same main()
Using Property Tree
This is somewhat difference, I opted to not implement operator>> because Boost Property doesn't really afford that.
#include
#include
#include
#include
namespace {
using Element = std::map<:string int>;
struct Array : std::vector { };
Array read(std::string fname) {
std::ifstream ifs(fname);
Array into;
using namespace boost::property_tree;
ptree pt;
read_json(ifs, pt);
for (auto& entry : pt.get_child("values")) {
Element e;
for (auto& property : entry.second)
e[property.first] = property.second.get_value(0);
into.push_back(std::move(e));
}
return into;
}
}
int main() {
try {
auto array = read("input.json");
std::cout << "Parsed " << array.size() << " elements:\n";
for (auto& e : array) {
std::cout << "--------------------\n{ ";
for (auto& kv : e)
std::cout << "\"" << kv.first << "\": " << kv.second << ", ";
std::cout << " }\n";
}
} catch (...) {
std::cout << "Parsing failed\n";
}
}
Of course, the output is, again, the same as earlier.