Flow123d  release_2.1.0-84-g6a13a75
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 
222  void begin_obj( Char_type c )
223  {
224  assert( c == '{' );
225 
226  begin_compound< Object_type >();
227  }
228 
229  void end_obj( Char_type c )
230  {
231  assert( c == '}' );
232 
233  end_compound();
234  }
235 
236  void begin_array( Char_type c )
237  {
238  assert( c == '[' );
239 
240  begin_compound< Array_type >();
241  }
242 
243  void end_array( Char_type c )
244  {
245  assert( c == ']' );
246 
247  end_compound();
248  }
249 
250  void new_name( Iter_type begin, Iter_type end )
251  {
252  assert( current_p_->type() == obj_type );
253 
254  name_ = get_str< String_type >( begin, end );
255  }
256 
257  void new_str( Iter_type begin, Iter_type end )
258  {
259  add_to_current( get_str< String_type >( begin, end ) );
260  }
261 
262  void new_true( Iter_type begin, Iter_type end )
263  {
264  assert( is_eq( begin, end, "true" ) );
265 
266  add_to_current( true );
267  }
268 
269  void new_false( Iter_type begin, Iter_type end )
270  {
271  assert( is_eq( begin, end, "false" ) );
272 
273  add_to_current( false );
274  }
275 
276  void new_null( Iter_type begin, Iter_type end )
277  {
278  assert( is_eq( begin, end, "null" ) );
279 
281  }
282 
283  void new_int( boost::int64_t i )
284  {
285  add_to_current( i );
286  }
287 
288  void new_uint64( boost::uint64_t ui )
289  {
290  add_to_current( ui );
291  }
292 
293  void new_real( double d )
294  {
295  add_to_current( d );
296  }
297 
298  private:
299 
301  // to prevent "assignment operator could not be generated" warning
302 
304  {
305  assert( current_p_ == 0 );
306 
307  value_ = value;
308  current_p_ = &value_;
309  return current_p_;
310  }
311 
312  template< class Array_or_obj >
314  {
315  if( current_p_ == 0 )
316  {
317  add_first( Array_or_obj() );
318  }
319  else
320  {
321  stack_.push_back( current_p_ );
322 
323  Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
324 
325  current_p_ = add_to_current( new_array_or_obj );
326  }
327  }
328 
330  {
331  if( current_p_ != &value_ )
332  {
333  current_p_ = stack_.back();
334 
335  stack_.pop_back();
336  }
337  }
338 
340  {
341  if( current_p_ == 0 )
342  {
343  return add_first( value );
344  }
345  else if( current_p_->type() == array_type )
346  {
347  current_p_->get_array().push_back( value );
348 
349  return &current_p_->get_array().back();
350  }
351 
352  assert( current_p_->type() == obj_type );
353 
354  return &Config_type::add( current_p_->get_obj(), name_, value );
355  }
356 
357  Value_type& value_; // this is the object or array that is being created
358  Value_type* current_p_; // the child object or array that is currently being constructed
359 
360  std::vector< Value_type* > stack_; // previous child objects and arrays
361 
362  String_type name_; // of current name/value pair
363  };
364 
365  template< typename Iter_type >
366  void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )
367  {
368  throw Error_position( i.get_position().line, i.get_position().column, reason );
369  }
370 
371  template< typename Iter_type >
372  void throw_error( Iter_type i, const std::string& reason )
373  {
374  throw reason;
375  }
376 
377  // the spirit grammer
378  //
379  template< class Value_type, class Iter_type >
380  class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >
381  {
382  public:
383 
385 
386  Json_grammer( Semantic_actions_t& semantic_actions )
387  : actions_( semantic_actions )
388  {
389  }
390 
391  static void throw_not_value( Iter_type begin, Iter_type end )
392  {
393  throw_error( begin, "not a value" );
394  }
395 
396  static void throw_not_array( Iter_type begin, Iter_type end )
397  {
398  throw_error( begin, "not an array" );
399  }
400 
401  static void throw_not_object( Iter_type begin, Iter_type end )
402  {
403  throw_error( begin, "not an object" );
404  }
405 
406  static void throw_not_pair( Iter_type begin, Iter_type end )
407  {
408  throw_error( begin, "not a pair" );
409  }
410 
411  static void throw_not_colon( Iter_type begin, Iter_type end )
412  {
413  throw_error( begin, "no colon in pair" );
414  }
415 
416  static void throw_not_string( Iter_type begin, Iter_type end )
417  {
418  throw_error( begin, "not a string" );
419  }
420 
421  template< typename ScannerT >
423  {
424  public:
425 
426  definition( const Json_grammer& self )
427  {
428  using namespace spirit_namespace;
429 
430  typedef typename Value_type::String_type::value_type Char_type;
431 
432  // first we convert the semantic action class methods to functors with the
433  // parameter signature expected by spirit
434 
435  typedef boost::function< void( Char_type ) > Char_action;
436  typedef boost::function< void( Iter_type, Iter_type ) > Str_action;
437  typedef boost::function< void( double ) > Real_action;
438  typedef boost::function< void( boost::int64_t ) > Int_action;
439  typedef boost::function< void( boost::uint64_t ) > Uint64_action;
440 
441  Char_action begin_obj ( boost::bind( &Semantic_actions_t::begin_obj, &self.actions_, _1 ) );
442  Char_action end_obj ( boost::bind( &Semantic_actions_t::end_obj, &self.actions_, _1 ) );
443  Char_action begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );
444  Char_action end_array ( boost::bind( &Semantic_actions_t::end_array, &self.actions_, _1 ) );
445  Str_action new_name ( boost::bind( &Semantic_actions_t::new_name, &self.actions_, _1, _2 ) );
446  Str_action new_str ( boost::bind( &Semantic_actions_t::new_str, &self.actions_, _1, _2 ) );
447  Str_action new_true ( boost::bind( &Semantic_actions_t::new_true, &self.actions_, _1, _2 ) );
448  Str_action new_false ( boost::bind( &Semantic_actions_t::new_false, &self.actions_, _1, _2 ) );
449  Str_action new_null ( boost::bind( &Semantic_actions_t::new_null, &self.actions_, _1, _2 ) );
450  Real_action new_real ( boost::bind( &Semantic_actions_t::new_real, &self.actions_, _1 ) );
451  Int_action new_int ( boost::bind( &Semantic_actions_t::new_int, &self.actions_, _1 ) );
452  Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64, &self.actions_, _1 ) );
453 
454  // actual grammer
455 
456  json_
457  = value_ | eps_p[ &throw_not_value ]
458  ;
459 
460  value_
461  = string_[ new_str ]
462  | number_
463  | object_
464  | array_
465  | str_p( "true" ) [ new_true ]
466  | str_p( "false" )[ new_false ]
467  | str_p( "null" ) [ new_null ]
468  ;
469 
470  object_
471  = ch_p('{')[ begin_obj ]
472  >> !members_
473  >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )
474  ;
475 
476  members_
477  = pair_ >> *( ( ch_p(',') | *space_p ) >> pair_ )
478  ;
479 
480  pair_
481  = ( string_[ new_name ] | key_name_[ new_name ] )
482  >> ( ch_p(':') | ch_p('=') | eps_p[ &throw_not_colon ] )
483  >> ( value_ | eps_p[ &throw_not_value ] )
484  ;
485 
486  array_
487  = ch_p('[')[ begin_array ]
488  >> !elements_
489  >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )
490  ;
491 
492  elements_
493  = value_ >> *( ',' >> value_ )
494  ;
495 
496  string_
497  = lexeme_d // this causes white space inside a string to be retained
498  [
499  confix_p
500  (
501  '"',
502  *lex_escape_ch_p,
503  '"'
504  )
505  ]
506  ;
507 
508  key_name_
509  = alpha_p
510  >> *( alnum_p | ch_p('_') )
511  ;
512 
513  number_
514  = strict_real_p[ new_real ]
515  | int64_p [ new_int ]
516  | uint64_p [ new_uint64 ]
517  ;
518  }
519 
520  spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, key_name_, number_;
521 
522  const spirit_namespace::rule< ScannerT >& start() const { return json_; }
523  };
524 
525  private:
526 
527  Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning
528 
529  Semantic_actions_t& actions_;
530  };
531 
532  template< class Iter_type, class Value_type >
533  void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
534  {
535  typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;
536 
537  const Posn_iter_t posn_begin( begin, end );
538  const Posn_iter_t posn_end( end, end );
539 
540  read_range_or_throw( posn_begin, posn_end, value );
541  }
542 
543  template< class Istream_type >
545  {
546  typedef typename Istream_type::char_type Char_type;
547  typedef std::istream_iterator< Char_type, Char_type > istream_iter;
548  typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;
549 
550  Multi_pass_iters( Istream_type& is )
551  {
552  is.unsetf( std::ios::skipws );
553 
554  begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );
555  end_ = spirit_namespace::make_multi_pass( istream_iter() );
556  }
557 
558  Mp_iter begin_;
559  Mp_iter end_;
560  };
561 
562  // reads a JSON Value from a pair of input iterators throwing an exception on invalid input, e.g.
563  //
564  // string::const_iterator start = str.begin();
565  // const string::const_iterator next = read_range_or_throw( str.begin(), str.end(), value );
566  //
567  // The iterator 'next' will point to the character past the
568  // last one read.
569  //
570  template< class Iter_type, class Value_type >
571  Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )
572  {
573  Semantic_actions< Value_type, Iter_type > semantic_actions( value );
574 
575  const spirit_namespace::parse_info< Iter_type > info =
576  spirit_namespace::parse( begin, end,
577  Json_grammer< Value_type, Iter_type >( semantic_actions ),
578  spirit_namespace::space_p );
579 
580  if( !info.hit )
581  {
582  assert( false ); // in theory exception should already have been thrown
583  throw_error( info.stop, "error" );
584  }
585 
586  return info.stop;
587  }
588 
589  // reads a JSON Value from a pair of input iterators, e.g.
590  //
591  // string::const_iterator start = str.begin();
592  // const bool success = read_string( start, str.end(), value );
593  //
594  // The iterator 'start' will point to the character past the
595  // last one read.
596  //
597  template< class Iter_type, class Value_type >
598  bool read_range( Iter_type& begin, Iter_type end, Value_type& value )
599  {
600  try
601  {
602  begin = read_range_or_throw( begin, end, value );
603 
604  return true;
605  }
606  catch( ... )
607  {
608  return false;
609  }
610  }
611 
612  // reads a JSON Value from a string, e.g.
613  //
614  // const bool success = read_string( str, value );
615  //
616  template< class String_type, class Value_type >
618  {
619  typename String_type::const_iterator begin = s.begin();
620 
621  return read_range( begin, s.end(), value );
622  }
623 
624  // reads a JSON Value from a string throwing an exception on invalid input, e.g.
625  //
626  // read_string_or_throw( is, value );
627  //
628  template< class String_type, class Value_type >
630  {
631  add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );
632  }
633 
634  // reads a JSON Value from a stream, e.g.
635  //
636  // const bool success = read_stream( is, value );
637  //
638  template< class Istream_type, class Value_type >
639  bool read_stream( Istream_type& is, Value_type& value )
640  {
641  Multi_pass_iters< Istream_type > mp_iters( is );
642 
643  return read_range( mp_iters.begin_, mp_iters.end_, value );
644  }
645 
646  // reads a JSON Value from a stream throwing an exception on invalid input, e.g.
647  //
648  // read_stream_or_throw( is, value );
649  //
650  template< class Istream_type, class Value_type >
651  void read_stream_or_throw( Istream_type& is, Value_type& value )
652  {
653  const Multi_pass_iters< Istream_type > mp_iters( is );
654 
655  add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );
656  }
657 }
658 
659 #endif
Semantic_actions< Value_type, Iter_type > Semantic_actions_t
const spirit_namespace::rule< ScannerT > & start() const
const spirit_namespace::uint_parser< boost::uint64_t > uint64_p
std::istream_iterator< Char_type, Char_type > istream_iter
Char_type unicode_str_to_char(Iter_type &begin)
bool read_stream(Istream_type &is, Value_type &value)
Json_grammer(Semantic_actions_t &semantic_actions)
void throw_error(spirit_namespace::position_iterator< Iter_type > i, const std::string &reason)
const spirit_namespace::int_parser< boost::int64_t > int64_p
String_type get_str_(typename String_type::const_iterator begin, typename String_type::const_iterator end)
static constexpr bool value
Definition: json.hpp:87
#define spirit_namespace
bool read_string(const String_type &s, Value_type &value)
void new_name(Iter_type begin, Iter_type end)
Value_type * add_to_current(const Value_type &value)
static void throw_not_array(Iter_type begin, Iter_type end)
Value_type * add_first(const Value_type &value)
void new_false(Iter_type begin, Iter_type end)
spirit_namespace::multi_pass< istream_iter > Mp_iter
void new_true(Iter_type begin, Iter_type end)
bool is_eq(Iter_type first, Iter_type last, const char *c_str)
Semantic_actions & operator=(const Semantic_actions &)
static void throw_not_object(Iter_type begin, Iter_type end)
void new_str(Iter_type begin, Iter_type end)
Char_type hex_to_num(const Char_type c)
spirit_namespace::rule< ScannerT > value_
void append_esc_char_and_incr_iter(String_type &s, typename String_type::const_iterator &begin, typename String_type::const_iterator end)
static void throw_not_pair(Iter_type begin, Iter_type end)
String_type substitute_esc_chars(typename String_type::const_iterator begin, typename String_type::const_iterator end)
void read_stream_or_throw(Istream_type &is, Value_type &value)
static void throw_not_string(Iter_type begin, Iter_type end)
void read_string_or_throw(const String_type &s, Value_type &value)
void new_null(Iter_type begin, Iter_type end)
Char_type hex_str_to_char(Iter_type &begin)
static void throw_not_value(Iter_type begin, Iter_type end)
std::string get_str(std::string::const_iterator begin, std::string::const_iterator end)
Iter_type read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)
bool read_range(Iter_type &begin, Iter_type end, Value_type &value)
static void throw_not_colon(Iter_type begin, Iter_type end)
void add_posn_iter_and_read_range_or_throw(Iter_type begin, Iter_type end, Value_type &value)