我建议
或者.....你可以使用Boost Spirit
您可以使用 Karma 生成器来序列化 Block:
template <typename It>
struct generator : karma::grammar<It, Block(), karma::space_type>
{
generator() : generator::base_type(start)
{
using namespace karma;
start = numbers << bits << states << off;
numbers = lit("numbers") << '{' << -(int_ % ',') << '}';
bits = lit("bits") << '{' << -(bit % ',') << '}';
states = lit("states") << '{' << -(state % ',') << '}';
off = lit("off") << string_;
bit = int_ << ':' << char_;
state = string_ << ':' << string_;
string_ = '"' << *('\\' << char_('"') | char_) << '"';
}
// ...
};
这个,当像这样使用时:
static const generator<boost::spirit::ostream_iterator> gen;
std::ostringstream oss;
oss << karma::format_delimited(gen, karma::space,
Block {
{ 1, -42 },
{ { 3, '0' }, { 17, '1' } },
{ { "disabled", "true" }, { "locked", "false" }, { "ui", "manual" } },
"something called 'off'"
}) << "\n";
将单个打印Block
到oss
:
numbers { 1 , -42 } bits { 3 : 0 , 17 : 1 } states { "disabled" : "true" , "locked" : "false" , "ui" : "manual" } off "something called 'off'"
然后您可以使用镜像语法将其解析回来:
Block reparsed;
std::string const input = oss.str();
auto f(begin(input)), l(end(input));
assert(qi::phrase_parse(f,l,par,qi::space,reparsed));
查看完整程序:
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
struct Block
{
typedef std::vector<int> Numbers;
typedef std::map<int, char> Bits;
typedef std::map<std::string, std::string> States;
Numbers numbers;
Bits bits;
States states;
std::string off;
};
BOOST_FUSION_ADAPT_STRUCT(Block,
(Block::Numbers, numbers)
(Block::Bits, bits)
(Block::States, states)
(std::string, off))
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, Block(), Skipper>
{
parser() : parser::base_type(start)
{
using namespace qi;
start = numbers >> bits >> states >> off;
numbers = no_case ["numbers"] >> '{' >> -(int_ % ',') > '}';
bits = no_case ["bits" ] >> '{' >> -(bit % ',') > '}';
states = no_case ["states" ] >> '{' >> -(state % ',') > '}';
off = no_case ["off" ] >> string_;
bit = int_ >> ':' > char_;
state = string_ >> ':' > string_;
string_ = '"' >> *('\\' > char_ | ~char_('"')) > '"';
BOOST_SPIRIT_DEBUG_NODES((start)(numbers)(bits)(states)(off)(bit)(state)(string_));
}
private:
qi::rule<It, Block(), Skipper> start;
qi::rule<It, Block::Numbers(), Skipper> numbers;
qi::rule<It, Block::Bits(), Skipper> bits;
qi::rule<It, Block::States(), Skipper> states;
qi::rule<It, std::pair<int, char>(), Skipper> bit;
qi::rule<It, std::pair<std::string, std::string>(), Skipper> state;
qi::rule<It, std::string(), Skipper> off;
qi::rule<It, std::string()> string_;
};
template <typename It>
struct generator : karma::grammar<It, Block(), karma::space_type>
{
generator() : generator::base_type(start)
{
using namespace karma;
start = numbers << bits << states << off;
numbers = lit("numbers") << '{' << -(int_ % ',') << '}';
bits = lit("bits") << '{' << -(bit % ',') << '}';
states = lit("states") << '{' << -(state % ',') << '}';
off = lit("off") << string_;
bit = int_ << ':' << char_;
state = string_ << ':' << string_;
string_ = '"' << *('\\' << char_('"') | char_) << '"';
BOOST_SPIRIT_DEBUG_NODES((start)(numbers)(bits)(states)(off)(bit)(state)(string_));
}
private:
karma::rule<It, Block(), karma::space_type> start;
karma::rule<It, Block::Numbers(), karma::space_type> numbers;
karma::rule<It, Block::Bits(), karma::space_type> bits;
karma::rule<It, Block::States(), karma::space_type> states;
karma::rule<It, std::pair<int, char>(), karma::space_type> bit;
karma::rule<It, std::pair<std::string, std::string>(), karma::space_type> state;
karma::rule<It, std::string(), karma::space_type> off;
karma::rule<It, std::string()> string_;
};
int main()
{
typedef std::string::const_iterator It;
static const parser<It, qi::space_type> par;
static const generator<boost::spirit::ostream_iterator> gen;
Block reparsed;
{
std::ostringstream oss;
oss << karma::format_delimited(gen, karma::space,
Block {
{ 1, -42 },
{ { 3, '0' }, { 17, '1' } },
{ { "disabled", "true" }, { "locked", "false" }, { "ui", "manual" } },
"something called 'off'"
}) << "\n";
std::string const input = oss.str();
auto f(begin(input)), l(end(input));
assert(qi::phrase_parse(f,l,par,qi::space,reparsed));
// serialize and deserialize the whopping 3D collection of Blocks:
std::cout << karma::format_delimited(gen, karma::space, reparsed) << "\n";
}
}