diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index 502209e..22d2f93 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -172,57 +172,21 @@ class JSON } template - JSON( T b, typename enable_if::value>::type* = nullptr ) : internal( static_cast(b) ) {} + explicit JSON( T b, typename enable_if::value>::type* = nullptr ) : internal( static_cast(b) ) {} template - JSON( T i, typename enable_if::value && !is_same::value>::type* = nullptr ) : internal( static_cast(i) ) {} + explicit JSON( T i, typename enable_if::value && !is_same::value>::type* = nullptr ) : internal( static_cast(i) ) {} template - JSON( T f, typename enable_if::value>::type* = nullptr ) : internal( static_cast(f) ) {} + explicit JSON( T f, typename enable_if::value>::type* = nullptr ) : internal( static_cast(f) ) {} template - JSON( T s, typename enable_if::value>::type* = nullptr ) : internal( static_cast(s) ) {} + explicit JSON( T s, typename enable_if::value>::type* = nullptr ) : internal( static_cast(s) ) {} static JSON Load( const std::string & ); - template - void append( T arg ) { - internal.set_type( Class::Array ); - internal.List->emplace_back( arg ); - } - - template - void append( T arg, U... args ) { - append( arg ); - append( args... ); - } - - template - typename enable_if::value, JSON&>::type operator=( T b ) { - internal = Internal(static_cast(b)); - return *this; - } - - template - typename enable_if::value && !is_same::value, JSON&>::type operator=( T i ) { - internal = Internal(static_cast(i)); - return *this; - } - - template - typename enable_if::value, JSON&>::type operator=( T f ) { - internal = Internal(static_cast(f)); - return *this; - } - - template - typename enable_if::value, JSON&>::type operator=( T s ) { - internal = Internal(static_cast(s)); - return *this; - } - JSON& operator[]( const std::string &key ) { internal.set_type( Class::Object ); return internal.Map->operator[]( key ); @@ -235,9 +199,9 @@ class JSON } return internal.List->operator[]( index ); - } + JSON &at( const std::string &key ) { return operator[]( key ); } @@ -254,6 +218,7 @@ class JSON return internal.List->at( index ); } + long length() const { if( internal.Type == Class::Array ) { return static_cast(internal.List->size()); @@ -525,7 +490,6 @@ struct JSONParser { } static JSON parse_number( const std::string &str, size_t &offset ) { - JSON Number; std::string val, exp_str; char c = '\0'; bool isDouble = false; @@ -564,29 +528,27 @@ struct JSONParser { } --offset; - if( isDouble ) - Number = chaiscript::parse_num( val ) * std::pow( 10, exp ); - else { - if( !exp_str.empty() ) - Number = static_cast(chaiscript::parse_num( val )) * std::pow( 10, exp ); - else - Number = chaiscript::parse_num( val ); + if( isDouble ) { + return JSON(chaiscript::parse_num( val ) * std::pow( 10, exp )); + } else { + if( !exp_str.empty() ) { + return JSON(static_cast(chaiscript::parse_num( val )) * std::pow( 10, exp )); + } else { + return JSON(chaiscript::parse_num( val )); + } } - return Number; } static JSON parse_bool( const std::string &str, size_t &offset ) { - JSON Bool; if( str.substr( offset, 4 ) == "true" ) { offset += 4; - Bool = true; + return JSON(true); } else if( str.substr( offset, 5 ) == "false" ) { offset += 5; - Bool = false; + return JSON(false); } else { throw std::runtime_error(std::string("JSON ERROR: Bool: Expected 'true' or 'false', found '") + str.substr( offset, 5 ) + "'"); } - return Bool; } static JSON parse_null( const std::string &str, size_t &offset ) { diff --git a/include/chaiscript/utility/json_wrap.hpp b/include/chaiscript/utility/json_wrap.hpp index 3d4b0d4..aec927b 100644 --- a/include/chaiscript/utility/json_wrap.hpp +++ b/include/chaiscript/utility/json_wrap.hpp @@ -102,32 +102,24 @@ namespace chaiscript try { Boxed_Number bn(t_bv); - json::JSON obj; if (Boxed_Number::is_floating_point(t_bv)) { - obj = bn.get_as(); + return json::JSON(bn.get_as()); } else { - obj = bn.get_as(); + return json::JSON(bn.get_as()); } - return obj; } catch (const chaiscript::detail::exception::bad_any_cast &) { // not a number } try { - bool b = boxed_cast(t_bv); - json::JSON obj; - obj = b; - return obj; + return json::JSON(boxed_cast(t_bv)); } catch (const chaiscript::exception::bad_boxed_cast &) { // not a bool } try { - std::string s = boxed_cast(t_bv); - json::JSON obj; - obj = s; - return obj; + return json::JSON(boxed_cast(t_bv)); } catch (const chaiscript::exception::bad_boxed_cast &) { // not a string }