1 #ifndef JSON_SPIRIT_READER_TEMPLATE
2 #define JSON_SPIRIT_READER_TEMPLATE
4 // Copyright John W. Wilkinson 2007 - 2009.
5 // Distributed under the MIT License, see accompanying file LICENSE.txt
7 // json spirit version 4.03
9 #include "json_spirit_value.h"
10 #include "json_spirit_error_position.h"
12 //#define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread
14 #include <boost/version.hpp>
19 #if BOOST_VERSION >= 103800
20 #include <boost/spirit/include/classic_core.hpp>
21 #include <boost/spirit/include/classic_confix.hpp>
22 #include <boost/spirit/include/classic_escape_char.hpp>
23 #include <boost/spirit/include/classic_multi_pass.hpp>
24 #include <boost/spirit/include/classic_position_iterator.hpp>
25 #define spirit_namespace boost::spirit::classic
27 #include <boost/spirit/core.hpp>
28 #include <boost/spirit/utility/confix.hpp>
29 #include <boost/spirit/utility/escape_char.hpp>
30 #include <boost/spirit/iterator/multi_pass.hpp>
31 #include <boost/spirit/iterator/position_iterator.hpp>
32 #define spirit_namespace boost::spirit
38 const spirit_namespace::int_parser < int64_t > int64_p = spirit_namespace::int_parser < int64_t >();
39 const spirit_namespace::uint_parser< uint64_t > uint64_p = spirit_namespace::uint_parser< uint64_t >();
41 template< class Iter_type >
42 bool is_eq( Iter_type first, Iter_type last, const char* c_str )
44 for( Iter_type i = first; i != last; ++i, ++c_str )
46 if( *c_str == 0 ) return false;
48 if( *i != *c_str ) return false;
54 template< class Char_type >
55 Char_type hex_to_num( const Char_type c )
57 if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
58 if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
59 if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
63 template< class Char_type, class Iter_type >
64 Char_type hex_str_to_char( Iter_type& begin )
66 const Char_type c1( *( ++begin ) );
67 const Char_type c2( *( ++begin ) );
69 return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
72 template< class Char_type, class Iter_type >
73 Char_type unicode_str_to_char( Iter_type& begin )
75 const Char_type c1( *( ++begin ) );
76 const Char_type c2( *( ++begin ) );
77 const Char_type c3( *( ++begin ) );
78 const Char_type c4( *( ++begin ) );
80 return ( hex_to_num( c1 ) << 12 ) +
81 ( hex_to_num( c2 ) << 8 ) +
82 ( hex_to_num( c3 ) << 4 ) +
86 template< class String_type >
87 void append_esc_char_and_incr_iter( String_type& s,
88 typename String_type::const_iterator& begin,
89 typename String_type::const_iterator end )
91 typedef typename String_type::value_type Char_type;
93 const Char_type c2( *begin );
97 case 't': s += '\t'; break;
98 case 'b': s += '\b'; break;
99 case 'f': s += '\f'; break;
100 case 'n': s += '\n'; break;
101 case 'r': s += '\r'; break;
102 case '\\': s += '\\'; break;
103 case '/': s += '/'; break;
104 case '"': s += '"'; break;
107 if( end - begin >= 3 ) // expecting "xHH..."
109 s += hex_str_to_char< Char_type >( begin );
115 if( end - begin >= 5 ) // expecting "uHHHH..."
117 s += unicode_str_to_char< Char_type >( begin );
124 template< class String_type >
125 String_type substitute_esc_chars( typename String_type::const_iterator begin,
126 typename String_type::const_iterator end )
128 typedef typename String_type::const_iterator Iter_type;
130 if( end - begin < 2 ) return String_type( begin, end );
134 result.reserve( end - begin );
136 const Iter_type end_minus_1( end - 1 );
138 Iter_type substr_start = begin;
141 for( ; i < end_minus_1; ++i )
145 result.append( substr_start, i );
149 append_esc_char_and_incr_iter( result, i, end );
151 substr_start = i + 1;
155 result.append( substr_start, end );
160 template< class String_type >
161 String_type get_str_( typename String_type::const_iterator begin,
162 typename String_type::const_iterator end )
164 assert( end - begin >= 2 );
166 typedef typename String_type::const_iterator Iter_type;
168 Iter_type str_without_quotes( ++begin );
169 Iter_type end_without_quotes( --end );
171 return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
174 inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
176 return get_str_< std::string >( begin, end );
179 inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
181 return get_str_< std::wstring >( begin, end );
184 template< class String_type, class Iter_type >
185 String_type get_str( Iter_type begin, Iter_type end )
187 const String_type tmp( begin, end ); // convert multipass iterators to string iterators
189 return get_str( tmp.begin(), tmp.end() );
192 // this class's methods get called by the spirit parse resulting
193 // in the creation of a JSON object or array
195 // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
197 template< class Value_type, class Iter_type >
198 class Semantic_actions
202 typedef typename Value_type::Config_type Config_type;
203 typedef typename Config_type::String_type String_type;
204 typedef typename Config_type::Object_type Object_type;
205 typedef typename Config_type::Array_type Array_type;
206 typedef typename String_type::value_type Char_type;
208 Semantic_actions( Value_type& value )
214 void begin_obj( Char_type c )
218 begin_compound< Object_type >();
221 void end_obj( Char_type c )
228 void begin_array( Char_type c )
232 begin_compound< Array_type >();
235 void end_array( Char_type c )
242 void new_name( Iter_type begin, Iter_type end )
244 assert( current_p_->type() == obj_type );
246 name_ = get_str< String_type >( begin, end );
249 void new_str( Iter_type begin, Iter_type end )
251 add_to_current( get_str< String_type >( begin, end ) );
254 void new_true( Iter_type begin, Iter_type end )
256 assert( is_eq( begin, end, "true" ) );
258 add_to_current( true );
261 void new_false( Iter_type begin, Iter_type end )
263 assert( is_eq( begin, end, "false" ) );
265 add_to_current( false );
268 void new_null( Iter_type begin, Iter_type end )
270 assert( is_eq( begin, end, "null" ) );
272 add_to_current( Value_type() );
275 void new_int( int64_t i )
280 void new_uint64( uint64_t ui )
282 add_to_current( ui );
285 void new_real( double d )
292 Semantic_actions& operator=( const Semantic_actions& );
293 // to prevent "assignment operator could not be generated" warning
295 Value_type* add_first( const Value_type& value )
297 assert( current_p_ == 0 );
300 current_p_ = &value_;
304 template< class Array_or_obj >
305 void begin_compound()
307 if( current_p_ == 0 )
309 add_first( Array_or_obj() );
313 stack_.push_back( current_p_ );
315 Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
317 current_p_ = add_to_current( new_array_or_obj );
323 if( current_p_ != &value_ )
325 current_p_ = stack_.back();
331 Value_type* add_to_current( const Value_type& value )
333 if( current_p_ == 0 )
335 return add_first( value );
337 else if( current_p_->type() == array_type )
339 current_p_->get_array().push_back( value );
341 return ¤t_p_->get_array().back();
344 assert( current_p_->type() == obj_type );
346 return &Config_type::add( current_p_->get_obj(), name_, value );
349 Value_type& value_; // this is the object or array that is being created
350 Value_type* current_p_; // the child object or array that is currently being constructed
352 std::vector< Value_type* > stack_; // previous child objects and arrays
354 String_type name_; // of current name/value pair
357 template< typename Iter_type >
358 void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
360 throw Error_position( i.get_position().line, i.get_position().column, reason );
363 template< typename Iter_type >
364 void throw_error( Iter_type i, const std::string& reason )
369 // the spirit grammer
371 template< class Value_type, class Iter_type >
372 class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
376 typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;
378 Json_grammer( Semantic_actions_t& semantic_actions )
379 : actions_( semantic_actions )
383 static void throw_not_value( Iter_type begin, Iter_type end )
385 throw_error( begin, "not a value" );
388 static void throw_not_array( Iter_type begin, Iter_type end )
390 throw_error( begin, "not an array" );
393 static void throw_not_object( Iter_type begin, Iter_type end )
395 throw_error( begin, "not an object" );
398 static void throw_not_pair( Iter_type begin, Iter_type end )
400 throw_error( begin, "not a pair" );
403 static void throw_not_colon( Iter_type begin, Iter_type end )
405 throw_error( begin, "no colon in pair" );
408 static void throw_not_string( Iter_type begin, Iter_type end )
410 throw_error( begin, "not a string" );
413 template< typename ScannerT >
418 definition( const Json_grammer& self )
420 using namespace spirit_namespace;
422 typedef typename Value_type::String_type::value_type Char_type;
424 // first we convert the semantic action class methods to functors with the
425 // parameter signature expected by spirit
427 typedef std::function< void( Char_type ) > Char_action;
428 typedef std::function< void( Iter_type, Iter_type ) > Str_action;
429 typedef std::function< void( double ) > Real_action;
430 typedef std::function< void( int64_t ) > Int_action;
431 typedef std::function< void( uint64_t ) > Uint64_action;
433 namespace stdp = std::placeholders;
434 Char_action begin_obj ( std::bind( &Semantic_actions_t::begin_obj, &self.actions_, stdp::_1 ) );
435 Char_action end_obj ( std::bind( &Semantic_actions_t::end_obj, &self.actions_, stdp::_1 ) );
436 Char_action begin_array( std::bind( &Semantic_actions_t::begin_array, &self.actions_, stdp::_1 ) );
437 Char_action end_array ( std::bind( &Semantic_actions_t::end_array, &self.actions_, stdp::_1 ) );
438 Str_action new_name ( std::bind( &Semantic_actions_t::new_name, &self.actions_, stdp::_1, stdp::_2 ) );
439 Str_action new_str ( std::bind( &Semantic_actions_t::new_str, &self.actions_, stdp::_1, stdp::_2 ) );
440 Str_action new_true ( std::bind( &Semantic_actions_t::new_true, &self.actions_, stdp::_1, stdp::_2 ) );
441 Str_action new_false ( std::bind( &Semantic_actions_t::new_false, &self.actions_, stdp::_1, stdp::_2 ) );
442 Str_action new_null ( std::bind( &Semantic_actions_t::new_null, &self.actions_, stdp::_1, stdp::_2 ) );
443 Real_action new_real ( std::bind( &Semantic_actions_t::new_real, &self.actions_, stdp::_1 ) );
444 Int_action new_int ( std::bind( &Semantic_actions_t::new_int, &self.actions_, stdp::_1 ) );
445 Uint64_action new_uint64 ( std::bind( &Semantic_actions_t::new_uint64, &self.actions_, stdp::_1 ) );
451 = value_ | eps_p[ &throw_not_value ]
459 | str_p( "true" ) [ new_true ]
460 | str_p( "false" )[ new_false ]
461 | str_p( "null" ) [ new_null ]
465 = ch_p('{')[ begin_obj ]
467 >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
471 = pair_ >> *( ',' >> pair_ )
475 = string_[ new_name ]
476 >> ( ':' | eps_p[ &throw_not_colon ] )
477 >> ( value_ | eps_p[ &throw_not_value ] )
481 = ch_p('[')[ begin_array ]
483 >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
487 = value_ >> *( ',' >> value_ )
491 = lexeme_d // this causes white space inside a string to be retained
503 = strict_real_p[ new_real ]
504 | int64_p [ new_int ]
505 | uint64_p [ new_uint64 ]
509 spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
511 const spirit_namespace::rule< ScannerT >& start() const { return json_; }
516 Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
518 Semantic_actions_t& actions_;
521 template< class Iter_type, class Value_type >
522 Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
524 Semantic_actions< Value_type, Iter_type > semantic_actions( value );
526 const spirit_namespace::parse_info< Iter_type > info =
527 spirit_namespace::parse( begin, end,
528 Json_grammer< Value_type, Iter_type >( semantic_actions ),
529 spirit_namespace::space_p );
533 assert( false ); // in theory exception should already have been thrown
534 throw_error( info.stop, "error" );
540 template< class Iter_type, class Value_type >
541 void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
543 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
545 const Posn_iter_t posn_begin( begin, end );
546 const Posn_iter_t posn_end( end, end );
548 read_range_or_throw( posn_begin, posn_end, value );
551 template< class Iter_type, class Value_type >
552 bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
556 begin = read_range_or_throw( begin, end, value );
566 template< class String_type, class Value_type >
567 void read_string_or_throw( const String_type& s, Value_type& value )
569 add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
572 template< class String_type, class Value_type >
573 bool read_string( const String_type& s, Value_type& value )
575 typename String_type::const_iterator begin = s.begin();
577 return read_range( begin, s.end(), value );
580 template< class Istream_type >
581 struct Multi_pass_iters
583 typedef typename Istream_type::char_type Char_type;
584 typedef std::istream_iterator< Char_type, Char_type > istream_iter;
585 typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
587 Multi_pass_iters( Istream_type& is )
589 is.unsetf( std::ios::skipws );
591 begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
592 end_ = spirit_namespace::make_multi_pass( istream_iter() );
599 template< class Istream_type, class Value_type >
600 bool read_stream( Istream_type& is, Value_type& value )
602 Multi_pass_iters< Istream_type > mp_iters( is );
604 return read_range( mp_iters.begin_, mp_iters.end_, value );
607 template< class Istream_type, class Value_type >
608 void read_stream_or_throw( Istream_type& is, Value_type& value )
610 const Multi_pass_iters< Istream_type > mp_iters( is );
612 add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );