1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/* Copyright (c) 2007 John W Wilkinson
This source code can be used for any purpose as long as
this comment is retained. */
// json spirit version 2.01
#include "json_spirit_reader.h"
#include "json_spirit_value.h"
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/spirit/core.hpp>
#include <boost/spirit/utility/confix.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <boost/spirit/utility/lists.hpp>
using namespace json_spirit;
using namespace std;
using namespace boost;
using namespace boost::spirit;
namespace
{
// this class solely allows its inner classes and methods to
// be conveniently templatised by Value type and dependent types
//
template< class Value_t >
struct Reader
{
typedef typename Value_t::String_type String_t;
typedef typename Value_t::Object Object_t;
typedef typename Value_t::Array Array_t;
typedef typename String_t::value_type Char_t;
typedef typename String_t::const_iterator Iter_t;
typedef Pair_impl< String_t > Pair_t;
typedef std::basic_istream< Char_t > Istream_t;
// this class's methods get called by the spirit parse resulting
// in the creation of a JSON object or array
//
class Semantic_actions
{
public:
Semantic_actions( Value_t& value )
: value_( value )
, current_p_( 0 )
{
}
void begin_obj( Char_t c )
{
assert( c == '{' );
begin_compound< Object_t >();
}
void end_obj( Char_t c )
{
assert( c == '}' );
end_compound();
}
void begin_array( Char_t c )
{
assert( c == '[' );
begin_compound< Array_t >();
}
void end_array( Char_t c )
{
assert( c == ']' );
end_compound();
}
void new_name( Iter_t str, Iter_t end )
{
assert( current_p_->type() == obj_type );
name_ = get_str( str, end );
}
void new_str( Iter_t str, Iter_t end )
{
add_to_current( get_str( str, end ) );
}
void new_true( Iter_t str, Iter_t end )
{
assert( is_eq( str, end, "true" ) );
add_to_current( true );
}
void new_false( Iter_t str, Iter_t end )
{
assert( is_eq( str, end, "false" ) );
add_to_current( false );
}
void new_null( Iter_t str, Iter_t end )
{
assert( is_eq( str, end, "null" ) );
add_to_current( Value_t() );
}
void new_int( int i )
{
add_to_current( i );
}
void new_real( double d )
{
add_to_current( d );
}
private:
template< class Array_or_obj >
void begin_compound()
{
if( current_p_ == 0 )
{
value_ = Array_or_obj();
current_p_ = &value_;
}
else
{
stack_.push_back( current_p_ );
Array_or_obj new_array_or_obj; // avoid copy by building new array or object in place
add_to_current( new_array_or_obj );
if( current_p_->type() == array_type )
{
current_p_ = ¤t_p_->get_array().back();
}
else
{
current_p_ = ¤t_p_->get_obj().back().value_;
}
}
}
void end_compound()
{
if( current_p_ != &value_ )
{
current_p_ = stack_.back();
stack_.pop_back();
}
}
void add_to_current( const Value_t& value )
{
if( current_p_->type() == array_type )
{
current_p_->get_array().push_back( value );
}
else
{
current_p_->get_obj().push_back( Pair_t( name_, value ) );
}
}
Value_t& value_; // this is the object or array that is being created
Value_t* current_p_; // the child object or array that is currently being constructed
vector< Value_t* > stack_; // previous child objects and arrays
String_t name_; // of current name/value pair
};
// the spirit grammer
//
class Json_grammer : public grammar< Json_grammer >
{
public:
Json_grammer( Semantic_actions& semantic_actions )
: actions_( semantic_actions )
{
}
template< typename ScannerT >
struct definition
{
definition( const Json_grammer& self )
{
// need to convert the semantic action class methods to functors with the
// parameter signature expected by spirit
typedef function< void( Char_t ) > Char_action;
typedef function< void( Iter_t, Iter_t ) > Str_action;
typedef function< void( double ) > Real_action;
typedef function< void( int ) > Int_action;
Char_action begin_obj ( bind( &Semantic_actions::begin_obj, &self.actions_, _1 ) );
Char_action end_obj ( bind( &Semantic_actions::end_obj, &self.actions_, _1 ) );
Char_action begin_array( bind( &Semantic_actions::begin_array, &self.actions_, _1 ) );
Char_action end_array ( bind( &Semantic_actions::end_array, &self.actions_, _1 ) );
Str_action new_name ( bind( &Semantic_actions::new_name, &self.actions_, _1, _2 ) );
Str_action new_str ( bind( &Semantic_actions::new_str, &self.actions_, _1, _2 ) );
Str_action new_true ( bind( &Semantic_actions::new_true, &self.actions_, _1, _2 ) );
Str_action new_false ( bind( &Semantic_actions::new_false, &self.actions_, _1, _2 ) );
Str_action new_null ( bind( &Semantic_actions::new_null, &self.actions_, _1, _2 ) );
Real_action new_real ( bind( &Semantic_actions::new_real, &self.actions_, _1 ) );
Int_action new_int ( bind( &Semantic_actions::new_int, &self.actions_, _1 ) );
json_
= ( object_ | array_ ) >> end_p
;
object_
= confix_p
(
ch_p('{')[ begin_obj ],
*members_,
ch_p('}')[ end_obj ]
)
;
members_
= pair_ >> *( ',' >> pair_ )
;
pair_
= string_[ new_name ]
>> ':'
>> value_
;
value_
= string_[ new_str ]
| number_
| object_
| array_
| str_p( "true" ) [ new_true ]
| str_p( "false" )[ new_false ]
| str_p( "null" ) [ new_null ]
;
array_
= confix_p
(
ch_p('[')[ begin_array ],
*list_p( elements_, ',' ),
ch_p(']')[ end_array ]
)
;
elements_
= value_ >> *( ',' >> value_ )
;
string_
= lexeme_d // this causes white space inside a string to be retained
[
confix_p
(
'"',
*lex_escape_ch_p,
'"'
)
]
;
number_
= strict_real_p[ new_real ]
| int_p [ new_int ]
;
}
rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;
const rule< ScannerT >& start() const { return json_; }
};
Semantic_actions& actions_;
};
static bool is_eq( Iter_t str, Iter_t end, const char* c_str )
{
const String_t s1( str, end );
const string s2( c_str );
if( s1.length() != s2.length() ) return false;
for( typename String_t::size_type i = 0; i < s1.length(); ++i )
{
if( s1[i] != s2[i] ) return false;
}
return true;
}
static Char_t hex_to_num( const Char_t c )
{
if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';
if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;
if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;
return 0;
}
static Char_t hex_str_to_char( Iter_t& str )
{
const Char_t c1( *( ++str ) );
const Char_t c2( *( ++str ) );
return hex_to_num( c1 ) * 0x10 + hex_to_num( c2 );
}
static Char_t unicode_str_to_char( Iter_t& str )
{
const Char_t c1( *( ++str ) );
const Char_t c2( *( ++str ) );
const Char_t c3( *( ++str ) );
const Char_t c4( *( ++str ) );
return hex_to_num( c1 ) * 0x1000 + hex_to_num( c2 ) * 0x100 + hex_to_num( c3 ) * 0x10 + hex_to_num( c4 );
}
static String_t substitute_esc_chars( Iter_t str, Iter_t end )
{
if( end - str < 2 ) return String_t( str, end );
String_t result;
Iter_t end_minus_1( end - 1 );
for( Iter_t i = str; i < end; ++i )
{
const Char_t c1( *i );
if( ( c1 == '\\' ) && ( i != end_minus_1 ) )
{
++i;
const Char_t c2( *i );
switch( c2 )
{
case 't': result += '\t'; break;
case 'b': result += '\b'; break;
case 'f': result += '\f'; break;
case 'n': result += '\n'; break;
case 'r': result += '\r'; break;
case '\\': result += '\\'; break;
case '/': result += '/'; break;
case '"': result += '"'; break;
case 'x':
{
if( end - i >= 3 ) // expecting "xHH..."
{
result += hex_str_to_char( i );
}
break;
}
case 'u':
{
if( end - i >= 5 ) // expecting "uHHHH..."
{
result += unicode_str_to_char( i );
}
break;
}
}
}
else
{
result += c1;
}
}
return result;
}
static String_t get_str( Iter_t str, Iter_t end )
{
assert( end - str >= 2 );
Iter_t str_without_quotes( str + 1 );
Iter_t end_without_quotes( end - 1 );
return substitute_esc_chars( str_without_quotes, end_without_quotes );
}
static void stream_to_string( Istream_t& is, String_t& s )
{
typedef istream_iterator< Char_t, Char_t > istream_iter;
is.unsetf( ios::skipws );
copy( istream_iter( is ), istream_iter(), back_inserter( s ) );
}
static bool read_string( const String_t& s, Value_t& value )
{
Semantic_actions semantic_actions( value );
parse_info< Iter_t > info = parse( s.begin(), s.end(),
Json_grammer( semantic_actions ),
space_p );
return info.full;
}
static bool read_stream( Istream_t& is, Value_t& value )
{
String_t s;
stream_to_string( is, s );
return read_string( s, value );
}
};
}
bool json_spirit::read( const std::string& s, Value& value )
{
return Reader< Value >::read_string( s, value );
}
bool json_spirit::read( std::istream& is, Value& value )
{
return Reader< Value >::read_stream( is, value );
}
#ifndef BOOST_NO_STD_WSTRING
bool json_spirit::read( const std::wstring& s, wValue& value )
{
return Reader< wValue >::read_string( s, value );
}
bool json_spirit::read( std::wistream& is, wValue& value )
{
return Reader< wValue >::read_stream( is, value );
}
#endif
|