#include #include #include #include #include #include #include namespace json { namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; struct nullptr_t_ : qi::symbols< char, void * > { nullptr_t_() { add( "null", NULL ); } } nullptr_; typedef std::map map_any; typedef std::vector vector_any; typedef std::pair pair_any; template< typename Iterator > struct Grammar : qi::grammar< Iterator, boost::any(), ascii::space_type > { Grammar(): Grammar::base_type( start ) { using qi::lexeme; using qi::double_; using qi::bool_; using ascii::char_; start = value_rule.alias(); object_rule = '{' >> pair_rule % ',' >> '}'; pair_rule = string_rule >> ':' >> value_rule; value_rule = object_rule | array_rule | string_rule | nullptr_ | double_ | bool_; array_rule = '[' >> value_rule % ',' >> ']'; string_rule = lexeme[ '\"' >> *( char_ - '\"' ) >> '\"' ]; } qi::rule start; qi::rule object_rule; qi::rule pair_rule; qi::rule value_rule; qi::rule array_rule; qi::rule string_rule; }; } int main(int argc, char** argv) { //"{\"name\":10}" const std::string source(argv[1]); json::Grammar< std::string::const_iterator > g; boost::any v; json::map_any ma; json::vector_any va; json::pair_any pa; std::string::const_iterator bit = source.begin(); std::string::const_iterator eit = source.end(); bool r = boost::spirit::qi::phrase_parse( bit, eit, g, boost::spirit::ascii::space, v ); if( r ) { std::cout << BOOST_LIB_VERSION << std::endl; std::cout << v.type().name() << std::endl; std::cout << typeid(ma).name() << std::endl; std::cout << typeid(va).name() << std::endl; std::cout << typeid(pa).name() << std::endl; return 0; std::vector< boost::any> a = boost::any_cast< std::vector< boost::any> >( v ); for( std::vector< boost::any>::iterator it = a.begin(); it != a.end(); ++it ) { if(it->type() == typeid(char*)) { std::cout << boost::any_cast< char*>( *it ) << std::endl; } else if(it->type() == typeid(const char*)) { std::cout << boost::any_cast< const char*>( *it ) << std::endl; } else if(it->type() == typeid(std::string)) { std::cout << boost::any_cast< std::string>( *it ) << std::endl; } else if(it->type() == typeid(double)) { std::cout << boost::any_cast< double>( *it ) << std::endl; } else if(it->type() == typeid(bool)) { std::cout << boost::any_cast< bool>( *it ) << std::endl; } else { std::cout << boost::any_cast< void * >( *it ) << std::endl; } } } else { std::cout << "not found" << std::endl; } return 0; }