Flow123d  DF_profiler_memory_monitor-83fd579
json_spirit_reader_template.h
Go to the documentation of this file.
1 #ifndef JSON_SPIRIT_READER_TEMPLATE
2 #define JSON_SPIRIT_READER_TEMPLATE
3 
4 // Copyright John W. Wilkinson 2007 - 2011
5 // Distributed under the MIT License, see accompanying file LICENSE.txt
6 
7 // json spirit version 4.05
8 
9 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 # pragma once
11 #endif
12 
13 #include "json_spirit_value.h"
15 
16 //#define BOOST_SPIRIT_THREADSAFE // uncomment for multithreaded use, requires linking to boost.thread
17 
18 #include <boost/bind.hpp>
19 #include <boost/function.hpp>
20 #include <boost/version.hpp>
21 
22 #if BOOST_VERSION >= 103800
23  #include <boost/spirit/include/classic_core.hpp>
24  #include <boost/spirit/include/classic_confix.hpp>
25  #include <boost/spirit/include/classic_escape_char.hpp>
26  #include <boost/spirit/include/classic_multi_pass.hpp>
27  #include <boost/spirit/include/classic_position_iterator.hpp>
28  #define spirit_namespace boost::spirit::classic
29 #else
30  #include <boost/spirit/core.hpp>
31  #include <boost/spirit/utility/confix.hpp>
32  #include <boost/spirit/utility/escape_char.hpp>
33  #include <boost/spirit/iterator/multi_pass.hpp>
34  #include <boost/spirit/iterator/position_iterator.hpp>
35  #define spirit_namespace boost::spirit
36 #endif
37 
38 namespace json_spirit
39 {
40  const spirit_namespace::int_parser < boost::int64_t > int64_p = spirit_namespace::int_parser < boost::int64_t >();
41  const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();
42 
43  template< class Iter_type >
44  bool is_eq( Iter_type first, Iter_type last, const char* c_str )
45  {
46  for( Iter_type i = first; i != last; ++i, ++c_str )
47  {
48  if( *c_str == 0 ) return false;
49 
50  if( *i != *c_str ) return false;
51  }
52 
53  return true;
54  }
55 
56  template< class Char_type >
57  Char_type hex_to_num( const Char_type c )
58  {
59  if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
60  if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
61  if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
62  return 0;
63  }
64 
65  template< class Char_type, class Iter_type >
66  Char_type hex_str_to_char( Iter_type& begin )
67  {
68  const Char_type c1( *( ++begin ) );
69  const Char_type c2( *( ++begin ) );
70 
71  return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );
72  }
73 
74  template< class Char_type, class Iter_type >
75  Char_type unicode_str_to_char( Iter_type& begin )
76  {
77  const Char_type c1( *( ++begin ) );
78  const Char_type c2( *( ++begin ) );
79  const Char_type c3( *( ++begin ) );
80  const Char_type c4( *( ++begin ) );
81 
82  return ( hex_to_num( c1 ) << 12 ) +
83  ( hex_to_num( c2 ) << 8 ) +
84  ( hex_to_num( c3 ) << 4 ) +
85  hex_to_num( c4 );
86  }
87 
88  template< class String_type >
89  void append_esc_char_and_incr_iter( String_type& s,
90  typename String_type::const_iterator& begin,
91  typename String_type::const_iterator end )
92  {
93  typedef typename String_type::value_type Char_type;
94 
95  const Char_type c2( *begin );
96 
97  switch( c2 )
98  {
99  case 't': s += '\t'; break;
100  case 'b': s += '\b'; break;
101  case 'f': s += '\f'; break;
102  case 'n': s += '\n'; break;
103  case 'r': s += '\r'; break;
104  case '\\': s += '\\'; break;
105  case '/': s += '/'; break;
106  case '"': s += '"'; break;
107  case 'x':
108  {
109  if( end - begin >= 3 ) // expecting "xHH..."
110  {
111  s += hex_str_to_char< Char_type >( begin );
112  }
113  break;
114  }
115  case 'u':
116  {
117  if( end - begin >= 5 ) // expecting "uHHHH..."
118  {
119  s += unicode_str_to_char< Char_type >( begin );
120  }
121  break;
122  }
123  }
124  }
125 
126  template< class String_type >
127  String_type substitute_esc_chars( typename String_type::const_iterator begin,
128  typename String_type::const_iterator end )
129  {
130  typedef typename String_type::const_iterator Iter_type;
131 
132  if( end - begin < 2 ) return String_type( begin, end );
133 
134  String_type result;
135 
136  result.reserve( end - begin );
137 
138  const Iter_type end_minus_1( end - 1 );
139 
140  Iter_type substr_start = begin;
141  Iter_type i = begin;
142 
143  for( ; i < end_minus_1; ++i )
144  {
145  if( *i == '\\' )
146  {
147  result.append( substr_start, i );
148 
149  ++i; // skip the '\'
150 
151  append_esc_char_and_incr_iter( result, i, end );
152 
153  substr_start = i + 1;
154  }
155  }
156 
157  result.append( substr_start, end );
158 
159  return result;
160  }
161 
162  template< class String_type >
163  String_type get_str_( typename String_type::const_iterator begin,
164  typename String_type::const_iterator end )
165  {
166  // assert( end - begin >= 2 );
167 
168  typedef typename String_type::const_iterator Iter_type;
169 
170  if ( ( *begin == '"' ) && ( *(end-1) == '"' ) )
171  {
172  ++begin;
173  --end;
174  }
175 
176  Iter_type str_without_quotes( begin );
177  Iter_type end_without_quotes( end );
178 
179  return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );
180  }
181 
182  inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )
183  {
184  return get_str_< std::string >( begin, end );
185  }
186 
187  inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )
188  {
189  return get_str_< std::wstring >( begin, end );
190  }
191 
192  template< class String_type, class Iter_type >
193  String_type get_str( Iter_type begin, Iter_type end )
194  {
195  const String_type tmp( begin, end ); // convert multipass iterators to string iterators
196 
197  return get_str( tmp.begin(), tmp.end() );
198  }
199 
200  // this class's methods get called by the spirit parse resulting
201  // in the creation of a JSON object or array
202  //
203  // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator
204  //
205  template< class Value_type, class Iter_type >
207  {
208  public:
209 
210  typedef typename Value_type::Config_type Config_type;
211  typedef typename Config_type::String_type String_type;
212  typedef typename Config_type::Object_type Object_type;
213  typedef typename Config_type::Array_type Array_type;
214  typedef typename String_type::value_type Char_type;
215 
217  : value_( value )
218  , current_p_( 0 )
219  {
220  }
221 
223  {
224  assert( c == '{' );
225  (void)c;
226 
227  begin_compound< Object_type >();
228  }
229 
230  void end_obj( Char_type c )
231  {
232  assert( c == '}' );
233  (void)c;
234 
235  end_compound();
236  }
237 
239  {
240  assert( c == '[' );
241  (void)c;
242 
243  begin_compound< Array_type >();
244  }
245 
247  {
248  assert( c == ']' );
249  (void)c;
250 
251  end_compound();
252  }
253 
254  void new_name( Iter_type begin, Iter_type end )
255  {
256  assert( current_p_->type() == obj_type );
257 
258  name_ = get_str< String_type >( begin, end );
259  }
260 
261  void new_str( Iter_type begin, Iter_type end )
262  {
263  add_to_current( get_str< String_type >( begin, end ) );
264  }
265 
266  void new_true( Iter_type begin, Iter_type end )
267  {
268  assert( is_eq( begin, end, "true" ) );
269  (void)begin;
270  (void)end;
271 
272  add_to_current( true );
273  }
274 
275  void new_false( Iter_type begin, Iter_type end )
276  {
277  assert( is_eq( begin, end, "false" ) );
278  (void)begin;
279  (void)end;
280 
281  add_to_current( false );
282  }
283 
284  void new_null( Iter_type begin, Iter_type end )
285  {
286  assert( is_eq( begin, end, "null" ) );
287  (void)begin;
288  (void)end;
289 
291  }
292 
293  void new_int( boost::int64_t i )
294  {
295  add_to_current( i );
296  }
297 
298  void new_uint64( boost::uint64_t ui )
299  {
300  add_to_current( ui );
301  }
302 
303  void new_real( double d )
304  {
305  add_to_current( d );
306  }
307 
308  private:
309 
311  // to prevent "assignment operator could not be generated" warning
312 
314  {
315  assert( current_p_ == 0 );
316 
317  value_ = value;
318  current_p_ = &value_;
319  return current_p_;
320  }
321 
322  template< class Array_or_obj >
324  {
325  if( current_p_ == 0 )
326  {
327  add_first( Array_or_obj() );
328  }
329  else
330  {
331  stack_.push_back( current_p_ );
332 
333  Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
334 
335  current_p_ = add_to_current( new_array_or_obj );
336  }
337  }
338 
340  {
341  if( current_p_ != &value_ )
342  {
343  current_p_ = stack_.back();
344 
345  stack_.pop_back();
346  }
347  }
348 
350  {
351  if( current_p_ == 0 )
352  {
353  return add_first( value );
354  }
355  else if( current_p_->type() == array_type )
356  {
357  current_p_->get_array().push_back( value );
358 
359  return &current_p_->get_array().back();
360  }
361 
362  assert( current_p_->type() == obj_type );
363 
364  return &Config_type::add( current_p_->get_obj(), name_, value );
365  }
366 
367  Value_type& value_; // this is the object or array that is being created
368  Value_type* current_p_; // the child object or array that is currently being constructed
369 
370  std::vector< Value_type* > stack_; // previous child objects and arrays
371 
372  String_type name_; // of current name/value pair
373  };
374 
375  template< typename Iter_type >
376  void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
377  {
378  throw Error_position( i.get_position().line, i.get_position().column, reason );
379  }
380 
381  template< typename Iter_type >
382  void throw_error(Iter_type i, const std::string& reason )
383  {
384  (void)i;
385  throw reason;
386  }
387 
388  // the spirit grammer
389  //
390  template< class Value_type, class Iter_type >
391  class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
392  {
393  public:
394 
396 
397  Json_grammer( Semantic_actions_t& semantic_actions )
398  : actions_( semantic_actions )
399  {
400  }
401 
402  static void throw_not_value( Iter_type begin, Iter_type end )
403  {
404  (void)end;
405  throw_error( begin, "not a value" );
406  }
407 
408  static void throw_not_array( Iter_type begin, Iter_type end )
409  {
410  (void)end;
411  throw_error( begin, "not an array" );
412  }
413 
414  static void throw_not_object( Iter_type begin, Iter_type end )
415  {
416  (void)end;
417  throw_error( begin, "not an object" );
418  }
419 
420  static void throw_not_pair( Iter_type begin, Iter_type end )
421  {
422  (void)end;
423  throw_error( begin, "not a pair" );
424  }
425 
426  static void throw_not_colon( Iter_type begin, Iter_type end )
427  {
428  (void)end;
429  throw_error( begin, "no colon in pair" );
430  }
431 
432  static void throw_not_string( Iter_type begin, Iter_type end )
433  {
434  (void)end;
435  throw_error( begin, "not a string" );
436  }
437 
438  template< typename ScannerT >
440  {
441  public:
442 
443  definition( const Json_grammer& self )
444  {
445  using namespace spirit_namespace;
446 
447  typedef typename Value_type::String_type::value_type Char_type;
448 
449  // first we convert the semantic action class methods to functors with the
450  // parameter signature expected by spirit
451 
452  typedef boost::function< void( Char_type ) > Char_action;
453  typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
454  typedef boost::function< void( double ) > Real_action;
455  typedef boost::function< void( boost::int64_t ) > Int_action;
456  typedef boost::function< void( boost::uint64_t ) > Uint64_action;
457 
458  Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) );
459  Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) );
460  Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
461  Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) );
462  Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) );
463  Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) );
464  Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) );
465  Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) );
466  Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) );
467  Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) );
468  Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) );
469  Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) );
470 
471  // actual grammer
472 
473  json_
474  = value_ | eps_p[ &throw_not_value ]
475  ;
476 
477  value_
478  = string_[ new_str ]
479  | number_
480  | object_
481  | array_
482  | str_p( "true" ) [ new_true ]
483  | str_p( "false" )[ new_false ]
484  | str_p( "null" ) [ new_null ]
485  ;
486 
487  object_
488  = ch_p('{')[ begin_obj ]
489  >> !members_
490  >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
491  ;
492 
493  members_
494  = pair_ >> *( ( ch_p(',') | *space_p ) >> pair_ )
495  ;
496 
497  pair_
498  = ( string_[ new_name ] | key_name_[ new_name ] )
499  >> ( ch_p(':') | ch_p('=') | eps_p[ &throw_not_colon ] )
500  >> ( value_ | eps_p[ &throw_not_value ] )
501  ;
502 
503  array_
504  = ch_p('[')[ begin_array ]
505  >> !elements_
506  >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
507  ;
508 
509  elements_
510  = value_ >> *( ',' >> value_ )
511  ;
512 
513  string_
514  = lexeme_d // this causes white space inside a string to be retained
515  [
516  confix_p
517  (
518  '"',
519  *lex_escape_ch_p,
520  '"'
521  )
522  ]
523  ;
524 
525  key_name_
526  = alpha_p
527  >> *( alnum_p | ch_p('_') )
528  ;
529 
530  number_
531  = strict_real_p[ new_real ]
532  | int64_p [ new_int ]
533  | uint64_p [ new_uint64 ]
534  ;
535  }
536 
537  spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, key_name_, number_;
538 
539  const spirit_namespace::rule< ScannerT >& start() const { return json_; }
540  };
541 
542  private:
543 
544  Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
545 
547  };
548 
549  template< class Iter_type, class Value_type >
550  void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
551  {
552  typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
553 
554  const Posn_iter_t posn_begin( begin, end );
555  const Posn_iter_t posn_end( end, end );
556 
557  read_range_or_throw( posn_begin, posn_end, value );
558  }
559 
560  template< class Istream_type >
562  {
563  typedef typename Istream_type::char_type Char_type;
564  typedef std::istream_iterator< Char_type, Char_type > istream_iter;
565  typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
566 
567  Multi_pass_iters( Istream_type& is )
568  {
569  is.unsetf( std::ios::skipws );
570 
571  begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
572  end_ = spirit_namespace::make_multi_pass( istream_iter() );
573  }
574 
577  };
578 
579  // reads a JSON Value from a pair of input iterators throwing an exception on invalid input, e.g.
580  //
581  // string::const_iterator start = str.begin();
582  // const string::const_iterator next = read_range_or_throw( str.begin(), str.end(), value );
583  //
584  // The iterator 'next' will point to the character past the
585  // last one read.
586  //
587  template< class Iter_type, class Value_type >
588  Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
589  {
591 
592  const spirit_namespace::parse_info< Iter_type > info =
593  spirit_namespace::parse( begin, end,
594  Json_grammer< Value_type, Iter_type >( semantic_actions ),
595  spirit_namespace::space_p );
596 
597  if( !info.hit )
598  {
599  assert( false ); // in theory exception should already have been thrown
600  throw_error( info.stop, "error" );
601  }
602 
603  return info.stop;
604  }
605 
606  // reads a JSON Value from a pair of input iterators, e.g.
607  //
608  // string::const_iterator start = str.begin();
609  // const bool success = read_string( start, str.end(), value );
610  //
611  // The iterator 'start' will point to the character past the
612  // last one read.
613  //
614  template< class Iter_type, class Value_type >
615  bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
616  {
617  try
618  {
619  begin = read_range_or_throw( begin, end, value );
620 
621  return true;
622  }
623  catch( ... )
624  {
625  return false;
626  }
627  }
628 
629  // reads a JSON Value from a string, e.g.
630  //
631  // const bool success = read_string( str, value );
632  //
633  template< class String_type, class Value_type >
634  bool read_string( const String_type& s, Value_type& value )
635  {
636  typename String_type::const_iterator begin = s.begin();
637 
638  return read_range( begin, s.end(), value );
639  }
640 
641  // reads a JSON Value from a string throwing an exception on invalid input, e.g.
642  //
643  // read_string_or_throw( is, value );
644  //
645  template< class String_type, class Value_type >
646  void read_string_or_throw( const String_type& s, Value_type& value )
647  {
648  add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
649  }
650 
651  // reads a JSON Value from a stream, e.g.
652  //
653  // const bool success = read_stream( is, value );
654  //
655  template< class Istream_type, class Value_type >
656  bool read_stream( Istream_type& is, Value_type& value )
657  {
658  Multi_pass_iters< Istream_type > mp_iters( is );
659 
660  return read_range( mp_iters.begin_, mp_iters.end_, value );
661  }
662 
663  // reads a JSON Value from a stream throwing an exception on invalid input, e.g.
664  //
665  // read_stream_or_throw( is, value );
666  //
667  template< class Istream_type, class Value_type >
668  void read_stream_or_throw( Istream_type& is, Value_type& value )
669  {
670  const Multi_pass_iters< Istream_type > mp_iters( is );
671 
673  }
674 }
675 
676 #endif
spirit_namespace::rule< ScannerT > array_
spirit_namespace::rule< ScannerT > string_
spirit_namespace::rule< ScannerT > json_
spirit_namespace::rule< ScannerT > key_name_
spirit_namespace::rule< ScannerT > elements_
spirit_namespace::rule< ScannerT > object_
spirit_namespace::rule< ScannerT > members_
spirit_namespace::rule< ScannerT > number_
const spirit_namespace::rule< ScannerT > & start() const
spirit_namespace::rule< ScannerT > pair_
spirit_namespace::rule< ScannerT > value_
static void throw_not_value(Iter_type begin, Iter_type end)
Json_grammer(Semantic_actions_t &semantic_actions)
static void throw_not_string(Iter_type begin, Iter_type end)
Json_grammer & operator=(const Json_grammer &)
static void throw_not_pair(Iter_type begin, Iter_type end)
static void throw_not_colon(Iter_type begin, Iter_type end)
static void throw_not_array(Iter_type begin, Iter_type end)
Semantic_actions< Value_type, Iter_type > Semantic_actions_t
static void throw_not_object(Iter_type begin, Iter_type end)
void new_name(Iter_type begin, Iter_type end)
void new_false(Iter_type begin, Iter_type end)
Value_type * add_first(const Value_type &value)
void new_true(Iter_type begin, Iter_type end)
Value_type * add_to_current(const Value_type &value)
Semantic_actions & operator=(const Semantic_actions &)
void new_str(Iter_type begin, Iter_type end)
void new_null(Iter_type begin, Iter_type end)
static constexpr bool value
Definition: json.hpp:87
#define spirit_namespace
void append_esc_char_and_incr_iter(String_type &s, typename String_type::const_iterator &begin, typename String_type::const_iterator end)
const spirit_namespace::uint_parser< boost::uint64_t > uint64_p
String_type get_str_(typename String_type::const_iterator begin, typename String_type::const_iterator end)
Char_type unicode_str_to_char(Iter_type &begin)
void add_posn_iter_and_read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)
String_type substitute_esc_chars(typename String_type::const_iterator begin, typename String_type::const_iterator end)
Iter_type read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)
bool is_eq(Iter_type first, Iter_type last, const char *c_str)
void throw_error(spirit_namespace::position_iterator< Iter_type > i, const std::string &reason)
Char_type hex_str_to_char(Iter_type &begin)
void read_string_or_throw(const String_type &s, Value_type &value)
bool read_range(Iter_type &begin, Iter_type end, Value_type &value)
Char_type hex_to_num(const Char_type c)
bool read_stream(Istream_type &is, Value_type &value)
void read_stream_or_throw(Istream_type &is, Value_type &value)
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
bool read_string(const String_type &s, Value_type &value)
const spirit_namespace::int_parser< boost::int64_t > int64_p
std::istream_iterator< Char_type, Char_type > istream_iter
spirit_namespace::multi_pass< istream_iter > Mp_iter