我已将我的问题浓缩为下面的(也许不是最小的)示例应用程序。该示例是一个通用 JSON 解析器。但是,它表现出两个问题。1. 当其他选项都没有通过时,当 bool_ 是变体列表中的输出器时,它总是输出 true 或 false。如果它不是最后一个,那么它之后的任何内容都将被有效地使用。我不知道为什么。2. 当输入是字符串时,字符串处理程序永远不会从变体中触发。在变体之外使用时,它会触发。
示例代码将字符串输出简化为 karma::string 并且仍然显示错误。当我把这里学到的东西返回到真正的应用程序时,字符串输出将是一个 C 风格的转义字符串,所以只适用于 karma::string 的东西将无济于事。
我已经阅读(并重新阅读)使用 boost::spirit::karma和boost::spirit::karma 字符串输出的 boost::variant 类型的输出,并且不能将其正确应用于我的案例(即我毕竟不明白答案),否则它在更复杂的示例中不起作用。而且我也熟悉 mini_xml 示例代码。
关于我做错了什么有什么建议吗?为什么我做的是错的,而修复是对的?
非常感谢所有帮助。
#include <boost/variant/recursive_variant.hpp>
#include <string>
#include <vector>
namespace lloyd
{
namespace json
{
struct null
{
bool operator==(const null& cmp) {return true; }
};
struct element;
typedef boost::make_recursive_variant<null, bool, long, double, std::string, std::vector<element>, std::vector<boost::recursive_variant_> >::type value;
struct element
{
std::string name;
json::value value;
inline element(const element& src): name(src.name), value(src.value) {}
inline element(const std::string& name, const json::value& value): name(name), value(value) {}
inline element() {}
};
typedef std::vector<element> object;
}
}
#include <boost/fusion/adapted.hpp>
BOOST_FUSION_ADAPT_STRUCT(
lloyd::json::element,
(std::string, name)
(lloyd::json::value, value)
)
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_auto.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace lloyd { namespace json { namespace karma {
template <typename OutputIterator>
struct json_object_out_generator
: boost::spirit::karma::grammar<OutputIterator, json::object(bool, unsigned, unsigned) >
{
// JSON Output Grammars
boost::spirit::karma::rule<OutputIterator, std::vector<json::value>(bool, unsigned, unsigned) > array_rule;
boost::spirit::karma::rule<OutputIterator, json::null(bool, unsigned, unsigned) > null_rule;
boost::spirit::karma::rule<OutputIterator, json::value(bool, unsigned, unsigned) > value_rule;
boost::spirit::karma::rule<OutputIterator, json::element(bool, unsigned, unsigned) > elem_rule;
boost::spirit::karma::rule<OutputIterator, json::object(bool, unsigned, unsigned) > obj_rule;
json_object_out_generator() : json_object_out_generator::base_type(obj_rule)
{
using boost::spirit::lit;
using boost::spirit::_r1;
using boost::spirit::_r2;
using boost::spirit::_r3;
namespace karma=boost::spirit::karma;
null_rule %= karma::eps << boost::spirit::karma::lit("null");
array_rule %= lit("[") << -(value_rule(_r1, _r2, _r3) % lit(",") ) << "]";
value_rule %= ( null_rule(_r1, _r2, _r3) | karma::string | karma::long_ | karma::double_ | obj_rule(_r1, _r2, _r3) | array_rule(_r1, _r2, _r3) | karma::bool_);
elem_rule %= boost::spirit::karma::string << ":" << -value_rule(_r1, _r2+1, _r3);
obj_rule %= boost::spirit::lit("{")
<< -boost::spirit::buffer[( elem_rule(_r1, _r2+1, _r3) % ',' ) ]
<< "}";
}
};
}}}
#include <vector>
#include <sstream>
#include <iomanip>
#include <boost/assign/list_of.hpp>
#include <boost/assign/std/vector.hpp>
using namespace boost::assign;
int main(int argc, const char* argv[])
{
using lloyd::json::value;
using lloyd::json::element;
using lloyd::json::null;
lloyd::json::object obj;
lloyd::json::object sobj;
std::vector<value> t5;
t5 += null(), true, false, value("Testing"), sobj;
obj += element("T1", null()), element("T2", true), element("T3", false);
obj += element("T4", "Testing 4"), element("T5", t5), element("T6", sobj);
obj += element("NT0", (long)50), element("NT1", 50.5), element("NT2", 50.0);
std::stringstream s;
typedef boost::spirit::karma::ostream_iterator<char> out_itr;
out_itr so(s);
lloyd::json::karma::json_object_out_generator<out_itr> json_object_out; // Our grammar definition
boost::spirit::karma::generate(so, json_object_out(true, 0, 1), obj);
std::cout << "Result:\n";
std::cout << s.str() << std::endl;
return 0;
}
编辑以更改标题以涵盖显示的实际问题。编辑以修复代码示例上的上传缺失。