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/bind.hpp>
15 #include <boost/function.hpp>
16 #include <boost/version.hpp>
18 #if BOOST_VERSION >= 103800
19 #include <boost/spirit/include/classic_core.hpp>
20 #include <boost/spirit/include/classic_confix.hpp>
21 #include <boost/spirit/include/classic_escape_char.hpp>
22 #include <boost/spirit/include/classic_multi_pass.hpp>
23 #include <boost/spirit/include/classic_position_iterator.hpp>
24 #define spirit_namespace boost::spirit::classic
26 #include <boost/spirit/core.hpp>
27 #include <boost/spirit/utility/confix.hpp>
28 #include <boost/spirit/utility/escape_char.hpp>
29 #include <boost/spirit/iterator/multi_pass.hpp>
30 #include <boost/spirit/iterator/position_iterator.hpp>
31 #define spirit_namespace boost::spirit
36 const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >();
37 const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
39 template< class Iter_type >
40 bool is_eq( Iter_type first, Iter_type last, const char* c_str )
42 for( Iter_type i = first; i != last; ++i, ++c_str )
44 if( *c_str == 0 ) return false;
46 if( *i != *c_str ) return false;
52 template< class Char_type >
53 Char_type hex_to_num( const Char_type c )
55 if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
56 if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
57 if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
61 template< class Char_type, class Iter_type >
62 Char_type hex_str_to_char( Iter_type& begin )
64 const Char_type c1( *( ++begin ) );
65 const Char_type c2( *( ++begin ) );
67 return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
70 template< class Char_type, class Iter_type >
71 Char_type unicode_str_to_char( Iter_type& begin )
73 const Char_type c1( *( ++begin ) );
74 const Char_type c2( *( ++begin ) );
75 const Char_type c3( *( ++begin ) );
76 const Char_type c4( *( ++begin ) );
78 return ( hex_to_num( c1 ) << 12 ) +
79 ( hex_to_num( c2 ) << 8 ) +
80 ( hex_to_num( c3 ) << 4 ) +
84 template< class String_type >
85 void append_esc_char_and_incr_iter( String_type& s,
86 typename String_type::const_iterator& begin,
87 typename String_type::const_iterator end )
89 typedef typename String_type::value_type Char_type;
91 const Char_type c2( *begin );
95 case 't': s += '\t'; break;
96 case 'b': s += '\b'; break;
97 case 'f': s += '\f'; break;
98 case 'n': s += '\n'; break;
99 case 'r': s += '\r'; break;
100 case '\\': s += '\\'; break;
101 case '/': s += '/'; break;
102 case '"': s += '"'; break;
105 if( end - begin >= 3 ) // expecting "xHH..."
107 s += hex_str_to_char< Char_type >( begin );
113 if( end - begin >= 5 ) // expecting "uHHHH..."
115 s += unicode_str_to_char< Char_type >( begin );
122 template< class String_type >
123 String_type substitute_esc_chars( typename String_type::const_iterator begin,
124 typename String_type::const_iterator end )
126 typedef typename String_type::const_iterator Iter_type;
128 if( end - begin < 2 ) return String_type( begin, end );
132 result.reserve( end - begin );
134 const Iter_type end_minus_1( end - 1 );
136 Iter_type substr_start = begin;
139 for( ; i < end_minus_1; ++i )
143 result.append( substr_start, i );
147 append_esc_char_and_incr_iter( result, i, end );
149 substr_start = i + 1;
153 result.append( substr_start, end );
158 template< class String_type >
159 String_type get_str_( typename String_type::const_iterator begin,
160 typename String_type::const_iterator end )
162 assert( end - begin >= 2 );
164 typedef typename String_type::const_iterator Iter_type;
166 Iter_type str_without_quotes( ++begin );
167 Iter_type end_without_quotes( --end );
169 return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
172 inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
174 return get_str_< std::string >( begin, end );
177 inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
179 return get_str_< std::wstring >( begin, end );
182 template< class String_type, class Iter_type >
183 String_type get_str( Iter_type begin, Iter_type end )
185 const String_type tmp( begin, end ); // convert multipass iterators to string iterators
187 return get_str( tmp.begin(), tmp.end() );
190 // this class's methods get called by the spirit parse resulting
191 // in the creation of a JSON object or array
193 // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
195 template< class Value_type, class Iter_type >
196 class Semantic_actions
200 typedef typename Value_type::Config_type Config_type;
201 typedef typename Config_type::String_type String_type;
202 typedef typename Config_type::Object_type Object_type;
203 typedef typename Config_type::Array_type Array_type;
204 typedef typename String_type::value_type Char_type;
206 Semantic_actions( Value_type& value )
212 void begin_obj( Char_type c )
216 begin_compound< Object_type >();
219 void end_obj( Char_type c )
226 void begin_array( Char_type c )
230 begin_compound< Array_type >();
233 void end_array( Char_type c )
240 void new_name( Iter_type begin, Iter_type end )
242 assert( current_p_->type() == obj_type );
244 name_ = get_str< String_type >( begin, end );
247 void new_str( Iter_type begin, Iter_type end )
249 add_to_current( get_str< String_type >( begin, end ) );
252 void new_true( Iter_type begin, Iter_type end )
254 assert( is_eq( begin, end, "true" ) );
256 add_to_current( true );
259 void new_false( Iter_type begin, Iter_type end )
261 assert( is_eq( begin, end, "false" ) );
263 add_to_current( false );
266 void new_null( Iter_type begin, Iter_type end )
268 assert( is_eq( begin, end, "null" ) );
270 add_to_current( Value_type() );
273 void new_int( boost::int64_t i )
278 void new_uint64( boost::uint64_t ui )
280 add_to_current( ui );
283 void new_real( double d )
290 Semantic_actions& operator=( const Semantic_actions& );
291 // to prevent "assignment operator could not be generated" warning
293 Value_type* add_first( const Value_type& value )
295 assert( current_p_ == 0 );
298 current_p_ = &value_;
302 template< class Array_or_obj >
303 void begin_compound()
305 if( current_p_ == 0 )
307 add_first( Array_or_obj() );
311 stack_.push_back( current_p_ );
313 Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
315 current_p_ = add_to_current( new_array_or_obj );
321 if( current_p_ != &value_ )
323 current_p_ = stack_.back();
329 Value_type* add_to_current( const Value_type& value )
331 if( current_p_ == 0 )
333 return add_first( value );
335 else if( current_p_->type() == array_type )
337 current_p_->get_array().push_back( value );
339 return ¤t_p_->get_array().back();
342 assert( current_p_->type() == obj_type );
344 return &Config_type::add( current_p_->get_obj(), name_, value );
347 Value_type& value_; // this is the object or array that is being created
348 Value_type* current_p_; // the child object or array that is currently being constructed
350 std::vector< Value_type* > stack_; // previous child objects and arrays
352 String_type name_; // of current name/value pair
355 template< typename Iter_type >
356 void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
358 throw Error_position( i.get_position().line, i.get_position().column, reason );
361 template< typename Iter_type >
362 void throw_error( Iter_type i, const std::string& reason )
367 // the spirit grammer
369 template< class Value_type, class Iter_type >
370 class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
374 typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;
376 Json_grammer( Semantic_actions_t& semantic_actions )
377 : actions_( semantic_actions )
381 static void throw_not_value( Iter_type begin, Iter_type end )
383 throw_error( begin, "not a value" );
386 static void throw_not_array( Iter_type begin, Iter_type end )
388 throw_error( begin, "not an array" );
391 static void throw_not_object( Iter_type begin, Iter_type end )
393 throw_error( begin, "not an object" );
396 static void throw_not_pair( Iter_type begin, Iter_type end )
398 throw_error( begin, "not a pair" );
401 static void throw_not_colon( Iter_type begin, Iter_type end )
403 throw_error( begin, "no colon in pair" );
406 static void throw_not_string( Iter_type begin, Iter_type end )
408 throw_error( begin, "not a string" );
411 template< typename ScannerT >
416 definition( const Json_grammer& self )
418 using namespace spirit_namespace;
420 typedef typename Value_type::String_type::value_type Char_type;
422 // first we convert the semantic action class methods to functors with the
423 // parameter signature expected by spirit
425 typedef boost::function< void( Char_type ) > Char_action;
426 typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
427 typedef boost::function< void( double ) > Real_action;
428 typedef boost::function< void( boost::int64_t ) > Int_action;
429 typedef boost::function< void( boost::uint64_t ) > Uint64_action;
431 Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) );
432 Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) );
433 Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
434 Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) );
435 Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) );
436 Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) );
437 Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) );
438 Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) );
439 Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) );
440 Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) );
441 Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) );
442 Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) );
447 = value_ | eps_p[ &throw_not_value ]
455 | str_p( "true" ) [ new_true ]
456 | str_p( "false" )[ new_false ]
457 | str_p( "null" ) [ new_null ]
461 = ch_p('{')[ begin_obj ]
463 >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
467 = pair_ >> *( ',' >> pair_ )
471 = string_[ new_name ]
472 >> ( ':' | eps_p[ &throw_not_colon ] )
473 >> ( value_ | eps_p[ &throw_not_value ] )
477 = ch_p('[')[ begin_array ]
479 >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
483 = value_ >> *( ',' >> value_ )
487 = lexeme_d // this causes white space inside a string to be retained
499 = strict_real_p[ new_real ]
500 | int64_p [ new_int ]
501 | uint64_p [ new_uint64 ]
505 spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
507 const spirit_namespace::rule< ScannerT >& start() const { return json_; }
512 Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
514 Semantic_actions_t& actions_;
517 template< class Iter_type, class Value_type >
518 Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
520 Semantic_actions< Value_type, Iter_type > semantic_actions( value );
522 const spirit_namespace::parse_info< Iter_type > info =
523 spirit_namespace::parse( begin, end,
524 Json_grammer< Value_type, Iter_type >( semantic_actions ),
525 spirit_namespace::space_p );
529 assert( false ); // in theory exception should already have been thrown
530 throw_error( info.stop, "error" );
536 template< class Iter_type, class Value_type >
537 void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
539 typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
541 const Posn_iter_t posn_begin( begin, end );
542 const Posn_iter_t posn_end( end, end );
544 read_range_or_throw( posn_begin, posn_end, value );
547 template< class Iter_type, class Value_type >
548 bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
552 begin = read_range_or_throw( begin, end, value );
562 template< class String_type, class Value_type >
563 void read_string_or_throw( const String_type& s, Value_type& value )
565 add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
568 template< class String_type, class Value_type >
569 bool read_string( const String_type& s, Value_type& value )
571 typename String_type::const_iterator begin = s.begin();
573 return read_range( begin, s.end(), value );
576 template< class Istream_type >
577 struct Multi_pass_iters
579 typedef typename Istream_type::char_type Char_type;
580 typedef std::istream_iterator< Char_type, Char_type > istream_iter;
581 typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
583 Multi_pass_iters( Istream_type& is )
585 is.unsetf( std::ios::skipws );
587 begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
588 end_ = spirit_namespace::make_multi_pass( istream_iter() );
595 template< class Istream_type, class Value_type >
596 bool read_stream( Istream_type& is, Value_type& value )
598 Multi_pass_iters< Istream_type > mp_iters( is );
600 return read_range( mp_iters.begin_, mp_iters.end_, value );
603 template< class Istream_type, class Value_type >
604 void read_stream_or_throw( Istream_type& is, Value_type& value )
606 const Multi_pass_iters< Istream_type > mp_iters( is );
608 add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );