| 1 | #include <boost/any.hpp>
|
|---|
| 2 | #include <boost/spirit/include/qi.hpp>
|
|---|
| 3 | #include <boost/fusion/adapted/std_pair.hpp>
|
|---|
| 4 | #include <boost/fusion/adapted/boost_tuple.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <vector>
|
|---|
| 7 | #include <map>
|
|---|
| 8 | #include <iostream>
|
|---|
| 9 |
|
|---|
| 10 | namespace json {
|
|---|
| 11 |
|
|---|
| 12 | namespace qi = boost::spirit::qi;
|
|---|
| 13 | namespace ascii = boost::spirit::ascii;
|
|---|
| 14 |
|
|---|
| 15 | struct nullptr_t_ : qi::symbols< char, void * > {
|
|---|
| 16 | nullptr_t_() {
|
|---|
| 17 | add( "null", NULL );
|
|---|
| 18 | }
|
|---|
| 19 | } nullptr_;
|
|---|
| 20 |
|
|---|
| 21 | typedef std::map<std::string, boost::any> map_any;
|
|---|
| 22 | typedef std::vector<boost::any> vector_any;
|
|---|
| 23 | typedef std::pair<std::string, boost::any> pair_any;
|
|---|
| 24 |
|
|---|
| 25 | template< typename Iterator >
|
|---|
| 26 | struct Grammar : qi::grammar< Iterator, boost::any(), ascii::space_type > {
|
|---|
| 27 | Grammar(): Grammar::base_type( start ) {
|
|---|
| 28 | using qi::lexeme;
|
|---|
| 29 | using qi::double_;
|
|---|
| 30 | using qi::bool_;
|
|---|
| 31 | using ascii::char_;
|
|---|
| 32 |
|
|---|
| 33 | start = value_rule.alias();
|
|---|
| 34 | object_rule = '{' >> pair_rule % ',' >> '}';
|
|---|
| 35 | pair_rule = string_rule >> ':' >> value_rule;
|
|---|
| 36 | value_rule = object_rule | array_rule | string_rule | nullptr_ | double_ | bool_;
|
|---|
| 37 | array_rule = '[' >> value_rule % ',' >> ']';
|
|---|
| 38 | string_rule = lexeme[ '\"' >> *( char_ - '\"' ) >> '\"' ];
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | qi::rule<Iterator, boost::any(), ascii::space_type> start;
|
|---|
| 42 | qi::rule<Iterator, map_any(), ascii::space_type> object_rule;
|
|---|
| 43 | qi::rule<Iterator, pair_any(), ascii::space_type> pair_rule;
|
|---|
| 44 | qi::rule<Iterator, boost::any(), ascii::space_type> value_rule;
|
|---|
| 45 | qi::rule<Iterator, vector_any(), ascii::space_type> array_rule;
|
|---|
| 46 | qi::rule<Iterator, std::string(), ascii::space_type> string_rule;
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | int main(int argc, char** argv) {
|
|---|
| 52 | //"{\"name\":10}"
|
|---|
| 53 | const std::string source(argv[1]);
|
|---|
| 54 | json::Grammar< std::string::const_iterator > g;
|
|---|
| 55 | boost::any v;
|
|---|
| 56 | json::map_any ma;
|
|---|
| 57 | json::vector_any va;
|
|---|
| 58 | json::pair_any pa;
|
|---|
| 59 | std::string::const_iterator bit = source.begin();
|
|---|
| 60 | std::string::const_iterator eit = source.end();
|
|---|
| 61 | bool r = boost::spirit::qi::phrase_parse( bit, eit, g, boost::spirit::ascii::space, v );
|
|---|
| 62 | if( r ) {
|
|---|
| 63 | std::cout << BOOST_LIB_VERSION << std::endl;
|
|---|
| 64 | std::cout << v.type().name() << std::endl;
|
|---|
| 65 | std::cout << typeid(ma).name() << std::endl;
|
|---|
| 66 | std::cout << typeid(va).name() << std::endl;
|
|---|
| 67 | std::cout << typeid(pa).name() << std::endl;
|
|---|
| 68 | return 0;
|
|---|
| 69 | std::vector< boost::any> a = boost::any_cast< std::vector< boost::any> >( v );
|
|---|
| 70 | for( std::vector< boost::any>::iterator it = a.begin(); it != a.end(); ++it ) {
|
|---|
| 71 | if(it->type() == typeid(char*)) {
|
|---|
| 72 | std::cout << boost::any_cast< char*>( *it ) << std::endl;
|
|---|
| 73 | } else if(it->type() == typeid(const char*)) {
|
|---|
| 74 | std::cout << boost::any_cast< const char*>( *it ) << std::endl;
|
|---|
| 75 | } else if(it->type() == typeid(std::string)) {
|
|---|
| 76 | std::cout << boost::any_cast< std::string>( *it ) << std::endl;
|
|---|
| 77 | } else if(it->type() == typeid(double)) {
|
|---|
| 78 | std::cout << boost::any_cast< double>( *it ) << std::endl;
|
|---|
| 79 | } else if(it->type() == typeid(bool)) {
|
|---|
| 80 | std::cout << boost::any_cast< bool>( *it ) << std::endl;
|
|---|
| 81 | } else {
|
|---|
| 82 | std::cout << boost::any_cast< void * >( *it ) << std::endl;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | } else {
|
|---|
| 86 | std::cout << "not found" << std::endl;
|
|---|
| 87 | }
|
|---|
| 88 | return 0;
|
|---|
| 89 | }
|
|---|