From 4d782e894f9866e87ca82448526881666ebd3082 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 3 Mar 2018 22:03:23 +0100 Subject: [PATCH] Deduplicate some code --- src/io/json.cpp | 41 +++++++---------------------------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/src/io/json.cpp b/src/io/json.cpp index 2c453cf..ca1c7fb 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -461,21 +461,9 @@ namespace fc { try { check_string_depth( utf8_str ); - fc::stringstream in( utf8_str ); - //in.exceptions( std::ifstream::eofbit ); - switch( ptype ) - { - case legacy_parser: - return variant_from_stream( in ); - case legacy_parser_with_string_doubles: - return variant_from_stream( in ); - case strict_parser: - return json_relaxed::variant_from_stream( in ); - case relaxed_parser: - return json_relaxed::variant_from_stream( in ); - default: - FC_ASSERT( false, "Unknown JSON parser type {ptype}", ("ptype", ptype) ); - } + fc::istream_ptr in( new fc::stringstream( utf8_str ) ); + fc::buffered_istream bin( in ); + return from_stream( bin, ptype ); } FC_RETHROW_EXCEPTIONS( warn, "", ("str",utf8_str) ) } variants json::variants_from_string( const std::string& utf8_str, parse_type ptype ) @@ -838,25 +826,10 @@ namespace fc bool json::is_valid( const std::string& utf8_str, parse_type ptype ) { if( utf8_str.size() == 0 ) return false; - fc::stringstream in( utf8_str ); - switch( ptype ) - { - case legacy_parser: - variant_from_stream( in ); - break; - case legacy_parser_with_string_doubles: - variant_from_stream( in ); - break; - case strict_parser: - json_relaxed::variant_from_stream( in ); - break; - case relaxed_parser: - json_relaxed::variant_from_stream( in ); - break; - default: - FC_ASSERT( false, "Unknown JSON parser type {ptype}", ("ptype", ptype) ); - } - try { in.peek(); } catch ( const eof_exception& e ) { return true; } + fc::istream_ptr in( new fc::stringstream( utf8_str ) ); + fc::buffered_istream bin( in ); + from_stream( bin, ptype ); + try { bin.peek(); } catch ( const eof_exception& e ) { return true; } return false; }