From e8326ca66c096714b8bd1361e3305dc3b0b2090d Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Sat, 17 May 2014 03:00:07 -0400 Subject: [PATCH] Make objectFromStream always throw parse_error_exception when throwing --- src/io/json.cpp | 61 +++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/io/json.cpp b/src/io/json.cpp index e17729e..641164b 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -43,9 +43,8 @@ namespace fc FC_THROW_EXCEPTION( parse_error_exception, "Expected '\\'" ); } - template - void skip_white_space( T& in ) + void skip_white_space( T& in ) { while( true ) { @@ -67,18 +66,18 @@ namespace fc fc::string stringFromStream( T& in ) { fc::stringstream token; - try + try { char c = in.peek(); if( c != '"' ) - FC_THROW_EXCEPTION( parse_error_exception, - "Expected '\"' but read '${char}'", + FC_THROW_EXCEPTION( parse_error_exception, + "Expected '\"' but read '${char}'", ("char", string(&c, (&c) + 1) ) ); in.get(); while( true ) { - + switch( c = in.peek() ) { case '\\': @@ -94,14 +93,14 @@ namespace fc } FC_THROW_EXCEPTION( parse_error_exception, "EOF before closing '\"' in string '${token}'", ("token", token.str() ) ); - } FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", + } FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", ("token", token.str() ) ); } template fc::string stringFromToken( T& in ) { fc::stringstream token; - try + try { char c = in.peek(); @@ -128,12 +127,12 @@ namespace fc } } return token.str(); - } + } catch( const fc::eof_exception& eof ) { return token.str(); } - FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", + FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", ("token", token.str() ) ); } @@ -143,10 +142,10 @@ namespace fc mutable_variant_object obj; try { - char c = in.peek(); + char c = in.peek(); if( c != '{' ) - FC_THROW_EXCEPTION( parse_error_exception, - "Expected '{', but read '${char}'", + FC_THROW_EXCEPTION( parse_error_exception, + "Expected '{', but read '${char}'", ("char",string(&c, &c + 1)) ); in.get(); skip_white_space(in); @@ -176,8 +175,10 @@ namespace fc return obj; } FC_THROW_EXCEPTION( parse_error_exception, "Expected '}' after ${variant}", ("variant", obj ) ); - - + } + catch( const fc::eof_exception& e ) + { + FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF: ${e}", ("e", e.to_detail_string() ) ); } FC_RETHROW_EXCEPTIONS( warn, "Error parsing object" ); } @@ -194,7 +195,7 @@ namespace fc while( in.peek() != ']' ) { - while( in.peek() == ',' ) + while( in.peek() == ',' ) in.get(); ar.push_back( variant_from_stream(in) ); skip_white_space(in); @@ -204,7 +205,7 @@ namespace fc ("variant", ar) ); in.get(); - } FC_RETHROW_EXCEPTIONS( warn, "Attempting to parse array ${array}", + } FC_RETHROW_EXCEPTIONS( warn, "Attempting to parse array ${array}", ("array", ar ) ); return ar; } @@ -228,11 +229,11 @@ namespace fc char c; while((c = in.peek()) && !done) { - + switch( c ) { case '.': - if (dot) + if (dot) FC_THROW_EXCEPTION(parse_error_exception, "Can't parse a number with two decimal places"); dot = true; case '0': @@ -265,7 +266,7 @@ namespace fc FC_THROW_EXCEPTION(parse_error_exception, "Can't parse token \"${token}\" as a JSON numeric constant", ("token", str)); if( dot ) return to_double(str); - if( neg ) + if( neg ) return to_int64(str); return to_uint64(str); } @@ -309,7 +310,7 @@ namespace fc if( str == "null" ) return variant(); if( str == "true" ) return true; if( str == "false" ) return false; - else + else { if (received_eof) FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF" ); @@ -372,7 +373,7 @@ namespace fc default: // ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) ); return stringFromToken(in); - in.get(); // + in.get(); // ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) ); return variant(); } @@ -395,7 +396,7 @@ namespace fc void toUTF8( const wchar_t c, ostream& os ) { - utf8::utf16to8( &c, (&c)+1, ostream_iterator(os) ); + utf8::utf16to8( &c, (&c)+1, ostream_iterator(os) ); } */ @@ -462,7 +463,7 @@ namespace fc { os << '{'; auto itr = o.begin(); - + while( itr != o.end() ) { escape_string( itr->key(), os ); @@ -480,13 +481,13 @@ namespace fc { switch( v.get_type() ) { - case variant::null_type: + case variant::null_type: os << "null"; return; - case variant::int64_type: + case variant::int64_type: os << v.as_int64(); return; - case variant::uint64_type: + case variant::uint64_type: os << v.as_uint64(); return; case variant::double_type: @@ -531,7 +532,7 @@ namespace fc switch( v[i] ) { case '\\': if( !escape ) { - if( quote ) + if( quote ) escape = true; } else { escape = false; } ss<( p, ifstream::binary ); //auto tmp = std::make_shared( p.generic_string().c_str(), std::ios::binary ); - //buffered_istream bi( tmp ); + //buffered_istream bi( tmp ); std::ifstream bi( p.generic_string().c_str(), std::ios::binary ); return variant_from_stream( bi ); }